如何更改 ListView 中每个项目的背景颜色?

How to change the background color of every item in a ListView?

我正在开发一个具有文本消息界面的应用程序(类似于 Facebook Messenger、Whatsapp 等)。我希望能够更改用户在选择新颜色(NavigationView 中)时发送的所有聊天气泡的背景颜色(TextView 中的 ListView)。

但是,使用我当前的代码,我只能在再次单击用于撰写消息的 EditText 后更改颜色。或者我只能编辑发送的第一个气泡,但一旦颜色改变。

这是我的尝试:

ItemColor1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v){

        Toast.makeText(activity, "Couleur mise à jour", Toast.LENGTH_SHORT).show();
        currentTheme = position;
        SharedPreferences.Editor editor = pref.edit();
        editor.putInt("indexColorSelected",currentTheme);
        editor.apply();
        chatAdapter.notifyDataSetChanged();
        //the following changes only the first message sent     
        for(int i=0; i<chatAdapter.getCount(); i++){
            ChatData message = chatMessageList.get(position);
            TextView msg = activity.findViewById(R.id.text);
            msg.setText(message.body);
            msg.setBackgroundResource(R.drawable.user_color1);
        }
    }
});

ChatData 是我创建的自定义 Class,如下所示:

public class ChatAdapter extends BaseAdapter {

    private static LayoutInflater inflater = null;
    private ArrayList<ChatData> chatMessageList;
    private Context mContext;

    ChatAdapter(Activity activity, ArrayList<ChatData> list) {
        mContext = activity;
        chatMessageList = list;
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    ...
}

可绘制颜色:

    <corners
        android:bottomRightRadius="5dp"
        android:radius="40dp"/>

    <gradient
        android:angle="45"
        android:endColor="#01f1fa"
        android:startColor="#0189ff"
        android:type="linear" />

</shape>

getView() 适配器方法:

public View getView(int position, View convertView, ViewGroup parent) {
    ChatData message = chatMessageList.get(position);
    View vi = convertView;

    if (convertView == null)
        vi = inflater.inflate(R.layout.msglist, parent, false);

    TextView msg = vi.findViewById(R.id.text);
    msg.setText(message.body);

    LinearLayout layout = vi.findViewById(R.id.message_layout);
    LinearLayout parent_layout = vi.findViewById(R.id.message_layout_parent);

    inflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    // if message is mine then align to right
    if (message.isMine) {
        layout.setGravity(Gravity.RIGHT);

        int couleurBubble = getCouleurSelectionnee();

        switch(couleurBubble){
            case R.color.c1: msg.setBackgroundResource(R.drawable.user_color1); break;
            case R.color.c2: msg.setBackgroundResource(R.drawable.user_color2); break;
            case R.color.c3: msg.setBackgroundResource(R.drawable.user_color3); break;
            case R.color.c4: msg.setBackgroundResource(R.drawable.user_color4); break;
            case R.color.c5: msg.setBackgroundResource(R.drawable.user_color5); break;
            case R.color.c6: msg.setBackgroundResource(R.drawable.user_color6); break;
            case R.color.c7: msg.setBackgroundResource(R.drawable.user_color7); break;
            case R.color.c8: msg.setBackgroundResource(R.drawable.user_color8); break;
            default: break;
        }

        parent_layout.setGravity(Gravity.RIGHT);
    }
    // If not mine then align to left
    else {
        layout.setGravity(Gravity.LEFT);
        msg.setBackgroundResource(R.drawable.bot_chat);
        parent_layout.setGravity(Gravity.LEFT);
    }
    return vi;
}

我真的不知道从这里该何去何从,所以任何形式的帮助将不胜感激。如果您希望我提供更多代码,请告诉我,我会的。

谢谢。

我正在分享我会怎么做。也许,这可以帮助你。 由于您正在调用 notifyDataSetChanged(),因此出现了一些奇怪的问题。这足以 re-draw 所有消息气泡。

我的想法是:

向适配器 class (mColorResource) 添加一个 int 变量。此变量将指向应使用的正确可绘制对象(如 R.drawable.user_color1)。

public class ChatAdapter extends BaseAdapter {

    int mColorResource;

    ChatAdapter(Activity activity, ArrayList<ChatData> list, int initialColorResource) {
        mContext = activity;
        chatMessageList = list;
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        // You must receive the color on the construtor
        mColorResource = initialColor;
    }

    // Use this method to update the color (when user select a new color)
    public void setColor(int newColorResource) {
        mColorResource = newColorResource;
    }


    public View getView(int position, View convertView, ViewGroup parent) {
        ...
        // Note how this if-else is cleaner now
        if (message.isMine) {
            layout.setGravity(Gravity.RIGHT);
            msg.setBackgroundResource(mColorResource);
            parent_layout.setGravity(Gravity.RIGHT);
        } else {
            layout.setGravity(Gravity.LEFT);
            msg.setBackgroundResource(R.drawable.bot_chat);
            parent_layout.setGravity(Gravity.LEFT);
        }
        ...
    }
}

然后,选择颜色后,根据点击的视图找到合适的可绘制对象并将其传递给适配器:

ItemColor1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v){
        Toast.makeText(activity, "Couleur mise à jour", Toast.LENGTH_SHORT).show();
        currentTheme = position;

        SharedPreferences.Editor editor = pref.edit();
        editor.putInt("indexColorSelected", currentTheme);
        editor.apply();

        // Here, you send the proper drawable...
        // I'm not sure how you convert color selected to the drawable
        // So, add your logic to convert the button clicked to a drawable here
        // like R.drawable.user_color1
        chatAdapter.setColor(R.drawable.NAME_OF_THE_COLOR);

        // Request to re-draw all items from the list (all bubbles)
        chatAdapter.notifyDataSetChanged();
    }
});

此外,在您的 activity 上,您使用上次使用的颜色创建适配器。类似于:

@Override
protected void onCreate(@Nullable final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ....
    // Set a default color (if user is open you app for the first time)
    int chatColor = R.drawable.user_color1;

    // Add your logic to read the shared preference and convert that last color used to a valid drawable.
    // Like chatColor = pref.getInt(indexColorSelected, R.drawable.user_color1) etc....

    // Set the color in the adapter.
    chatAdapter = newAdapter(this, mChatDataList, chatColor);
}