從源碼角度入手實現RecyclerView的Item點擊事件

 轉載請註明出處:http://www.cnblogs.com/cnwutianhao/p/6758373.html html

 

 RecyclerView 做爲 ListView 和 GridView 的替代產物,相信在Android界已廣爲流傳。android

 RecyclerView 本是不會有相似 ListView 的那種點擊事件,可是知道和會用又是兩種狀況,跟隨我一塊兒從源碼角度分析,RecyclerView 點擊事件。app

 首先看一下 Google 對 ListView 家族譜的介紹:ide

 能夠看出 ListView 歸根結底就是一個 View 。佈局

 再來看一下 Google 對 OnItemClickListener 接口作的描述:this

 源代碼總結的很全面、詳細。spa

 下面看一下 ListView 調用 onItemClick 點擊事件。.net

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // TODO
    }
});

 其實否則,RecyclerView 也能夠使用 onItemClick 點擊事件。請往下看:code

 示例圖片(Material Design + Butter Knife + Snackbar):xml

 

 實現 RecyclerView 點擊事件

 1.導入必要的庫

 Material Design 時代的Android,少不了各種庫的支持

 UI類庫

compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:cardview-v7:25.3.1'
compile 'com.android.support:recyclerview-v7:25.3.1'
compile 'com.android.support:design:25.3.1'

 Butter Knife (都啥時代了,快放棄 findViewById 吧)

compile 'com.jakewharton:butterknife:8.5.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'

 

 2.佈局文件(分爲 主界面 和 Item佈局界面)

 主界面(RecyclerView)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:overScrollMode="never"
        android:scrollbars="none" />

</RelativeLayout>

 Item佈局界面(CardView)

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:layout_margin="10dp"
    android:background="@drawable/recycler_item_selector"
    app:cardBackgroundColor="@color/colorPrimary"
    app:cardCornerRadius="10dp"
    app:cardElevation="10dp">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="10dp"
        android:text="item"
        android:textColor="@android:color/white"
        android:textSize="24sp" />

</android.support.v7.widget.CardView>

 

 3.自定義 Adapter 類

public class DemoAdapter extends RecyclerView.Adapter<DemoAdapter.ViewHolder> {

    private onRecyclerViewItemClick mOnRvItemClick;

    private Context mContext;

    String[] strings;

    public DemoAdapter(Context ctx, String[] strings, onRecyclerViewItemClick onRvItemClick) {
        mContext = ctx;
        this.mOnRvItemClick = onRvItemClick;
        this.strings = strings;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.activity_main_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.setData(position);
    }

    @Override
    public int getItemCount() {
        return 20;
    }


    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        @BindView(R.id.textView)
        TextView textView;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            itemView.setOnClickListener(this);
        }

        public void setData(int position) {
            textView.setText("第" + position + "行");
        }

        @Override
        public void onClick(View view) {
            if (mOnRvItemClick != null)
                mOnRvItemClick.onItemClick(view, getAdapterPosition());
        }
    }

    /**
     * item點擊接口
     */
    public interface onRecyclerViewItemClick {
        void onItemClick(View v, int position);
    }

}

 

4.主類(實現 onItemClick 方法 + Butter Knife + Snackbar)

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        DemoAdapter myAdapter = new DemoAdapter(this, null, new DemoAdapter.onRecyclerViewItemClick() {
            @Override
            public void onItemClick(View v, int position) {
                Snackbar.make(v, "第" + position + "行", Snackbar.LENGTH_SHORT).show();
            }
        });
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mRecyclerView.setAdapter(myAdapter);
    }

}

 示例Demo下載地址:RecyclerView的Item點擊事件

 

關注個人新浪微博,請認準黃V認證,獲取最新安卓開發資訊。
關注科技評論家,領略科技、創新、教育以及最大化人類智慧與想象力!

相關文章
相關標籤/搜索