如何将RecyclerView中单击项目的背景颜色传递给下一个活动的布局?

时间:2018-04-23 10:04:16

标签: android android-recyclerview

我有一个带有多种背景颜色项目的RecyclerView。 我希望当我点击我的recyclerView中的项目时,下一个活动的布局将具有与所单击项目相同的背景颜色。我怎样才能在setOnClickListener方法中执行此操作?感谢

这是我的适配器:

    public class AdapterColors extends RecyclerView.Adapter<AdapterColors.MyViewholder> {

        List<Colors> listArray;
        Context context;

        public AdapterColors(List<Color> List,Context context){
        this.listArray = List;
        this.context = context;
    }

    @Override
    public MyViewholder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from ( parent.getContext ()).inflate ( R.layout.itemview,parent,false );
        return new MyViewholder(view);
    }
        }
        @Override
            public void onBindViewHolder(MyViewholder holder, int position) {

                Color data = listArray.get ( position );

                if(position % 5 == 0){
                    holder.TextColor.setBackgroundColor(ContextCompat.getColor(context,R.color.color1));
                }else if(position % 5 == 1){
                    holder.TextColor.setBackgroundColor(ContextCompat.getColor(context,R.color.color2));
                }else if(position % 5 == 2){
                    holder.TextColor.setBackgroundColor(ContextCompat.getColor(context,R.color.color3));
                }else if(position % 5 == 3){
                    holder.TextColor.setBackgroundColor(ContextCompat.getColor(context,R.color.color4));
                }else if(position % 5 == 4){
                    holder.TextColor.setBackgroundColor(ContextCompat.getColor(context,R.color.color5));
                }

                holder.TextColor.setText ( data.getColor ( ) );
            }
    }

4 个答案:

答案 0 :(得分:0)

通过意图传递颜色代码,下一个活动通过getIntent()获取值.getStringExtra()然后使用.setBackgroudColor(Color.parse(getIntntStringExtra()))在父视图中设置背景颜色。

答案 1 :(得分:0)

试试这个:

int color = Color.TRANSPARENT;
Drawable background = view.getBackground();
if (background instanceof ColorDrawable)
  color = ((ColorDrawable) background).getColor();

//While passing in intent
intent.putExtra("backgroundColor",color);

替换你的&#34;视图&#34;您自己的视图(回收者视图父项:ex线性或相对布局)

希望这有帮助!

答案 2 :(得分:0)

更改你的onBindViewHolder方法代码:

public void onBindViewHolder(MyViewholder holder, int position) {

        Color data = listArray.get ( position );

        if(position % 5 == 0){
            holder.TextColor.setBackgroundColor(ContextCompat.getColor(context,R.color.color1));
    holder.TextColor.setOnClickListener( View.OnCLickListener(){
        @Override
        public void onClick(View v) {
            Bundle bundle = new Bundle();
            bundle.putInt("color_code", 0);
            Intent intent = new Intent();
            intent.putExtras(bundle);
            startActivity(intent);
            // Enter the Above code in all the below cases as well

        }
    }
        }else if(position % 5 == 1){
            holder.TextColor.setBackgroundColor(ContextCompat.getColor(context,R.color.color2));
        }else if(position % 5 == 2){
            holder.TextColor.setBackgroundColor(ContextCompat.getColor(context,R.color.color3));
        }else if(position % 5 == 3){
            holder.TextColor.setBackgroundColor(ContextCompat.getColor(context,R.color.color4));
        }else if(position % 5 == 4){
            holder.TextColor.setBackgroundColor(ContextCompat.getColor(context,R.color.color5));
        }

        holder.TextColor.setText ( data.getColor ( ) );
    }

下一个开场活动

Bundle bundle = getIntent().getExtras();
    if (bundle != null) {
        switch(bundle.getString("color_code")) {
            case 0:
                // Your code logic
                break;
            case 1:
                // Your code logic
                break;
            case 2:
                // Your code logic
                break;
            case 3:
                // Your code logic
                break;
            case 4:
                // Your code logic
                break;
        }
    }

答案 3 :(得分:0)

尝试以下理想的方法

public class AdapterColors extends RecyclerView.Adapter<AdapterColors.MyViewholder> {

    List<Color> listArray;
    Context context;
    OnItemClickListener onItemClickListener;


    public AdapterColors(List<Color> List,OnItemClickListener onItemClickListener){
        this.listArray = List;
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(Color colors);
    }
    @Override
        public void onBindViewHolder(MyViewholder holder, int position) {

            Color data = listArray.get ( position );

            if(position % 5 == 0){
                holder.TextColor.setBackgroundColor(ContextCompat.getColor(context,R.color.color1));
            }else if(position % 5 == 1){
                holder.TextColor.setBackgroundColor(ContextCompat.getColor(context,R.color.color2));
            }else if(position % 5 == 2){
                holder.TextColor.setBackgroundColor(ContextCompat.getColor(context,R.color.color3));
            }else if(position % 5 == 3){
                holder.TextColor.setBackgroundColor(ContextCompat.getColor(context,R.color.color4));
            }else if(position % 5 == 4){
                holder.TextColor.setBackgroundColor(ContextCompat.getColor(context,R.color.color5));
            }

            holder.TextColor.setText ( data.getColor ( ) );

        holder.TextColor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onItemClickListener.onItemClick(data);

            }
        });
        }
}

在您的活动类

AdapterColors adapterColors = new AdapterColors(yourColorList,new AdapterColors.OnItemClickListener() {
    @Override
    public void onItemClick(Color color) {
        //Add your Activity code here
        // Add color in your bundle
    }
});