其他分享
首页 > 其他分享> > 将值从recycleler adapter传递到android中的fragment

将值从recycleler adapter传递到android中的fragment

作者:互联网

我有一个回收器适配器,我已经为适配器中的项添加了setOnClickListener.代码如下:

@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();
                }
            }
        });  

这里有片段代码,用于处理从适配器到片段的值.

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();
            }
        });

    }  

我正试图传递pos和值,从适配器到片段有pos.但我的代码不起作用.我该如何解决?

解决方法:

make click将click事件处理到recyclerview中..
用于处理点击事件的代码.

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);
        }
    });
 }

之后将适配器绑定到RecyclerView调用下面的代码..

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();
    }
});

标签:android-adapter,android,android-fragments
来源: https://codeday.me/bug/20191003/1847039.html