在RecyclerView项目上应用SearchFilter和OnClick后,在下一个片段中获取RecyclerView项目的错误位置

时间:2018-04-09 11:09:53

标签: android android-fragments android-recyclerview android-cardview android-search

我在Fragment上设置了RecyclerView。我已经为RecyclerView Adapter中的RecyclerView项设置了OnClick侦听器。我使用SearchFilter获取Filtered项目。在点击RecyclerView项目后我需要获取项目位置和传递到下一个片段。但是我在过滤项目后得到错误的位置。这是设置Recyclerview适配器的代码

public class StatusAdapter extends RecyclerView.Adapter<StatusAdapter.ViewHolder> {
    private ArrayList<CurrentEntry> originalList,filterList;
    Context context;

    public void filter(String newText)
    {
        filterList.clear();
        if(TextUtils.isEmpty(newText))
        {
            filterList.addAll(originalList);
        }
        else
        {
            for(CurrentStatusEntry entry:originalList)
            {
                if(entry.getName().toLowerCase().contains(newText))
                    filterList.add(entry);
            }
            notifyDataSetChanged();
        }
        //    notifyDataSetChanged();
    }
    public StatusAdapter(ArrayList<CurrentEntry> originalList,Context context)
    {
        this.originalList=originalList;
        this.context=context;
        this.filterList=new ArrayList<CurrentEntry>();
        this.filterList.addAll(this.originalList);
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.current_status_card,parent,false);
        return new ViewHolder(v);
    }


     @Override
        public void onBindViewHolder(final ViewHolder holder, int position) {
            final  CurrentEntry current=filterList.get(position);
            int temppos=holder.getAdapterPosition();
            int finalPos=temppos+1;
            holder.Name.setText(current.getName());
            holder.People.setText(current.getNo());
            holder.Estimate.setText(current.getEsti());
            holder.bookTime.setText(current.getTime());
            holder.myNumber.setText(String.valueOf(finalPos));
            holder.lapsedTime.setText(current.getLapsedTime());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position=holder.getAdapterPosition();
            Bundle bundle=new Bundle();
            bundle.putInt("position",position);
            StatusInfo satusInfo=new StatusInfo();
            satusInfo.setArguments(bundle);
            AppCompatActivity activity=(AppCompatActivity)v.getContext();
            activity.getSupportFragmentManager().beginTransaction().add(R.id.frame,satusInfo).addToBackStack("info").commit();
                }
            });
        } 

@Override
public int getItemCount() {
    return (null!=filterList ? filterList.size():0);
}  

这是我的ViewHolder的代码

public class ViewHolder extends RecyclerView.ViewHolder {
      private   TextView Name ;
     private    TextView People;
   private      TextView Estimate;
   private      TextView myNumber;
    private     TextView bookTime;
    private     TextView lapsedTime;
        private  ViewHolder(View itemView) {
            super(itemView);
            Name=(TextView)itemView.findViewById(R.id.name);
            People=(TextView)itemView.findViewById(R.id.people);
            Estimate=(TextView)itemView.findViewById(R.id.estimate);
            myNumber=(TextView)itemView.findViewById(R.id.no);
            bookTime=(TextView)itemView.findViewById(R.id.bookTime);
            lapsedTime=(TextView)itemView.findViewById(R.id.lapsedTime);
        }

过滤后如何获得RecyclerView项目的正确位置?

1 个答案:

答案 0 :(得分:1)

使用对象索引从originalList查找位置,我已更新代码

holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int position=holder.getAdapterPosition();
                Bundle bundle=new Bundle();

               // change
                bundle.putInt("position",  originalList.indexOf(filterList.get(position)));

                StatusInfo satusInfo=new StatusInfo();
                satusInfo.setArguments(bundle);
                AppCompatActivity activity=(AppCompatActivity)v.getContext();
                activity.getSupportFragmentManager().beginTransaction().add(R.id.frame,satusInfo).addToBackStack("info").commit();
                    }
                });

传递位置无用尝试传递直接对象或ID