欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android recycleView的应用和点击事件实例详解

程序员文章站 2024-02-25 23:01:03
android recycleview的应用和点击事件 效果图: 长按是删除: 下面代码: mainactivity 类 public class...

android recycleview的应用和点击事件

效果图:

Android recycleView的应用和点击事件实例详解

长按是删除:

下面代码:

mainactivity 类

public class mainactivity extends appcompatactivity {

  recyclerview rv;

  list<string> mlist = new arraylist<>();

  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);


    setcontentview(r.layout.activity_main);
    rv = (recyclerview) findviewbyid(r.id.rv);
    for (int i = 0; i < 1000; i++) {
      mlist.add("条目" + i);
    }
    final myadapter adapter = new myadapter(this, mlist);
    adapter.setonitemclicklistener(new myadapter.onitemclicklistener() {
      @override
      public void onclick(int position) {
        toast.maketext(getbasecontext(), "点击的是" + position + "的itemclick", toast.length_short).show();
      }
    });
    adapter.setonitemlongclicklistener(new myadapter.onitemlongclicklistener() {
      @override
      public void onlongclick(int position) {
        //长按删除数据
        mlist.remove(position);
        adapter.notifydatasetchanged();
      }
    });
    rv.setadapter(adapter);

    //布局管理器
    rv.setlayoutmanager(new staggeredgridlayoutmanager(5, staggeredgridlayoutmanager.vertical));
  }


  public static class vh extends recyclerview.viewholder {
    textview tv;

    public vh(view itemview) {
      super(itemview);
      tv = (textview) itemview.findviewbyid(android.r.id.text1);
      tv.setbackgroundresource(r.drawable.bg);
    }
  }


}

myadapter.java类

public class myadapter extends recyclerview.adapter<mainactivity.vh> {

  context mcontext;
  list<string> mlist;

  public myadapter(context context, list<string> list) {
    this.mcontext = context;
    this.mlist = list;
  }

  @override
  public mainactivity.vh oncreateviewholder(viewgroup parent, int viewtype) {
    return new mainactivity.vh(view.inflate(mcontext, android.r.layout.simple_list_item_1, null));
  }

  @override
  public void onbindviewholder(mainactivity.vh holder, final int position) {
    holder.tv.settext(mlist.get(position));

    //自己做item点击
    holder.itemview.setonclicklistener(new view.onclicklistener() {
      @override
      public void onclick(view v) {
        if (onitemclicklistener != null)
          onitemclicklistener.onclick(position);
      }
    });
    holder.itemview.setonlongclicklistener(new view.onlongclicklistener() {
      @override
      public boolean onlongclick(view v) {
        if (onitemlongclicklistener != null)
          onitemlongclicklistener.onlongclick(position);
        //返回false会在长安结束后继续点击
        return true;
      }
    });
  }

  @override
  public int getitemcount() {
    return mlist.size();
  }


  /**
   * 点击
   */
  public interface onitemclicklistener {
    void onclick(int position);
  }

  onitemclicklistener onitemclicklistener;


  public void setonitemclicklistener(onitemclicklistener onitemclicklistener) {
    this.onitemclicklistener = onitemclicklistener;
  }

  public interface onitemlongclicklistener {
    void onlongclick(int position);
  }

  onitemlongclicklistener onitemlongclicklistener;

  public void setonitemlongclicklistener(onitemlongclicklistener onitemlongclicklistener) {
    this.onitemlongclicklistener = onitemlongclicklistener;
  }
}

activity_main.xml类

<?xml version="1.0" encoding="utf-8"?>
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/activity_main"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.example.administrator.lesson10_recyclerview2.mainactivity">


  <android.support.v7.widget.recyclerview
    android:id="@+id/rv"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</relativelayout> 

 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!