简体   繁体   English

触摸事件后重绘RecyclerView

[英]Redraw RecyclerView After A Touch Event

I have a RecycleView Which shows a bunch of list in which one item is selected by showing its background color blue, now I want user to select any item from list and its color get changed to blue how to implement this inside RecyclerView.Adapter or any other logic 我有一个RecycleView,它显示一堆列表,其中一个项目通过显示其背景颜色为蓝色而被选中,现在我希望用户从列表中选择任何项目,并且其颜色变为蓝色,如何在RecyclerView.Adapter或任何内部实现该项目其他逻辑

    public class ToggleAdapter extends RecyclerView.Adapter<ToggleAdapter.ToggleViewHolder>{

    private ArrayList<ToggleParams> dataList=new ArrayList<>();
    private Context context;
    private static int selection;

    public ToggleAdapter(ArrayList<ToggleParams> dataList, Context context,int selection) {
        setData(dataList,context,selection);
    }



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

        ToggleViewHolder toggleViewHolder=new ToggleViewHolder(v);
        return toggleViewHolder;
    }

    @Override
    public void onBindViewHolder(ToggleViewHolder holder, int position) {
        if(position==selection){
            holder.selected_item.setBackgroundColor(context.getResources().getColor(R.color.blue));
            holder.text_view.setTextColor(context.getResources().getColor(android.R.color.white));
        }
        holder.image_view.setImageDrawable(context.getResources().getDrawable(dataList.get(position).getIMAGE_ID()));
        holder.text_view.setText(dataList.get(position).getTOGGLE_TEXT());

    }

    private void setData(ArrayList<ToggleParams> dataList, Context context,int selection) {
        this.dataList = dataList;
        this.context = context;
        this.selection = selection;
    }

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

    public  static class ToggleViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        public LinearLayout selected_item;
        public ImageView image_view;
        public TextView text_view;

        public ToggleViewHolder(View itemView) {
            super(itemView);

            selected_item= (LinearLayout) itemView.findViewById(R.id.selected_item);
            selected_item.setOnClickListener(this);
            image_view= (ImageView) itemView.findViewById(R.id.imageView);
            text_view= (TextView) itemView.findViewById(R.id.textView);
        }


        public void onClick(View v) {
            selection=getPosition();
            //After getting this position I want that this item list in recyclerview to change its background color but how to call notifyDataSetChange() here something equivalent to that  
        }
    }
}

You can use the setOnCLickListener in onBindViewHolder and can call notifyDatasetChanged there or a better way is to define an interface which gets called on when an itemView is clicked and to instantiate it in the adapter. 您可以在setOnCLickListener中使用onBindViewHolder并在onBindViewHolder调用notifyDatasetChanged ,或者更好的方法是定义一个单击onBindViewHolder时调用的接口,并在适配器中实例化该接口。 For example see the following answer Why doesn't RecyclerView have onItemClickListener()? 例如,请参见以下答案: 为什么RecyclerView没有onItemClickListener()? And how RecyclerView is different from Listview? RecyclerView与Listview有何不同?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM