如何为每个项添加clicklistener

时间:2016-11-27 10:38:02

标签: android android-recyclerview onitemclicklistener

我这里有一个数组列表,

我希望每个人在点击时,他们会做一个特定的功能或意图活动。

目前:每个项目在列表中包含ImageButton和Textview。

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetDialogFragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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


public class MyBottomSheetDialogFragment extends BottomSheetDialogFragment {

    String mString;
    private RecyclerView mRecyclerView;
    private VerticalListAdapter mVerticalListAdapter;

    static MyBottomSheetDialogFragment newInstance(String string) {
        MyBottomSheetDialogFragment f = new MyBottomSheetDialogFragment();
        Bundle args = new Bundle();
        args.putString("string", string);
        f.setArguments(args);
        return f;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mString = getArguments().getString("string");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.bottom_sheet_modal_list, container, false);
        mRecyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);
        return v;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        List<Model> list = new ArrayList<Model>();
        list.add(new Model(R.drawable.a1, "Call"));
        list.add(new Model(R.drawable.a2, "Chat"));
        list.add(new Model(R.drawable.a3, "Android"));
        list.add(new Model(R.drawable.a4, "Location"));
        list.add(new Model(R.drawable.a5, "Call"));
        list.add(new Model(R.drawable.a6, "Chat"));
        list.add(new Model(R.drawable.a7, "Android"));


        mVerticalListAdapter = new VerticalListAdapter(getContext(), list);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);

        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.setAdapter(mVerticalListAdapter);
    }

我可以提供与此代码相对关联的项目的其他XML,Java代码,请发表评论以澄清。

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

import java.util.List;


public class VerticalListAdapter extends RecyclerView.Adapter<VerticalListAdapter.MyViewHolder>{

    private Context mContext;
    private List<Model> mModelList;

    public VerticalListAdapter(Context context, List<Model> list) {
        this.mContext = context;
        this.mModelList = list;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.modal_list_item, parent, false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.txtView.setText(mModelList.get(position).getName());
        holder.imgView.setImageDrawable(mContext.getResources().getDrawable(mModelList.get(position).getImgId()));
    }

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

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView txtView;
        public ImageButton imgView;

        public MyViewHolder(View view) {
            super(view);
            txtView = (TextView) view.findViewById(R.id.txtView);
            imgView = (ImageButton) view.findViewById(R.id.imgView);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您需要做的是将OnClickListener实现到Holder类并在modal_list_item.xml中为您的LinearLayout添加id,这样您就可以点击整个布局而不仅仅是它的视图(如果我理解正确的话)并最终设置onClickListener。

执行此操作时,您需要实现onClick方法。如需灵感,请参阅以下代码:

public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    public TextView txtView;
    public ImageButton imgView;

    private LinearLayout linearLayout;

    public MyViewHolder(View view) {
        super(view);
        txtView = (TextView) view.findViewById(R.id.txtView);
        imgView = (ImageButton) view.findViewById(R.id.imgView);

        linearLayout = (LinearLayout) view.findViewById(R.id.linear_l);
        linearLayout.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Log.i("RecyclerView Item", String.valueOf(getLayoutPosition()));
        mModelList.remove(getLayoutPosition());
        notifyDataSetChanged();
    }
}