适配器外部的Recyclerview更改项目颜色

时间:2018-10-09 21:28:14

标签: java android android-recyclerview

当前,我正在尝试找出如何在适配器外部更改RecyclerView项目颜色。我只是在操纵位置,并且工作正常!但是我必须滑动页面以进行颜色更新,而且我不知道该怎么办才能解决此问题。

检查我的代码:

MainActivity.java

...
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...


        adapter = new viewPagerAdapter(getSupportFragmentManager());

        //Add fragments here
        adapter.addFragment(new fragmentList(), ""); //Lista de música
        adapter.addFragment(new fragmentFrequent(), ""); //Frequentes
        adapter.addFragment(new fragmentPlayList(), ""); //Playlist

        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);

        player_prev = findViewById(R.id.prev);
        player_prev.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if ((currentPos-1) > 0) {
                    killMediaPlayer();
                    initAudio(getApplicationContext(), 
                    RecyclerViewAdapter.mData.get(currentPos - 1).getURL());
                    currentPos -= 1;


                    RecyclerViewAdapter.OldselectedPos = RecyclerViewAdapter.selectedPos;
                    RecyclerViewAdapter.selectedPos = currentPos;
                    adapter.notifyDataSetChanged();
            }
        });

        ...
}

RecyclerViewAdapter.java

package etes.xdda.music;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;


public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> implements View.OnClickListener{


    MainActivity activity;
    static List<mList> mData;
    Dialog myDialog;

    public static int selectedPos = RecyclerView.NO_POSITION;
    public static int OldselectedPos = RecyclerView.NO_POSITION;

    private LinearLayout menu_dialog, menu_dialog2;
    public static TextView song_detail;
    public RecyclerViewAdapter(MainActivity activity, List<mList> mData) {
        this.activity = activity;
        this.mData = mData;
    }

    @Override
    public void onClick(View view) { }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v;
        v = LayoutInflater.from(activity).inflate(R.layout.item_list, parent, false);
        final MyViewHolder vHolder = new MyViewHolder(v);

        // Dialog ini
        myDialog = new Dialog(activity);
        myDialog.setContentView(R.layout.dialog);
        myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        vHolder.item_play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                TextView dialog_name_tv = myDialog.findViewById(R.id.dialog_name_id);
                TextView dialog_phone_tv = myDialog.findViewById(R.id.dialog_author_id);
                ImageView dialog_contact_img = myDialog.findViewById(R.id.dialog_img);
                dialog_name_tv.setText(mData.get(vHolder.getAdapterPosition()).getName());
                dialog_phone_tv.setText(mData.get(vHolder.getAdapterPosition()).getPhone());
                dialog_contact_img.setImageResource(mData.get(vHolder.getAdapterPosition()).getPhoto());

                //Toast.makeText(mContext, "Test click "+String.valueOf(vHolder.getAdapterPosition()), Toast.LENGTH_SHORT).show();
                myDialog.show();
            }
        });
        vHolder.menu_play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                OldselectedPos = selectedPos;
                selectedPos = vHolder.getAdapterPosition();

                notifyItemChanged(selectedPos);
                notifyItemChanged(OldselectedPos);


                menu_dialog = v.getRootView().findViewById(R.id.menu_dialog);
                menu_dialog.setVisibility(v.VISIBLE);

                menu_dialog2 = v.getRootView().findViewById(R.id.menu_dialog2);
                menu_dialog2.setVisibility(v.VISIBLE);

                song_detail = v.getRootView().findViewById(R.id.song_detail);
                song_detail.setVisibility(v.VISIBLE);


                String newName;
                newName = mData.get(vHolder.getAdapterPosition()).getName();
                if (newName.length() > 42) {
                    newName = newName.substring(0, 38) + "...";
                }
                song_detail.setText(newName);

                activity.killMediaPlayer();
                activity.initAudio(v.getContext(), mData.get(vHolder.getAdapterPosition()).getURL());
                activity.setMargins(v.getRootView().findViewById(R.id.viewpager_id), 0,0,0,205);
                activity.updateNotificationBar("mzPlay", mData.get(vHolder.getAdapterPosition()).getName());
                MainActivity.currentPos =  vHolder.getAdapterPosition();
            }
        });
        return vHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.tv_name.setText(mData.get(position).getName());
        holder.tv_author.setText(mData.get(position).getPhone());
        holder.img.setImageResource(mData.get(position).getPhoto());

        //Toast.makeText(activity, String.valueOf(holder), Toast.LENGTH_LONG).show();
        if(selectedPos == position){
            holder.itemView.setBackgroundColor(Color.parseColor("#373737"));
        }
        else
        {
            holder.itemView.setBackgroundColor(Color.parseColor("#212121"));
        }
    }


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


    public static class MyViewHolder extends RecyclerView.ViewHolder {

        private ImageButton item_play;
        private LinearLayout menu_play;
        private TextView tv_name;
        private TextView tv_author;
        private ImageView img;

        public MyViewHolder(View itemView) {
            super(itemView);
            item_play = itemView.findViewById(R.id.info_id);
            menu_play = itemView.findViewById(R.id.list_item_id);

            tv_name = itemView.findViewById(R.id.name_list);
            tv_author = itemView.findViewById(R.id.author_list);
            img = itemView.findViewById(R.id.img_music);
        }
    }
}

视频:https://ntcdn.stream/20181009_2342010.mp4

问题是:我必须滑动页面以更新项目颜色,如何在不滑动页面的情况下更新颜色?

1 个答案:

答案 0 :(得分:1)

将类对象类存储在静态变量中确实是一个非常糟糕的做法。您应该使用非静态字段与适配器进行常规交互,例如:

//This will go in your onCreate() for example
MyAdapter adapter = new MyAdapter(parameters);
...
adapter.highligtedItemPosition = 10;
adapter.notifyItemChanged(10);

,然后在您的onBindViewHolder()中根据要设置的背景做出反应:

if(position == highligtedItemPosition ){
    holder.itemView.setBackgroundColor(Color.parseColor("#373737"));
}
else{
    holder.itemView.setBackgroundColor(Color.parseColor("#212121"));
}

在此示例中,您必须添加一个整数字段:

int highligtedItemPosition;

到适配器以存储要更改颜色的项目的位置。

更新: 您没有从活动中调用notifyItemChange()或notifyDataSetChanged(),因此适配器需要“等待”滚动操作才能“看到”更改。您应该改为从player_prev的OnClickListener调用notifyItemChange()或notifyDataSetChanged()。因此适配器将立即反映更改。