1. 程式人生 > >Android中Recyclerview使用7----條目中按鈕點選事件,在activity中呼叫(介面回撥)

Android中Recyclerview使用7----條目中按鈕點選事件,在activity中呼叫(介面回撥)

0引入Recyclerview的支援庫

   compile 'com.android.support:recyclerview-v7:23.4.0'

1效果圖:


2程式碼:
2.1MainActivity中:

<span style="font-size:18px;">public class MainActivity extends Activity {
    //
    private RecyclerView mRecyclerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//      初始化RecyclerView
        mRecyclerView = (RecyclerView) findViewById(R.id.id_recyclerview);
//      RecyclerView設定展示的的樣式(listView樣子,gridView樣子,瀑布流樣子)
//        listView縱向滑動樣子
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(linearLayoutManager);

//      獲取資料,向介面卡傳資料,繫結介面卡
        ArrayList<String> datas = initData();
        HomeAdapter honmeAdapter = new HomeAdapter(MainActivity.this,datas);
        mRecyclerView.setAdapter(honmeAdapter);
//      呼叫按鈕返回事件回撥的方法
        honmeAdapter.buttonSetOnclick(new HomeAdapter.ButtonInterface() {
            @Override
            public void onclick(View view, int position) {
                Toast.makeText(MainActivity.this, "點選條目上的按鈕"+position, Toast.LENGTH_SHORT).show();
            }
        });

    }
    /**
     * 編寫一套假資料
     */
    protected ArrayList<String> initData() {
        ArrayList<String> mDatas = new ArrayList<String>();
        for (int i = 0; i < 100; i++) {
            mDatas.add("我是條目" + i);
        }
        return  mDatas;
    }

}</span>
2.2佈局檔案:activity_main.xml
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

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

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

</LinearLayout></span>
2.3條目的佈局檔案:item_home.xml
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="#44ff0000"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
    <TextView
        android:id="@+id/id_num"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:gravity="center"
        android:layout_weight="1"
        />
    <Button
        android:id="@+id/id_button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="按鈕"
        android:layout_weight="1"
        />
</LinearLayout>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#000033"
        />


</LinearLayout></span>
3介面卡中HomeAdapter
<span style="font-size:18px;">class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder> {
    private Context context;
    private ArrayList<String> list;
    private ButtonInterface buttonInterface;

    public HomeAdapter(Context context, ArrayList<String> list) {
        this.context = context;
        this.list = list;
    }

    /**
     *按鈕點選事件需要的方法
     */
    public void buttonSetOnclick(ButtonInterface buttonInterface){
        this.buttonInterface=buttonInterface;
    }

    /**
     * 按鈕點選事件對應的介面
     */
    public interface ButtonInterface{
        public void onclick( View view,int position);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(
                context).inflate(R.layout.item_home, parent,
                false));
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        holder.tv.setText(list.get(position));
        holder.id_button.setText(list.get(position));
        holder.id_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(buttonInterface!=null) {
//                  介面例項化後的而物件,呼叫重寫後的方法
                    buttonInterface.onclick(v,position);
                }

            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    /**
     * ViewHolder的類,用於快取控制元件
     */
    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv;
        Button id_button;
        public MyViewHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.id_num);
            id_button = (Button) itemView.findViewById(R.id.id_button);

        }


    }
}</span>