本篇文章只可以实现简单的点击“编辑”-- 后“我的频道”里面的内容可以删掉回到下面“其他频道里面”,,点击“其他频道”里的内容可以到上面的“我的频道”里。
首先需要几个类,分别是:Mainactivity类、Myadapter类、Myadapter2类、SpacesItemDecoration类、
itemBean类、Mainview类(是一个接口)
Mainactivity类
public class MainActivity extends AppCompatActivity implements View.OnClickListener,MainView { private TextView text3_act; private LinearLayout lin; private RecyclerView recy1; private RecyclerView recy2; private ArrayList<ItemBean> myarr; private ArrayList<ItemBean> himarr; private int screenWidth; private MyAdapter myAdapter1; private MyAdapter2 myAdapter2; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //去标题 getSupportActionBar().hide(); //2个for循环 initFor(); //各种控件id initView(); //下面的数据for盼盼 initDataOne(); //下面的数据for他 initDataTwo(); } private void initFor() { myarr = new ArrayList<>(); for (int i = 0; i < 15; i++) { myarr.add(new ItemBean("me的频道"+i,false)); } himarr = new ArrayList<>(); for (int i = 0; i < 15; i++) { himarr.add(new ItemBean("he频道"+i,false)); } } private void initDataOne() { GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3); recy1.setLayoutManager(gridLayoutManager); myAdapter1 = new MyAdapter(MainActivity.this); myAdapter1.setData(myarr); myAdapter1.setOnclickListener(this); recy1.setAdapter(myAdapter1); //设置recycleview之间的间距 int spacingInPixels = 30; recy1.addItemDecoration(new SpacesItemDecoration(spacingInPixels)); recy1.setAdapter(myAdapter1); } private void initDataTwo() { GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3); recy2.setLayoutManager(gridLayoutManager); myAdapter2 = new MyAdapter2(MainActivity.this); this.myAdapter2.setData(himarr); this.myAdapter2.setOnclickListener(this); recy2.setAdapter(this.myAdapter2); //设置recycleview之间的间距 int spacingInPixels = 30; recy2.addItemDecoration(new SpacesItemDecoration(spacingInPixels)); recy2.setAdapter(this.myAdapter2); } //各种控件 private void initView() { text3_act = (TextView) findViewById(R.id.text3_act); recy1 = (RecyclerView) findViewById(R.id.recy1); recy2 = (RecyclerView) findViewById(R.id.recy2); text3_act.setOnClickListener(this); } //“编辑”的点击 @Override public void onClick(View v) { switch (v.getId()) { case R.id.text3_act: CharSequence text = text3_act.getText(); if (text.equals("编辑")) { text3_act.setText("完成"); myAdapter1.setIsShow(true); myAdapter1.notifyDataSetChanged(); } else if (text.equals("完成")) { text3_act.setText("编辑"); myAdapter1.setIsShow(false); myAdapter1.notifyDataSetChanged(); } break; } } @Override public void MyItenOnclickLister(View view, int position,int type) { switch (type){ case 1: himarr.add(myarr.get(position)); myAdapter2.setData(himarr); myAdapter2.notifyDataSetChanged(); myarr.remove(position); myAdapter1.setData(myarr); myAdapter1.notifyDataSetChanged(); break; case 2: myarr.add(himarr.get(position)); myAdapter1.setData(myarr); myAdapter1.notifyDataSetChanged(); himarr.remove(position); myAdapter2.setData(himarr); myAdapter2.notifyDataSetChanged(); // Toast.makeText(this, ""+position, Toast.LENGTH_SHORT).show(); break; } } }
Myadapter类
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHoder> { private Context context; private ArrayList<ItemBean> list = new ArrayList<>(); private boolean isshow = false; private MainView mainActivity; public MyAdapter(Context context) { this.context = context; } @Override public MyHoder onCreateViewHolder(ViewGroup parent, int viewType) { View inflate = View.inflate(context, R.layout.item, null); MyHoder myHoder = new MyHoder(inflate); return myHoder; } @Override public void onBindViewHolder(MyHoder holder, final int position) { if (isshow) { if (position == 0 || position == 1) { holder.delete_item.setVisibility(View.GONE); } else { holder.delete_item.setVisibility(View.VISIBLE); } } else { holder.delete_item.setVisibility(View.GONE); } //点击小叉叉,删除该条目 holder.delete_item.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //判断引用是否为空 if (mainActivity != null) { mainActivity.MyItenOnclickLister(v, position,1); } } }); holder.text_item.setText(list.get(position).getText()); } @Override public int getItemCount() { return list != null ? list.size() : 0; } //做标记显示小叉叉的 public void setIsShow(boolean b) { this.isshow = b; } public void setOnclickListener(MainActivity mainActivity) { this.mainActivity = mainActivity; } public void setData(ArrayList<ItemBean> myarr) { list.clear(); if (myarr != null) { list.addAll(myarr); } } //MyHoder class MyHoder extends RecyclerView.ViewHolder { private final TextView text_item; private final TextView delete_item; public MyHoder(View itemView) { super(itemView); text_item = (TextView) itemView.findViewById(R.id.text_item); delete_item = (TextView) itemView.findViewById(R.id.delete_item); } } }
Myadapter2类
public class MyAdapter2 extends RecyclerView.Adapter<MyAdapter2.MyHoder> { private Context context; private ArrayList<ItemBean> list = new ArrayList<>(); private boolean isshow = false; private MainView mainActivity; public MyAdapter2(Context context) { this.context = context; } @Override public MyHoder onCreateViewHolder(ViewGroup parent, int viewType) { View inflate = View.inflate(context, R.layout.item, null); MyHoder myHoder = new MyHoder(inflate); return myHoder; } @Override public void onBindViewHolder(MyHoder holder, final int position) { if (isshow) { if (position == 0 || position == 1) { holder.delete_item.setVisibility(View.GONE); } else { holder.delete_item.setVisibility(View.VISIBLE); } } else { holder.delete_item.setVisibility(View.GONE); } //点击小叉叉,删除该条目 holder.delete_item.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //判断引用是否为空 if (mainActivity != null) { mainActivity.MyItenOnclickLister(v, position,1); } } }); //长按条目,移动。(目前还没有使用) holder.text_item.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mainActivity.MyItenOnclickLister(v,position,2); } }); holder.text_item.setText(list.get(position).getText()); } @Override public int getItemCount() { return list != null ? list.size() : 0; } //做标记显示小叉叉的 public void setIsShow(boolean b) { this.isshow = b; } public void setOnclickListener(MainActivity mainActivity) { this.mainActivity = mainActivity; } public void setData(ArrayList<ItemBean> myarr) { list.clear(); if (myarr != null) { list.addAll(myarr); } } //MyHoder class MyHoder extends RecyclerView.ViewHolder { private final TextView text_item; private final TextView delete_item; public MyHoder(View itemView) { super(itemView); text_item = (TextView) itemView.findViewById(R.id.text_item); delete_item = (TextView) itemView.findViewById(R.id.delete_item); } } }
SpacesItemDecoration类
public class SpacesItemDecoration extends RecyclerView.ItemDecoration { private int space; public SpacesItemDecoration(int space) { this.space = space; } @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { outRect.left = space; outRect.right = space; outRect.bottom = space; } }
itemBean类
public class ItemBean { private String text; private boolean select; public ItemBean(String text, boolean select) { this.text = text; this.select = select; } public String getText() { return text; } public void setText(String text) { this.text = text; } public boolean isSelect() { return select; } public void setSelect(boolean select) { this.select = select; } }
Mainview类(是一个接口)
public interface MainView { void MyItenOnclickLister(View view,int position,int type); }