在RecyclerView中过滤时如何保持项目位置?

时间:2017-02-19 09:59:13

标签: android android-recyclerview

当SearchView过滤了RecyclerView中的项目时,是否可以在RecyclerView中维护项目的位置? 我不知道我做得对不对,但如果这是正确的做法,我不知道。 继承我的适配器代码

public class bird_recyclerAdapter extends RecyclerView.Adapter<bird_recyclerAdapter.birdViewHolder> {
    ArrayList<bird_counter> arrayList=new ArrayList<>();
    Context bird_context;
   public static int flag;
    public static int bird_id;
    bird_recyclerAdapter(ArrayList<bird_counter> arrayList, Context bird_context)
   {

        this.arrayList=arrayList;
        this.bird_context=bird_context;
    }
    public birdViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.petbook_bird,parent,false);
        return new birdViewHolder(view,bird_context,arrayList);
    }
    public void filter_bird(Context context, ArrayList<bird_counter> arrayList)
    {
        this.arrayList=arrayList;
        this.bird_context=context;
    }

    @Override
    public void onBindViewHolder(birdViewHolder holder, int position) {
        holder.bird.setImageResource(arrayList.get(position).getCount());
        holder.bird_name.setText(arrayList.get(position).getName());
    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }
    public static class birdViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        ImageButton bird;
        TextView bird_name;
        ArrayList<bird_counter> arrayList=new ArrayList<>();
        Context bird_context;
        public birdViewHolder(View itemView,Context bird_context,ArrayList<bird_counter> arrayList) {
            super(itemView);
            itemView.setOnClickListener(this);
            this.arrayList=arrayList;
            this.bird_context=bird_context;
            bird=(ImageButton) itemView.findViewById(R.id.petbird_imag);
            bird_name=(TextView)itemView.findViewById(R.id.petbird_name1);
            bird.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            int position=getAdapterPosition();
            if(position==0) {
                flag = 0;
            }
            else if(position==1) {
                flag = 1;
            }
            else if(position==2) {
                flag = 2;
            }
            else if(position==3) {
                flag = 3;
            }
            else if(position==4) {
                flag = 4;
            }
            else if(position==5) {
                flag = 5;
            }
            if(flag==0) {
                    bird_id=0;
                    Intent intent = new Intent(this.bird_context, bird_abyssinian.class);
                    this.bird_context.startActivity(intent);
                        }
            if(flag==1) {
                bird_id=1;
                    Intent intent = new Intent(this.bird_context, bird_african_ring.class);
                    this.bird_context.startActivity(intent);
                }
            if(flag==2) {
                bird_id=2;
                    Intent intent = new Intent(this.bird_context, bird_bare_eye.class);
                    this.bird_context.startActivity(intent);
            }

            if(flag==3) {
                bird_id=3;
                    Intent intent = new Intent(this.bird_context, bird_barraban.class);
                    this.bird_context.startActivity(intent);

            }
            if(flag==4) {
                bird_id=5;
                    Intent intent = new Intent(this.bird_context, bird_calico.class);
                    this.bird_context.startActivity(intent);
                }
            if(flag==5) {
                bird_id=5;
                    Intent intent = new Intent(this.bird_context, bird_cape_eye.class);
                    this.bird_context.startActivity(intent);
                }
        }
    }
    public void setFilter(ArrayList<bird_counter> newList){
        arrayList=new ArrayList<>();
        arrayList.addAll(newList);
        notifyDataSetChanged();
        this.arrayList=newList;

    }
}

我的片段活动代码

    public class bird_petbook extends Fragment implements SearchView.OnQueryTextListener{
    RecyclerView recyclerView;
    bird_recyclerAdapter bird_recyclerAdapter;
    RecyclerView.LayoutManager layoutManager;
    ArrayList<bird_counter> arrayList=new ArrayList<>();

    public bird_petbook() {
        // Required empty public constructor
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.fragment_bird_petbook, container, false);
        setHasOptionsMenu(true);
        return v;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView=(RecyclerView)view.findViewById(R.id.bird_recycle);
        layoutManager=new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        setHasOptionsMenu(true);
        arrayList=new ArrayList<>();
        arrayList.add(new bird_counter("Abyssinian Love Bird",R.drawable.abyssinian_love_bird));
        arrayList.add(new bird_counter("Affrican Ring-necked Parakeet",R.drawable.africanring_necked_parakeet));
        arrayList.add(new bird_counter("Bare Eye Cacatoo",R.drawable.bare_eye_cacatoo));
        arrayList.add(new bird_counter("Barraband Parakeet",R.drawable.barraband_parakeet));
        arrayList.add(new bird_counter("Calico Macaw",R.drawable.calico_macaw));
        arrayList.add(new bird_counter("Cape Parrot",R.drawable.cape_parrot));

        bird_recyclerAdapter= new bird_recyclerAdapter(arrayList,getActivity());
        recyclerView.setAdapter(bird_recyclerAdapter);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        getActivity().getMenuInflater().inflate(R.menu.search_menu,menu);
        MenuItem menuItem=menu.findItem(R.id.search_action);
        SearchView searchView=(SearchView)MenuItemCompat.getActionView(menuItem);
        searchView.setOnQueryTextListener(this);
    }
    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        newText = newText.toLowerCase();
        ArrayList<bird_counter> newList=new ArrayList<>();
        for(bird_counter bird_counter:arrayList){
            String name= bird_counter.getName().toLowerCase();
            if(name.contains(newText))
                newList.add(bird_counter);
        }
        bird_recyclerAdapter.setFilter(newList);
        return true;
    }
}

0 个答案:

没有答案