1. 程式人生 > >Android中Recyclerview使用6----新增條目得到點選事件和長按事件(另一種寫法,較簡單)

Android中Recyclerview使用6----新增條目得到點選事件和長按事件(另一種寫法,較簡單)

效果圖


activity中:
MainActivity
<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.setOnItemListener(new HomeAdapter.OnItemListener() {
            @Override
            public void onClick(View v, int position, String str) {
                Toast.makeText(MainActivity.this, "條目被點選了"+position, Toast.LENGTH_SHORT).show();
            }
        });
//      條目長按事件被呼叫
        honmeAdapter.setOnLongItemListener(new HomeAdapter.OnLongItemListener() {
            @Override
            public void onClick(View v, int position, String str) {
                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>

介面卡中:

HomeAdapter
class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder> {
    private Context context;
    private ArrayList<String> list;
    private OnItemListener listener;
    private OnLongItemListener onLongItemListener;

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

    }
//  點選事件
    public void setOnItemListener(OnItemListener onItemListener) {
        this.listener = onItemListener;
    }
    public interface OnItemListener {
        void onClick(View v, int position, String str);
    }
//  長按事件
    public void setOnLongItemListener(OnLongItemListener onLongItemListener) {
        this.onLongItemListener = onLongItemListener;
    }
    public interface OnLongItemListener {
        void onClick(View v, int position, String str);
    }

    @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, int position) {
        holder.tv.setText(list.get(position));
    }

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

    /**
     * ViewHolder的類,用於快取控制元件
     */
    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView tv;

        public MyViewHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.id_num);
//          條目點選事件,給引數賦值
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onClick(v, getLayoutPosition(), list.get(getLayoutPosition()));
                    }
                }
            });
//          條目長按事件,給引數賦值
            itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    if (onLongItemListener != null) {
                        onLongItemListener.onClick(v, getLayoutPosition(), list.get(getLayoutPosition()));
                    }
                    return false;
                }
            });
        }


    }
}
原始碼下載: