点击其他商品时,不会删除Recyclerview旧商品视图?

时间:2016-06-16 10:16:13

标签: android android-recyclerview

我有一个recyclerview。我点击一个项目。它突出显示(设置背景 - 颜色)。然后,我点击另一个项目,它不会删除旧的突出显示的视图。我的课非常泄密。你也可以给我一个提高效率的建议吗?

public class DrawerMenuShelfListAdapter extends RecyclerView.Adapter<DrawerMenuShelfListAdapter.ViewHolder> implements View.OnClickListener {

    private int selectedRow = 0;

    public Fragment fragment;
    public ViewHolder holder;
    public RecyclerView recyclerView;

    private ArrayList<ShelfModel> shelfList;

    public DrawerMenuShelfListAdapter(Fragment fragment, ArrayList<ShelfModel> shelfList, RecyclerView recyclerView) {
        this.shelfList = shelfList;
        this.fragment = fragment;
        this.recyclerView = recyclerView;
    }

    public void setItemSelected(int position){
        recyclerView.getChildAt(selectedRow).setBackgroundColor(fragment.getActivity().getResources().getColor(android.R.color.transparent));
        notifyItemChanged(selectedRow);
        selectedRow = position;
        recyclerView.getChildAt(selectedRow).setBackgroundColor(fragment.getActivity().getResources().getColor(android.R.color.black));
        notifyItemChanged(selectedRow);
    }

    public void updateList(ArrayList<ShelfModel> list){
        shelfList.clear();
        shelfList.addAll(list);
        notifyDataSetChanged();
        notifyItemChanged(selectedRow);
    }

    @Override
    public DrawerMenuShelfListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v= LayoutInflater.from(parent.getContext())
                .inflate(R.layout.navigation_drawer_list_row, parent, false);

        holder = new ViewHolder(v);
        holder.text = (TextView) v.findViewById(R.id.definitionText);
        holder.total = (TextView) v.findViewById(R.id.countText);
        holder.editButton = (ImageButton)v.findViewById(R.id.editButton);
        holder.text.setTypeface(App.MUSEO_100);
        holder.total.setTypeface(App.MUSEO_300);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        final ShelfModel shelfModel = shelfList.get(position);
        holder.text.setText(shelfModel.getName());
        holder.total.setText(String.valueOf(shelfModel.getTotal()));
        holder.shelfId = shelfModel.getShelfId();
        holder.itemView.setOnClickListener(this);
    }

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

    @Override
    public void onClick(View v) {
        int position = recyclerView.getChildLayoutPosition(v);
        ((DrawerMenuShelfListListener)fragment).onShelfItemClick(shelfList.get(position));
        setItemSelected(position);

    }

    public interface DrawerMenuShelfListListener extends BaseRecyclerViewListener {
        void onShelfItemClick(ShelfModel shelfModel);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView text;
        public TextView total;
        public ImageView editButton;
        public int shelfId;
        public ViewHolder(View view) {
            super(view);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

确定。我找到了解决方案。如果我不调用notifyItemChanged,它可以正常工作