1. 程式人生 > >RecyclerView---點選條目---介面回撥

RecyclerView---點選條目---介面回撥

activity_main

<Button
    android:id="@+id/add"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="新增"
    android:onClick="onClick"/>

<Button
    android:id="@+id/del"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="刪除"
    android:onClick="onClick"/>

<Button
    android:id="@+id/list"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="List"
    android:onClick="onClick"/>

<Button
    android:id="@+id/grid"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Grid"
    android:onClick="onClick"/>

<Button
    android:id="@+id/flow"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Flow"
    android:onClick="onClick"/>

<android.support.v7.widget.RecyclerView
android:id="@+id/recy"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:scrollbars=“vertical” />

recy_item

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center">

        <ImageView
            android:id="@+id/recy_image"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@mipmap/ic_launcher"
            android:padding="10dp"/>

        <TextView
            android:id="@+id/recy_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="15dp"
            android:text="@string/app_name" />
    </LinearLayout>
</RelativeLayout>

MainActivity

package wanghuiqi.bawie.com.whq_recyclerview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button add, del, list, grid, flow;
    private RecyclerView recyclerView;
    private List<String> textList;
    private MyRecyAdapter myRecyAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控制元件
        initView();
        textList = new ArrayList<>();

        for (int i = 0; i < 100; i++) {
            textList.add("小餅乾er" + i);
        }
        myRecyAdapter = new MyRecyAdapter(textList, MainActivity.this);
        //佈局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this,
                LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(myRecyAdapter);

        myRecyAdapter.setRecyInterface(new MyRecyAdapter.OnClickRecyListener() {
            @Override
            public void onItemclick(View view, String i) {
                Toast.makeText(MainActivity.this, "點選了"+i, Toast.LENGTH_SHORT).show();
            }
        });

    }

    private void initView() {
        add = findViewById(R.id.add);
        del = findViewById(R.id.del);
        list = findViewById(R.id.list);
        grid = findViewById(R.id.grid);
        flow = findViewById(R.id.flow);
        recyclerView = findViewById(R.id.recy);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.add:
                myRecyAdapter.addData(0);
                break;
            case R.id.del:
                myRecyAdapter.delData(1);
                break;
            case R.id.list:
                listData();
                break;
            case R.id.grid:
                gridData();
                break;
            case R.id.flow:
                stoggData();
                break;
        }
    }

    private void stoggData() {
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(staggeredGridLayoutManager);

    }

    private void gridData() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(gridLayoutManager);
    }

    private void listData() {
        //佈局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);
    }
}

MyRecyAdapter

package wanghuiqi.bawie.com.whq_recyclerview;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

public class MyRecyAdapter extends RecyclerView.Adapter<MyRecyAdapter.Viewholder> {

    private List<String> list;
    private Context context;

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

    @NonNull
    @Override
    public Viewholder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = View.inflate(context, R.layout.recy_item, null);
        Viewholder holder = new Viewholder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final Viewholder viewholder, final int i) {
        viewholder.recyImage.setImageResource(R.mipmap.a);
        viewholder.recyText.setText(list.get(i));
        
	 viewholder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnclickRecy.onItemclick(v,list.get(i));
                }
            });
    }

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

    //點選按鈕
    public void addData(int i) {
        list.add(i, "新增的資料");
        notifyItemInserted(i);
        notifyDataSetChanged();
    }

    public void delData(int i) {
        list.remove(i);
        notifyItemRemoved(i);
    }

    public class Viewholder extends RecyclerView.ViewHolder {

        private final ImageView recyImage;
        private final TextView recyText;

        public Viewholder(@NonNull View itemView) {
            super(itemView);
            recyImage = itemView.findViewById(R.id.recy_image);
            recyText = itemView.findViewById(R.id.recy_text);
        }
    }

    //建立介面
    public interface OnClickRecyListener{
        void onItemclick(View view, String i);
    }
    //宣告介面變數
    private OnClickRecyListener mOnclickRecy;
    //暴露方法,外界使用
    public void setRecyInterface(OnClickRecyListener recyInterface){
        mOnclickRecy=recyInterface;
    }
}

依賴

implementation 'com.android.support:recyclerview-v7:28.0.0'