页面二
页面需求
需要在change页面中出现两列内容,左边列为1-30,在数字后跟删除按钮;右边为31-60,数字后为删除按钮,在点击某一边的删除按钮后,对应的数组会跳转到另一列中。
流程说明
首先,需要创建change_page.xml页面,在页面中使用线性布局,添加两个RecyclerView,然后再单独创建item_demo.xml为RecyclerView中的item设置样式。
接下来创建适配器RecycleAdapterDemo.java ① 创建一个继承RecyclerView.Adapter的Adapter类 ② 创建一个继承RecyclerView.ViewHolder的静态内部类 ③ 在Adapter中实现3个方法: onCreateViewHolder()(用来绑定xml样式) onBindViewHolder()(可以用来绑定监听事件) getItemCount()
再在内部类绑定控件,其中再创建方法绑定TextView的id。
定义点击事件接口,声明监听器,并提供其set方法。
代码
RecycleAdapterDemo.java
package com.example.firstproject;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class RecycleAdapterDemo extends RecyclerView.Adapter<RecycleAdapterDemo.MyViewHolder> {
private Context context;
private List<Integer> list;
private View inflater;
private OnClickListener mOnClickListener;
public RecycleAdapterDemo(Context context, List<Integer> list) {
this.context = context;
this.list = list;
}
public void setmOnClickListener(OnClickListener onClickListener){
this.mOnClickListener = onClickListener;
}
interface OnClickListener{
void onClick(int adapterPosition);
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
inflater = LayoutInflater.from(context).inflate(R.layout.item_demo, parent, false);
MyViewHolder myViewHolder = new MyViewHolder(inflater);
return myViewHolder;
}
@Override
public void onBindViewHolder(RecycleAdapterDemo.MyViewHolder holder, int position) {
holder.textView.setText(String.valueOf(list.get(position)));
holder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mOnClickListener != null)
mOnClickListener.onClick(holder.getAdapterPosition());
}
});
}
@Override
public int getItemCount() {
return list.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView textView;
Button button;
public MyViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.text_view);
button = (Button) itemView.findViewById(R.id.button);
}
}
}
ChangeActivity.java
package com.example.firstproject;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Context;
import android.os.Bundle;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class ChangeActivity extends AppCompatActivity {
private RecyclerView recyclerView1;
private RecyclerView recyclerView2;
private RecycleAdapterDemo adapterDemo1;
private RecycleAdapterDemo adapterDemo2;
private Context context;
private List<Integer> list1;
private List<Integer> list2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
setContentView(R.layout.change_page);
recyclerView1 = (RecyclerView) findViewById(R.id.recyclerView_left);
recyclerView2 = (RecyclerView) findViewById(R.id.recyclerView_right);
list1 = new ArrayList<>();
for (int i = 1; i <= 30; i++) {
list1.add(i);
}
list2 = new ArrayList<>();
for (int i = 31; i <= 60; i++) {
list2.add(i);
}
adapterDemo1 = new RecycleAdapterDemo(context, list1);
adapterDemo2 = new RecycleAdapterDemo(context, list2);
LinearLayoutManager manager1 = new LinearLayoutManager(context);
manager1.setOrientation(LinearLayoutManager.VERTICAL);
LinearLayoutManager manager2 = new LinearLayoutManager(context);
manager2.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView1.setLayoutManager(manager1);
recyclerView1.setAdapter(adapterDemo1);
recyclerView2.setLayoutManager(manager2);
recyclerView2.setAdapter(adapterDemo2);
adapterDemo1.setmOnClickListener(new RecycleAdapterDemo.OnClickListener() {
@Override
public void onClick(int position) {
Toast.makeText(ChangeActivity.this, "pos:"+list1.get(position), Toast.LENGTH_SHORT).show();
int temp = list1.get(position);
list1.remove(position);
list2.add(0,temp);
adapterDemo1.notifyItemRemoved(position);
adapterDemo2.notifyDataSetChanged();
}
});
adapterDemo2.setmOnClickListener(new RecycleAdapterDemo.OnClickListener() {
@Override
public void onClick(int position) {
Toast.makeText(ChangeActivity.this, "pos:"+list2.get(position), Toast.LENGTH_SHORT).show();
int temp = list2.get(position);
list2.remove(position);
list1.add(0,temp);
adapterDemo2.notifyItemRemoved(position);
adapterDemo1.notifyDataSetChanged();
}
});
}
}
AndroidManifest.xml配置
<activity
android:name=".ChangeActivity"
android:exported="true">
</activity>
|