1. 程式人生 > >Recycler點選事件_介面回撥

Recycler點選事件_介面回撥

//介面卡
package com.example.recyclerview_dianjishijian;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

/**
 * Created by Administrator on 2017/11/27.
 */

public class Myadaper extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    Context  context;
    List<String> list;
    private Myviewhodler myviewhodler;

    //介面回撥點選事件
    SetonClick setonClick;
    //介面回撥方法
    public interface SetonClick{
        void setOnclick(int position);
    }
    //set方法
    public void setSetonClick(SetonClick setonClick) {
        this.setonClick = setonClick;
    }


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

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context,R.layout.item,null);
        myviewhodler = new Myviewhodler(view);
        return myviewhodler;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
                myviewhodler.tv.setText("你猜");
        //點選事件
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            if (setonClick != null){
                setonClick.setOnclick(position);
            }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
        class  Myviewhodler extends RecyclerView.ViewHolder{

            private final TextView tv;

            public Myviewhodler(View itemView) {
                super(itemView);
                tv = itemView.findViewById(R.id.tvv);
            }
        }
}
MainActivity
package com.example.recyclerview_dianjishijian;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

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

public class MainActivity extends AppCompatActivity {
List<String> list = new ArrayList<>();
    private RecyclerView rv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rv = (RecyclerView) findViewById(R.id.tv);

        for (int i = 0; i <20 ; i++) {
            list.add("不知道"+i);
        }
        Myadaper myadaper = new Myadaper(this,list);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        rv.setLayoutManager(manager);
        rv.setAdapter(myadaper);
        //介面回撥點選介面卡控制元件
        myadaper.setSetonClick(new Myadaper.SetonClick() {
            @Override
            public void setOnclick(int position) {
                Toast.makeText(MainActivity.this, ""+position, Toast.LENGTH_SHORT).show();
            }
        });


    }
}