如何将点击功能添加到回收站视图?

时间:2019-03-12 08:22:11

标签: android android-intent android-recyclerview

这是我在Myviewholder中添加onclicklistener的适配器。我想在即时贴视图上执行onclick功能,并获取被点击即时贴的位置,然后将即时贴的值发送到其他使用意图的活动中。请在下面我给我的适配器和片段提供帮助。

private LayoutInflater inflater;
private ArrayList<Books> dataModelArrayList;
private OnItemClickListener onItemClickListener;

private Context ctx;

String url = "https://test1.xz.com/pub/media/wysiwyg/";

public MyRecyclerAdapter(Context ctx, ArrayList<Books> dataModelArrayList){
    inflater = LayoutInflater.from(ctx);
    this.dataModelArrayList = dataModelArrayList;
}

@Override
public MyRecyclerAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = inflater.inflate(R.layout.singleitem_recyclerview, parent, false);
    MyViewHolder holder = new MyViewHolder(view);
    return holder;
}

@Override
public void onBindViewHolder(MyRecyclerAdapter.MyViewHolder holder, int position) {
    if (dataModelArrayList.get(position).getImage().isEmpty()) {
        Picasso.get().load(R.drawable.placeholder)
                .placeholder(R.drawable.placeholder)
                .error(R.drawable.placeholder)
                .into(holder.iv);
    }else{Picasso.get().load(url + dataModelArrayList.get(position).getImage()).into(holder.iv);}
    holder.name.setText(dataModelArrayList.get(position).getBookFullName());
}

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

class MyViewHolder extends RecyclerView.ViewHolder implements
        View.OnClickListener{

    TextView name;
    ImageView iv;

    public MyViewHolder(View itemView) {
        super(itemView);

        name = (TextView) itemView.findViewById(R.id.name);
        iv = (ImageView) itemView.findViewById(R.id.iv);
    }

    @Override
    public void onClick(View v) {

        onItemClickListener.onItemClick(dataModelArrayList);
    }
}

public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
    this.onItemClickListener = onItemClickListener;
}
public interface OnItemClickListener {
    void onItemClick(ArrayList<Books> Books);
}

下面给出的代码是我的oncreateview片段,在其中我正在setupRecycler()方法中使用上面给出的适配器,但我正在设置适配器,但无法获得点击功能。

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    view = inflater.inflate(R.layout.fragment_home, container, false);
    recyclerView = view.findViewById(R.id.recycler);
    fetchingJSON();

    return view;
}

private void fetchingJSON() {

    showSimpleProgressDialog(getActivity(), "Loading...","Loading",false);

    StringRequest stringRequest = new StringRequest(Request.Method.GET, URLs.URL_BOOKS,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Log.d("response", ">>" + response);

                    try {
                        removeSimpleProgressDialog();

                        JSONObject obj = new JSONObject(response);
                        if(obj.optString("status_code").equals("200")){

                            dataModelArrayList = new ArrayList<>();
                            JSONArray dataArray  = obj.getJSONArray("data");

                            for (int i = 0; i < dataArray.length(); i++) {
                                Books playerModel = new Books();
                                JSONObject dataobj = dataArray.getJSONObject(i);

                                playerModel.setImage(dataobj.getString("Image"));
                                playerModel.setBookFullName(dataobj.getString("BookFullName"));

                                dataModelArrayList.add(playerModel);
                            }
                            setupRecycler();
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    //displaying the error in toast if occurrs
                    Toast.makeText(getActivity(), error.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });

    // request queue
    RequestQueue requestQueue = Volley.newRequestQueue(getActivity());

    requestQueue.add(stringRequest);
}

private void setupRecycler(){

    rvAdapter= new MyRecyclerAdapter(getActivity().getApplicationContext(), dataModelArrayList);
    recyclerView.setAdapter(rvAdapter);
    recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), getResources().getInteger(R.integer.number_of_grid_items)));
    rvAdapter.setOnItemClickListener(new MyRecyclerAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(ArrayList<Books> Books) {
            //Toast.makeText(getActivity(), "lakshman", Toast.LENGTH_LONG).show();
            Toast.makeText(getActivity(), Books.getBookFullName(), Toast.LENGTH_LONG).show();
        }

        /*@Override
        public void onItemClick(Books Books) {
            Toast.makeText(getActivity(), Books.getBookFullName(), Toast.LENGTH_LONG).show();
        }*/
    });
}

如何获取被点击的卡的位置并将特定的值传递给其他活动。

请帮助我解决这个问题。

2 个答案:

答案 0 :(得分:1)

您忘记在 OnClickListener

中抵抗MyViewHolder
name.setOnClickListener(this);
iv.setOnClickListener(this);

您需要像这样在{strong> OnClickListener 中设置MyViewHolder()

示例代码

 public MyViewHolder(View itemView) {
        super(itemView);

        name = (TextView) itemView.findViewById(R.id.name);
        iv = (ImageView) itemView.findViewById(R.id.iv);
        name.setOnClickListener(this);
        iv.setOnClickListener(this);

    }

答案 1 :(得分:0)

onBindViewHolder中放入下面的方法。

holder.itemView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (viewItemInterface != null) {
            viewItemInterface.onItemClick(holder.getAdapterPosition(), bgImage.getCompressedImg());
        }
    }
});