类微信界面 02

1、功能说明

编辑功能
psLxi.png

删除功能
psD8L.jpg

查看详情页
ps4DC.jpg

2、核心代码

xml部分

item.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/linearLayout_item1"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="@drawable/underline_list"
android:orientation="horizontal">

<ImageView
android:id="@+id/imageView_item1"
android:layout_width="72dp"
android:layout_height="53dp"
android:layout_gravity="center"
app:srcCompat="@drawable/ic___wx_this" />

<TextView
android:id="@+id/textView_item1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="编号"
android:textSize="16dp" />

<TextView
android:id="@+id/textView_item2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"

android:text="内容"
android:textSize="16dp" />

</LinearLayout>

tab02.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="@color/hui"
android:layout_height="match_parent"
android:orientation="horizontal">


<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never"
android:scrollbars="none"
android:visibility="visible"

/>

<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_gravity="end|bottom"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="36dp"
android:layout_marginBottom="41dp"
android:contentDescription="add"
android:src="@drawable/ic_action_name" />

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintlayout_none"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="2dp"
android:layout_marginTop="48dp"
android:layout_marginEnd="-2dp"
android:layout_marginBottom="461dp"
android:background="@color/hui"
android:visibility="gone">

<EditText
android:id="@+id/text_add"
android:layout_width="294dp"
android:layout_height="58dp"
android:layout_marginTop="4dp"
android:ems="10"
android:hint="请输入..."
android:imeOptions="actionNext"
android:inputType="text"
android:minHeight="48dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button_cl"
android:layout_width="117dp"
android:layout_height="wrap_content"
android:layout_marginTop="36dp"
android:text="取消"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/button_ok"
app:layout_constraintHorizontal_bias="0.444"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text_add"
app:layout_constraintVertical_bias="0.0" />

<Button
android:id="@+id/button_ok"
android:layout_width="137dp"
android:layout_height="48dp"
android:layout_marginTop="36dp"
android:layout_marginEnd="32dp"
android:text="确认"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text_add"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>

</RelativeLayout>

输入框开始是隐藏状态

Java部分

onBindViewHolder中设置3个组件内容

1
2
3
holder.textView1.setText("" + position);//转字符串
holder.textView2.setText(list.get(position));
holder.imageView_item1.setImageResource(addimg(position));

onBindViewHolder中的监听

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
holder.linearLayout_item1.setOnTouchListener(new View.OnTouchListener() {
private int X;
private int lastX;
private int Y;
@SuppressLint("ResourceAsColor")
@Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN://当屏幕检测到第一个触点按下之后就会触发到这个事件。
lastX = (int) event.getRawX();
Y = (int) event.getRawY();
X = (int) event.getRawX();
break;
case MotionEvent.ACTION_UP://当触点松开时被触发。
if (Math.abs((int) event.getRawX() - X)<2){//单击,点击坐标没发生变化
Intent intent = new Intent(holder.itemView.getContext(), MainActivity2.class);
intent.putExtra("msg","position:"+position+"\n"+list.get(position));
context.startActivity(intent);
}
if(Math.abs(((int) event.getRawX() - X))<200){
holder.linearLayout_item1.setLeft(0);
}else{
onSwiped(position);
holder.linearLayout_item1.setLeft(0);
}
break;
case MotionEvent.ACTION_MOVE:
int dx = (int) event.getRawX() - lastX;
int left = view.getLeft() + dx;
holder.linearLayout_item1.setLeft(left);
lastX = (int) event.getRawX();
break;
}
return true;
}
});

MotionEvent的几种触发的事件,目前存在bug.竖直滚动与左右滑动的判断
拖动还是点击的判断:根据触摸与抬起的坐标差在一定范围

删除数据

1
2
3
4
5
6
7
//  删除数据
@SuppressLint("NotifyDataSetChanged")
public void onSwiped(int p) {
list.remove(p);
notifyItemRemoved(p);
notifyDataSetChanged();
}

Fragment.java中Viewinit()函数

1
2
3
4
5
6
7
8
9
10
11
12
13
private void Viewinit() {
context = this.getActivity();
//fullview=getView();
myadapter = new Adapter(context);
//设置LayoutManager
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(myadapter);
myadapter.list(list);

}

列表的添加

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
@SuppressLint("NotifyDataSetChanged")
@Override
public void onStart() {
super.onStart();
fab=getActivity().findViewById(R.id.fab);
ConstraintLayout constraintlayout_none=getActivity().findViewById(R.id.constraintlayout_none);
EditText text_add=getActivity().findViewById(R.id.text_add);
Button button_cl=getActivity().findViewById(R.id.button_cl);
Button button_ok=getActivity().findViewById(R.id.button_ok);
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); ;
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
constraintlayout_none.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.INVISIBLE);
imm.showSoftInput(text_add, 0);
text_add.setText("");
button_ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String s=text_add.getText().toString();
constraintlayout_none.setVisibility(View.GONE);
list.add(s);
Log.d("add","+1");
imm.hideSoftInputFromWindow(text_add.getWindowToken(), 0);
myadapter.notifyDataSetChanged();
recyclerView.scrollToPosition(myadapter.getItemCount() - 1);
Toast.makeText(context, "添加成功", Toast.LENGTH_SHORT).show();
recyclerView.setVisibility(View.VISIBLE);
}
});
button_cl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
constraintlayout_none.setVisibility(View.GONE);
Toast.makeText(context, "取消添加", Toast.LENGTH_SHORT).show();
recyclerView.setVisibility(View.VISIBLE);
imm.hideSoftInputFromWindow(text_add.getWindowToken(), 0);
}
});
//list.add("111111");
//Log.d("add",list.toString());


}
});


}

思路是点击添加按钮后显示输入的layout,隐藏Reclyview,打开键盘

  • 如果点击确认则获取输入的值添加到列表,刷新reclyview,隐藏输入layout,软键盘,显示Reclyview

  • 如果点击取消直接还原视图(即隐藏输入layout,显示Reclyview…)

完整代码


Adapter.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package com.example.applicationtest;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;

public class Adapter extends RecyclerView.Adapter<Adapter.Myholder> {
private View itemview;
private Context context;

private List<String> list;


public int addimg(int i) {
if (i % 4 == 0) {
return R.drawable.num_a;
} else if (i % 4 - 1 == 0) {
return R.drawable.num_b;
} else if (i % 4 - 2 == 0) {
return R.drawable.num_c;
} else if (i % 4 - 3 == 0) {
return R.drawable.num_d;
} else {
return R.drawable.ic___fx;
}
}
public Adapter(Context context) {

this.context = context;
}

public void list(List<String> list) {
this.list = list;
notifyDataSetChanged();

}

@NonNull
@Override
public Myholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
itemview = LayoutInflater.from(context).inflate(R.layout.item1, parent, false);
Myholder holder;

holder = new Myholder(itemview);
return holder;
}

@SuppressLint({"SetTextI18n", "ClickableViewAccessibility"})
@Override
public void onBindViewHolder(@NonNull Myholder holder, @SuppressLint("RecyclerView") int position) {
holder.textView1.setText("" + position);//转字符串
holder.textView2.setText(list.get(position));
holder.imageView_item1.setImageResource(addimg(position));
// holder.linearLayout_item1.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
// //Toast.makeText(itemview.getContext(), position + "号:" + list.get(position), Toast.LENGTH_SHORT).show();
// Intent intent = new Intent(holder.itemView.getContext(), MainActivity2.class);
// intent.putExtra("msg","position:"+position+"\n"+list.get(position));
// context.startActivity(intent);
// //onSwiped(position);
// }
// });
holder.linearLayout_item1.setOnTouchListener(new View.OnTouchListener() {
private int X;
private int lastX;
private int Y;
@SuppressLint("ResourceAsColor")
@Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN://当屏幕检测到第一个触点按下之后就会触发到这个事件。
lastX = (int) event.getRawX();
Y = (int) event.getRawY();
X = (int) event.getRawX();
break;
case MotionEvent.ACTION_UP://当触点松开时被触发。
if (Math.abs((int) event.getRawX() - X)<2){//单击,点击坐标没发生变化
Intent intent = new Intent(holder.itemView.getContext(), MainActivity2.class);
intent.putExtra("msg","position:"+position+"\n"+list.get(position));
context.startActivity(intent);
}
if(Math.abs(((int) event.getRawX() - X))<200){
holder.linearLayout_item1.setLeft(0);
}else{
onSwiped(position);
holder.linearLayout_item1.setLeft(0);
}
break;
case MotionEvent.ACTION_MOVE:
int dx = (int) event.getRawX() - lastX;
int left = view.getLeft() + dx;
holder.linearLayout_item1.setLeft(left);
lastX = (int) event.getRawX();
break;
}
return true;
}
});
}
// 删除数据
@SuppressLint("NotifyDataSetChanged")
public void onSwiped(int p) {
list.remove(p);
notifyItemRemoved(p);
notifyDataSetChanged();
}


@Override
public int getItemCount() {
return list.size();
}

public static class Myholder extends RecyclerView.ViewHolder {

private TextView textView1, textView2;
private ImageView imageView_item1;
private LinearLayout linearLayout_item1;

public Myholder(@NonNull View itemView) {
super(itemView);
textView1 = itemView.findViewById(R.id.textView_item1);
textView2 = itemView.findViewById(R.id.textView_item2);
imageView_item1 = itemView.findViewById(R.id.imageView_item1);
linearLayout_item1 = itemView.findViewById(R.id.linearLayout_item1);


}
}

}

Fragment2.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package com.example.applicationtest;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.security.auth.PrivateCredentialPermission;


public class Fragment2 extends Fragment {

private RecyclerView recyclerView;
private Context context;
private FloatingActionButton fab;
private Adapter myadapter;
private List<String> list = new ArrayList<>();


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view;
view = inflater.inflate(R.layout.tab02, container, false);

recyclerView = view.findViewById(R.id.recyclerView1);
listdata();
Viewinit();
return view;



}

@SuppressLint("NotifyDataSetChanged")
@Override
public void onStart() {
super.onStart();
fab=getActivity().findViewById(R.id.fab);
ConstraintLayout constraintlayout_none=getActivity().findViewById(R.id.constraintlayout_none);
EditText text_add=getActivity().findViewById(R.id.text_add);
Button button_cl=getActivity().findViewById(R.id.button_cl);
Button button_ok=getActivity().findViewById(R.id.button_ok);
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); ;
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
constraintlayout_none.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.INVISIBLE);
imm.showSoftInput(text_add, 0);
text_add.setText("");
button_ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String s=text_add.getText().toString();
constraintlayout_none.setVisibility(View.GONE);
list.add(s);
Log.d("add","+1");
imm.hideSoftInputFromWindow(text_add.getWindowToken(), 0);
myadapter.notifyDataSetChanged();
recyclerView.scrollToPosition(myadapter.getItemCount() - 1);
Toast.makeText(context, "添加成功", Toast.LENGTH_SHORT).show();
recyclerView.setVisibility(View.VISIBLE);
}
});
button_cl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
constraintlayout_none.setVisibility(View.GONE);
Toast.makeText(context, "取消添加", Toast.LENGTH_SHORT).show();
recyclerView.setVisibility(View.VISIBLE);
imm.hideSoftInputFromWindow(text_add.getWindowToken(), 0);
}
});
//list.add("111111");
//Log.d("add",list.toString());


}
});


}

private void Viewinit() {
context = this.getActivity();
//fullview=getView();
myadapter = new Adapter(context);
//设置LayoutManager
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(myadapter);
myadapter.list(list);

}

private void listdata() {
list.add("明媚的阳光和明媚的少女······是不是很相衬呢?");
list.add("别动哦,借你的眼睛照照镜子。是不是觉得很怀念?");
list.add("我的眼睛漂亮吗?这可不是美瞳哦,是美少女的魔法。");
...

}


}

3、实验小结

  • 继承于RecyclerView.ViewHolder,编写自己的ViewHolder

  • 实现onCreateViewHolder(ViewGroup parent, int viewType)
    当RecyclerView需要我们提供类型为viewType的新ViewHolder时,会回调这个方法。实例化出了Item的根布局,并返回一个和它绑定的ViewHolder

  • 实现onBindViewHolder(VH viewHolder, int position)
    当RecyclerView需要展示对应position位置的数据时会回调这个方法。
    通过viewHolder中持有的对应position上的View,更新视图。

  • 实现getItemCount()
    返回Item的总数。

代码仓库:

https://github.com/2412322029/Applicationtest