Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
747 views
in Technique[技术] by (71.8m points)

passing values from recycler adapter to fragment in android

I've a recycler adapter where I've added setOnClickListener for items in adapter. Code is as given below:

@Override
    public void onBindViewHolder(final FiltersAdapter.MyViewHolder holder, final int position) {
        holder.mOrganizer.setText(filtersList.get(position));
        holder.mLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try {
                    //Log.e("message","ONclick FIlter");
                    if (holder.mOrganizerCheck.isChecked()){
                        holder.mOrganizerCheck.setChecked(false);
                    }else {
                        holder.mOrganizerCheck.setChecked(true);
                        int pos = holder.getAdapterPosition();

                        filtersList.get(pos);

                        /*Bundle bundle = new Bundle();
                        bundle.putInt(pos, );*/

                        Log.d(TAG, "onClick: " +filtersList);
                        Log.d(TAG, "onClick of position: " +pos);

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });  

And here is fragment code which deals with getting values from adapter to fragment.

private void initView() {
        mrvFilterBySender = (RecyclerView) mFilterView.findViewById(R.id.rvFilterBySender);
        mFiltersAdapter = new FiltersAdapter(getActivity(), mListOrganizer);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getContext());
        mrvFilterBySender.setLayoutManager(mLayoutManager);

        mrvFilterBySender.setAdapter(mFiltersAdapter);
        mFiltersAdapter.notifyDataSetChanged();

        mrvFilterBySender.setAdapter(mFiltersAdapter);
        mrvFilterBySender.setRecyclerListener(new RecyclerView.RecyclerListener() {
            @Override
            public void onViewRecycled(RecyclerView.ViewHolder holder) {
                holder.getAdapterPosition();
            }
        });

    }  

I'm trying to pass pos and value having that pos from adapter to fragment. But my code isn't working. How can I fix it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

make interface to handle click event into recyclerview .. used below code for handling click event.

onItemClickListner onItemClickListner;

public void setOnItemClickListner(RecyclerViewAdpater.onItemClickListner onItemClickListner) {
    this.onItemClickListner = onItemClickListner;
}

public interface onItemClickListner{
    void onClick(String str);//pass your object types.
}
   @Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
    String data=mStringList.get(position); // if you pass object of class then create that class object.
    holder.textView.setText(data);
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            onItemClickListner.onClick(data);
        }
    });
 }

then after when you bind adapter into RecyclerView call below code..

mRvData.setLayoutManager(new LinearLayoutManager(this));
mRvData.setAdapter(recyclerViewAdpater);
recyclerViewAdpater.notifyDataSetChanged();
recyclerViewAdpater.setOnItemClickListner(new RecyclerViewAdpater.onItemClickListner() {
    @Override
    public void onClick(String str) {
        Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG).show();
    }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...