1. 程式人生 > >RecyclerView條目點選和長按

RecyclerView條目點選和長按

   RecyclerView雖然很強大,但是沒有點選事件,美中不足!所以需要自己寫點選事件啦。

   Activity介面

public class MainActivity extends AppCompatActivity {

    private RecyclerView rv;
    private SwipeRefreshLayout sr;
    private List<String> data;
    private MyRecyclerViewAdapter adapter;

    @Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView();//初始化view initData();//填充資料 //條目點選事件,呼叫介面卡寫的條目點選的方法 adapter.setOnItemClickListener(new OnItemClickListener() { @Override
public void setOnItemClickListener(View view, int position) { Toast.makeText(MainActivity.this,"這是條目"+position,Toast.LENGTH_SHORT).show(); } }); //條目長按事件,呼叫介面卡寫的條目長按的方法 adapter.setOnLongClickListener(new OnLongClickListener() { @Override
public void setOnLongClickListener(View view, int position) { //長按刪除 data.remove(position); //重新整理介面卡 adapter.notifyDataSetChanged(); } }); sr.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { initData(); sr.setRefreshing(false); } }); } private void initData() { data = new ArrayList<>(); for (int i = 0; i <20; i++) { data.add("這是條目"+i); } adapter = new MyRecyclerViewAdapter(MainActivity.this, data); rv.setAdapter(adapter);//設定介面卡 } private void initView() { rv = (RecyclerView) findViewById(R.id.rv); sr = (SwipeRefreshLayout) findViewById(R.id.sr); LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);//線性樣式顯示,和ListView顯示的佈局格式一致 GridLayoutManager gridLayoutManager=new GridLayoutManager(this,3);//九宮格樣式顯示,和GridView顯示的佈局格式一致 StaggeredGridLayoutManager staggeredGridLayoutManager=new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);//瀑布流樣式顯示 rv.setLayoutManager(linearLayoutManager);//LinearLayoutManager、gridLayoutManager、staggeredGridLayoutManager三者都可使用 } } activity_main佈局檔案:
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
   >
    <android.support.v4.widget.SwipeRefreshLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/sr">
        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/rv">
        </android.support.v7.widget.RecyclerView>
    </android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
介面卡佈局檔案
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:padding="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv"/>
</LinearLayout>

 
介面卡程式碼:
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.MyViewHolder> {
    private List<String> data=new ArrayList<>();
    private Context context;
    //定義兩個變數,接收條目點選和長按的物件
    private OnItemClickListener mOnItemClickListener;
    private OnLongClickListener mOnLongClickListener;
    public MyRecyclerViewAdapter(Context context,List<String> data){
        this.context=context;
        this.data=data;
    }
    //建立條目點選的方法,用變數接收一下介面物件
    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.mOnItemClickListener=onItemClickListener;

    }
    //建立條目長按的方法,用變數接收一下介面物件
    public void setOnLongClickListener(OnLongClickListener onLongClickListener){
        this.mOnLongClickListener=onLongClickListener;
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //建立自己的ViewHolder
        View view = LayoutInflater.from(context).inflate(R.layout.recycletview, null);
        MyViewHolder myViewHolder=new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        holder.tv.setText(data.get(position));
        //holder的檢視點選監聽
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

               mOnItemClickListener.setOnItemClickListener(holder.tv,position);//控制元件和條目下標
            }
        });
        //holder的條目檢視長按監聽
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                mOnLongClickListener.setOnLongClickListener(holder.tv,position);
                return false;
            }
        });
    }

    @Override
    public int getItemCount() {
        return data!=null?data.size():0;
    }
    //建立ViewHolder
    class MyViewHolder extends RecyclerView.ViewHolder{

        private  TextView tv;

        public MyViewHolder(View itemView) {
            super(itemView);
            //使用itemView找到控制元件
            tv = (TextView) itemView.findViewById(R.id.tv);
        }
    }
}
兩個介面:
public interface OnItemClickListener {
    void setOnItemClickListener(View view,int position);
}
public interface OnLongClickListener {
    void setOnLongClickListener(View view, int position);
}
 程式碼就這麼多,很好理解!