{"id":1191,"date":"2016-02-04T20:40:34","date_gmt":"2016-02-04T12:40:34","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=1191"},"modified":"2026-08-18T12:58:47","modified_gmt":"2026-08-18T04:58:47","slug":"1191","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=1191","title":{"rendered":"RecyclerView\u7684item\u70b9\u51fb\u4e8b\u4ef6"},"content":{"rendered":"<section id=\"wenyan\" style=\"font-family: system-ui, 'Apple Color Emoji', 'Segoe UI', 'Segoe UI Symbol', 'Noto Sans', Roboto, sans-serif; line-height: 1.75; font-size: 16px; color: #000000; caret-color: #000000;\" data-provider=\"WenYan\">\n<blockquote style=\"margin: 20px 0px; display: block; font-size: 0.9em; overflow: auto; border-left: 3px solid #ef7060; color: #6a737d; padding: 10px 10px 10px 20px; background: #fff9f9;\">\n<p style=\"margin: 1em 0px;\">RecyclerView \u5e03\u5c40\u7075\u6d3b\uff0c\u5374\u6ca1\u6709 ListView \u90a3\u6837\u7684 setOnItemClickListener\u3002\u4e0b\u6587\u7528\u89c2\u5bdf\u8005\u6a21\u5f0f\u7ed9 item \u8865\u4e0a\u70b9\u51fb\u56de\u8c03\uff0c\u8c03\u7528\u65b9\u53ea\u9700\u8981\u7c7b\u4f3c setOnItemClickListener \u7684\u63a5\u53e3\uff0c\u5e76\u7ed9\u51fa\u5b8c\u6574 Adapter\u3001Activity \u548c\u5e03\u5c40\u4ee3\u7801\u3002<\/p>\n<\/blockquote>\n<p><!--more--><\/p>\n<p style=\"margin: 1em 0px;\">RecyclerView\u4fa7\u91cd\u7684\u662f\u5e03\u5c40\u7684\u7075\u6d3b\u6027\uff0c\u867d\u8bf4\u53ef\u4ee5\u66ff\u4ee3ListView\u4f46\u662f\u8fde\u57fa\u672c\u7684\u70b9\u51fb\u4e8b\u4ef6\u90fd\u6ca1\u6709\uff0c\u8fd9\u7bc7\u6587\u7ae0\u5c31\u6765\u8be6\u7ec6\u8bb2\u89e3\u5982\u4f55\u4e3aRecyclerView\u7684item\u6dfb\u52a0\u70b9\u51fb\u4e8b\u4ef6\uff0c\u987a\u4fbf\u590d\u4e60\u4e00\u4e0b\u89c2\u5bdf\u8005\u6a21\u5f0f\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u5176\u5b9e\u5728 <a href=\"http:\/\/jcodecraeer.com\/a\/anzhuokaifa\/androidkaifa\/2014\/1118\/2004.html\" target=\"_self\">\u8fd9\u7bc7\u6587\u7ae0<\/a>\u00a0 \u4e2d\u5df2\u7ecf\u63d0\u5230\u5982\u4f55\u5b9e\u73b0\uff0c\u4f46\u662f\u91cc\u9762\u6709\u5f88\u591a\u4e0d\u89c4\u8303\u7684\u5730\u65b9\uff0c\u800c\u4e14\u6ca1\u6709\u5b8c\u6574\u7684\u4ee3\u7801\u3002<\/p>\n<hr>\n<h2 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid #ef7060; display: flex;\"><span style=\"display: inline-block; font-weight: bold; background: #ef7060; color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;\">\u6700\u7ec8\u76ee\u7684<\/span><\/h2>\n<p style=\"margin: 1em 0px;\">\u6a21\u62dfListView\u7684setOnItemClickListener()\u65b9\u6cd5\uff0c\u8c03\u7528\u8005\u53ea\u987b\u8c03\u7528\u7c7b\u4f3c\u4e8esetOnItemClickListener\u7684\u4e1c\u897f\u5c31\u80fd\u83b7\u5f97\u88ab\u70b9\u51fbitem\u7684\u76f8\u5173\u6570\u636e\u3002<\/p>\n<hr>\n<h2 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid #ef7060; display: flex;\"><span style=\"display: inline-block; font-weight: bold; background: #ef7060; color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;\">\u539f\u7406<\/span><\/h2>\n<p style=\"margin: 1em 0px;\">\u4e3aRecyclerView\u7684\u6bcf\u4e2a\u5b50item\u8bbe\u7f6esetOnClickListener\uff0c\u7136\u540e\u5728onClick\u4e2d\u518d\u8c03\u7528\u4e00\u6b21\u5bf9\u5916\u5c01\u88c5\u7684\u63a5\u53e3\uff0c\u5c06\u8fd9\u4e2a\u4e8b\u4ef6\u4f20\u9012\u7ed9\u5916\u9762\u7684\u8c03\u7528\u8005\u3002\u800c\u201c\u4e3aRecyclerView\u7684\u6bcf\u4e2a\u5b50item\u8bbe\u7f6esetOnClickListener\u201d\u5728Adapter\u4e2d\u8bbe\u7f6e\u3002\u5176\u5b9e\u76f4\u63a5\u5728onClick\u4e2d\u4e5f\u80fd\u5b8c\u5168\u5904\u7406item\u7684\u70b9\u51fb\u4e8b\u4ef6\uff0c\u4f46\u662f\u8fd9\u6837\u4f1a\u7834\u574f\u4ee3\u7801\u7684\u903b\u8f91\u3002<\/p>\n<hr>\n<h2 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid #ef7060; display: flex;\"><span style=\"display: inline-block; font-weight: bold; background: #ef7060; color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;\">\u6b65\u9aa4<\/span><\/h2>\n<h3 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em;\">adapter\u4e2d<\/h3>\n<p style=\"margin: 1em 0px;\">\u81ea\u5b9a\u4e49\u4e00\u4e2a\u7ee7\u627f\u81eaRecyclerView.Adapter\u7684MyAdapter\u3002<\/p>\n<p style=\"margin: 1em 0px;\">1.\u5728MyAdapter\u4e2d\u5b9a\u4e49\u5982\u4e0b\u63a5\u53e3,\u6a21\u62dfListView\u7684OnItemClickListener\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">    \/\/define interface\n    public static interface OnRecyclerViewItemClickListener {\n        void onItemClick(View view , String data);\n    }<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u58f0\u660e\u4e00\u4e2a\u8fd9\u4e2a\u63a5\u53e3\u7684\u53d8\u91cf<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">    private OnRecyclerViewItemClickListener mOnItemClickListener = null;<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u5728onCreateViewHolder()\u4e2d\u4e3a\u6bcf\u4e2aitem\u6dfb\u52a0\u70b9\u51fb\u4e8b\u4ef6<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">    @Override\n    public ViewHolder onCreateViewHolder(ViewGroup viewGroup,  int viewType) {\n        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);\n        ViewHolder vh = new ViewHolder(view);\n        \/\/\u5c06\u521b\u5efa\u7684View\u6ce8\u518c\u70b9\u51fb\u4e8b\u4ef6\n        view.setOnClickListener(this);\n        return vh;\n    }<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u5c06\u70b9\u51fb\u4e8b\u4ef6\u8f6c\u79fb\u7ed9\u5916\u9762\u7684\u8c03\u7528\u8005\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">    @Override\n    public void onClick(View v) {\n        if (mOnItemClickListener != null) {\n            \/\/\u6ce8\u610f\u8fd9\u91cc\u4f7f\u7528getTag\u65b9\u6cd5\u83b7\u53d6\u6570\u636e\n            mOnItemClickListener.onItemClick(v,(String)v.getTag());\n        }\n    }<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u6ce8\u610f\u4e0a\u9762\u8c03\u7528\u63a5\u53e3\u7684onItemClick()\u4e2d\u7684v.getTag()\u65b9\u6cd5\uff0c\u8fd9\u9700\u8981\u5728onBindViewHolder()\u65b9\u6cd5\u4e2d\u8bbe\u7f6e\u548citem\u76f8\u5173\u7684\u6570\u636e<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">    @Override\n    public void onBindViewHolder(ViewHolder viewHolder,  int position) {\n        viewHolder.mTextView.setText(datas[position]);\n        \/\/\u5c06\u6570\u636e\u4fdd\u5b58\u5728itemView\u7684Tag\u4e2d\uff0c\u4ee5\u4fbf\u70b9\u51fb\u65f6\u8fdb\u884c\u83b7\u53d6\n        viewHolder.itemView.setTag(datas[position]);\n    }<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u6700\u540e\u66b4\u9732\u7ed9\u5916\u9762\u7684\u8c03\u7528\u8005\uff0c\u5b9a\u4e49\u4e00\u4e2a\u8bbe\u7f6eListener\u7684\u65b9\u6cd5\uff08\uff09\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {\n        this.mOnItemClickListener = listener;\n    }<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u4ee5\u4e0a\u6240\u6709\u6b65\u9aa4\u90fd\u53d1\u751f\u5728\u81ea\u5b9a\u4e49\u7684adapter\u4e2d\uff0c\u5178\u578b\u7684\u89c2\u5bdf\u8005\u6a21\u5f0f\uff0c\u6709\u70b9\u7ed5\u7684\u5730\u65b9\u5728\u4e8e\uff0c\u8fd9\u91cc\u6d89\u53ca\u5230\u4e24\u4e2a\u89c2\u5bdf\u8005\u6a21\u5f0f\u7684\u4f7f\u7528\uff0cview\u7684setOnClickListener\u672c\u6765\u5c31\u662f\u89c2\u5bdf\u8005\u6a21\u5f0f\uff0c\u6211\u4eec\u5c06\u8fd9\u4e2a\u89c2\u5bdf\u8005\u6a21\u5f0f\u7684\u4e8b\u4ef6\u76d1\u542c\u4f20\u9012\u7ed9\u4e86\u6211\u4eec\u81ea\u5df1\u7684\u89c2\u5bdf\u8005\u6a21\u5f0f\u3002<\/p>\n<h3 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em;\">\u5728Activity\u4e2d\u4f7f\u7528<\/h3>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">        mRecyclerView = (RecyclerView)findViewById(R.id.my_recycler_view);\n        \/\/\u521b\u5efa\u9ed8\u8ba4\u7684\u7ebf\u6027LayoutManager\n        mLayoutManager = new LinearLayoutManager(this);\n        mRecyclerView.setLayoutManager(mLayoutManager);\n        \/\/\u5982\u679c\u53ef\u4ee5\u786e\u5b9a\u6bcf\u4e2aitem\u7684\u9ad8\u5ea6\u662f\u56fa\u5b9a\u7684\uff0c\u8bbe\u7f6e\u8fd9\u4e2a\u9009\u9879\u53ef\u4ee5\u63d0\u9ad8\u6027\u80fd\n        mRecyclerView.setHasFixedSize(true);\n        \/\/\u521b\u5efa\u5e76\u8bbe\u7f6eAdapter\n        mAdapter = new MyAdapter(data);\n        mRecyclerView.setAdapter(mAdapter);\n        mAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener(){\n            @Override\n            public void onItemClick(View view , String data){\n                Toast.makeText(MainActivity.this, data, 600).show();\n            }\n        });<\/code><\/pre>\n<hr>\n<h2 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid #ef7060; display: flex;\"><span style=\"display: inline-block; font-weight: bold; background: #ef7060; color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;\">\u5b8c\u6574\u4ee3\u7801<\/span><\/h2>\n<p style=\"margin: 1em 0px;\">MyAdapter.java<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">package com.example.recyclerviewdemo;\n\nimport android.support.v7.widget.RecyclerView;\nimport android.util.Log;\nimport android.view.LayoutInflater;\nimport android.view.View;\nimport android.view.ViewGroup;\nimport android.widget.TextView;\n\npublic class MyAdapter extends RecyclerView.Adapter&lt;MyAdapter.ViewHolder&gt; implements View.OnClickListener{\n    private  String[]  datas;\n    public MyAdapter(String[] datas) {\n        this.datas = datas;\n    }\n    private OnRecyclerViewItemClickListener mOnItemClickListener = null;\n\n    \/\/define interface\n    public static interface OnRecyclerViewItemClickListener {\n        void onItemClick(View view , String data);\n    }\n\n    @Override\n    public ViewHolder onCreateViewHolder(ViewGroup viewGroup,  int viewType) {\n        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);\n        ViewHolder vh = new ViewHolder(view);\n        \/\/\u5c06\u521b\u5efa\u7684View\u6ce8\u518c\u70b9\u51fb\u4e8b\u4ef6\n        view.setOnClickListener(this);\n        return vh;\n    }\n\n    @Override\n    public void onBindViewHolder(ViewHolder viewHolder,  int position) {\n        viewHolder.mTextView.setText(datas[position]);\n        \/\/\u5c06\u6570\u636e\u4fdd\u5b58\u5728itemView\u7684Tag\u4e2d\uff0c\u4ee5\u4fbf\u70b9\u51fb\u65f6\u8fdb\u884c\u83b7\u53d6\n        viewHolder.itemView.setTag(datas[position]);\n    }\n\n    @Override\n    public void onClick(View v) {\n        if (mOnItemClickListener != null) {\n            \/\/\u6ce8\u610f\u8fd9\u91cc\u4f7f\u7528getTag\u65b9\u6cd5\u83b7\u53d6\u6570\u636e\n            mOnItemClickListener.onItemClick(v,(String)v.getTag());\n        }\n    }\n\n    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {\n        this.mOnItemClickListener = listener;\n    }\n\n\n    \/\/\u83b7\u53d6\u6570\u636e\u7684\u6570\u91cf\n    @Override\n    public int getItemCount() {\n        return datas.length;\n    }\n    \/\/\u81ea\u5b9a\u4e49\u7684ViewHolder\uff0c\u6301\u6709\u6bcf\u4e2aItem\u7684\u7684\u6240\u6709\u754c\u9762\u5143\u7d20\n    public static class ViewHolder extends RecyclerView.ViewHolder {\n        public TextView mTextView;\n        public ViewHolder(View view){\n        super(view);\n            mTextView = (TextView) view.findViewById(R.id.text);\n        }\n    }\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">item.xml<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"50dip\"\n   &gt;\n    &lt;TextView\n        android:id=\"@+id\/text\"\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        \/&gt;\n&lt;\/RelativeLayout&gt;<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">MainActivity.java<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">package com.example.recyclerviewdemo;\n\nimport com.example.recyclerviewdemo.MyAdapter.OnRecyclerViewItemClickListener;\n\nimport android.support.v7.app.ActionBarActivity;\nimport android.support.v7.widget.LinearLayoutManager;\nimport android.support.v7.widget.RecyclerView;\nimport android.os.Bundle;\nimport android.view.Menu;\nimport android.view.MenuItem;\nimport android.view.View;\nimport android.widget.Toast;\n\npublic class MainActivity extends ActionBarActivity {\n    private RecyclerView mRecyclerView;\n    private LinearLayoutManager mLayoutManager;\n    private MyAdapter mAdapter;\n    private String[] data= new String[] {\"aa\",\"bb\", \"aa\",\"bb\", \"aa\",\"bb\", \"aa\",\"bb\", \"aa\",\"bb\",\"aa\",\"bb\", \"aa\",\"bb\", \"aa\",\"bb\", \"aa\",\"bb\", \"aa\",\"bb\"  };\n\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_main);\n        mRecyclerView = (RecyclerView)findViewById(R.id.my_recycler_view);\n        \/\/\u521b\u5efa\u9ed8\u8ba4\u7684\u7ebf\u6027LayoutManager\n        mLayoutManager = new LinearLayoutManager(this);\n        mRecyclerView.setLayoutManager(mLayoutManager);\n        \/\/\u5982\u679c\u53ef\u4ee5\u786e\u5b9a\u6bcf\u4e2aitem\u7684\u9ad8\u5ea6\u662f\u56fa\u5b9a\u7684\uff0c\u8bbe\u7f6e\u8fd9\u4e2a\u9009\u9879\u53ef\u4ee5\u63d0\u9ad8\u6027\u80fd\n        mRecyclerView.setHasFixedSize(true);\n        \/\/\u521b\u5efa\u5e76\u8bbe\u7f6eAdapter\n        mAdapter = new MyAdapter(data);\n        mRecyclerView.setAdapter(mAdapter);\n        mAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener(){\n            @Override\n            public void onItemClick(View view , String data){\n                Toast.makeText(MainActivity.this, data, 600).show();\n            }\n        });\n    }\n\n    @Override\n    public boolean onCreateOptionsMenu(Menu menu) {\n        \/\/ Inflate the menu; this adds items to the action bar if it is present.\n        getMenuInflater().inflate(R.menu.main, menu);\n        return true;\n    }\n\n    @Override\n    public boolean onOptionsItemSelected(MenuItem item) {\n        \/\/ Handle action bar item clicks here. The action bar will\n        \/\/ automatically handle clicks on the Home\/Up button, so long\n        \/\/ as you specify a parent activity in AndroidManifest.xml.\n        int id = item.getItemId();\n        if (id == R.id.action_settings) {\n            return true;\n        }\n        return super.onOptionsItemSelected(item);\n    }\n\n\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">activity_main.xml<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"\n    tools:context=\".MainActivity\"&gt;\n    &lt;android.support.v7.widget.RecyclerView\n        android:id=\"@+id\/my_recycler_view\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"match_parent\"\n        android:scrollbars=\"vertical\"\/&gt;\n&lt;\/RelativeLayout&gt;<\/code><\/pre>\n<hr>\n<h2 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid #ef7060; display: flex;\"><span style=\"display: inline-block; font-weight: bold; background: #ef7060; color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;\">\u603b\u7ed3<\/span><\/h2>\n<p style=\"margin: 1em 0px;\">\u5728ListView\u4e2d\u6211\u4eec\u662f\u8c03\u7528ListView\u7684setOnItemClickListener\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">        mListView.setOnItemClickListener(new OnItemClickListener() {\n            public void onItemClick(AdapterView&lt;?&gt; parent, View v, int position, long id) {\n\n                    ...\n\n            }\n        });<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u800c\u5728\u6211\u4eec\u8fd9\u91cc\u662f\u8c03\u7528mAdapter\u7684setOnItemClickListener\u3002\u4e14\u56de\u8c03\u65b9\u6cd5public void onItemClick()\u7684\u53c2\u6570\u4e5f\u4e0d\u4e00\u81f4\uff0cListView\u4e2d\u6709\u88ab\u70b9\u51fbitem\u7684position\u53c2\u6570\uff0c\u800c\u6211\u4eec\u8fd9\u91cc\u76f4\u63a5\u662f\u88ab\u70b9\u51fbitem\u7684\u76f8\u5173\u6570\u636e\uff08\u8fd9\u91cc\u53ea\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff09\u3002<\/p>\n<blockquote style=\"margin: 20px 0px; display: block; font-size: 0.9em; overflow: auto; border-left: 3px solid #ef7060; color: #6a737d; padding: 10px 10px 10px 20px; background: #fff9f9;\">\n<p style=\"margin: 1em 0px;\"><strong>\u4e00\u53e5\u8bdd\u603b\u7ed3\uff1a\u5728 Adapter \u91cc\u7ed9\u5b50 View \u8bbe OnClickListener\uff0c\u518d\u628a\u4e8b\u4ef6\u8f6c\u5230\u5bf9\u5916\u7684 OnRecyclerViewItemClickListener\uff0c\u5c31\u80fd\u5f97\u5230\u63a5\u8fd1 ListView \u7684 item \u70b9\u51fb\u3002<\/strong><\/p>\n<\/blockquote>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>RecyclerView \u5e03\u5c40\u7075\u6d3b\uff0c\u5374\u6ca1\u6709 ListView \u90a3\u6837\u7684 setOnItemClickListen [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[75],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1191"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1191"}],"version-history":[{"count":2,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1191\/revisions"}],"predecessor-version":[{"id":1199,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1191\/revisions\/1199"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}