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

Java 如何更改ListView中每个项目的背景色?,java,android,android-layout,android-listview,Java,Android,Android Layout,Android Listview,我正在开发一个有文字信息界面的应用程序(比如Facebook Messenger、Whatsapp等)。我希望能够在用户选择新颜色时(在导航视图中)更改用户发送的所有聊天泡泡的背景色(属于文本视图的列表视图) <corners android:bottomRightRadius="5dp" android:radius="40dp"/> <gradient android:angle="45" a

我正在开发一个有文字信息界面的应用程序(比如Facebook Messenger、Whatsapp等)。我希望能够在用户选择新颜色时(在
导航视图
中)更改用户发送的所有聊天泡泡的背景色(属于
文本视图
列表视图

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

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

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

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

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

</shape>
以下是我尝试过的:

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);
        }
    }
});
    <corners
        android:bottomRightRadius="5dp"
        android:radius="40dp"/>

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

</shape>
可绘制颜色:

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

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

</shape>
我真的不知道从这里到哪里去,所以任何形式的帮助都将不胜感激。如果你想让我提供更多的代码,让我知道,我会的

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

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

</shape>

谢谢。

我正在分享我将如何做到这一点。也许,这可以帮助你。
    <corners
        android:bottomRightRadius="5dp"
        android:radius="40dp"/>

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

</shape>
出现了一些奇怪的问题,因为您正在调用
notifyDataSetChanged()
。这将足以重新绘制所有消息气泡

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

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

</shape>
我的想法是:

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

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

</shape>
int
变量添加到适配器类(
mColorResource
)。此变量将指向应使用的正确可绘制文件(如
R.drawable.user\u color1

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

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

</shape>
另外,在您的活动中,使用上次使用的颜色创建适配器。比如:

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

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

</shape>
@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);
}

您必须从适配器更新getView中的颜色。从适配器共享该getView方法为什么必须使用列表视图。是否有什么东西阻止您使用recyclerview@W0rmH0le是的,我已经试过了,但不幸的是,在我点击我输入信息的
EditText
后,这只会改变信息的颜色。我希望消息在按下新选择的颜色后立即更改颜色。我在帖子中添加了我的getView方法,以防你想看一看。老实说,我从来没有听说过RecyclerView,我会看一看,看看这是否解决了我的问题。感谢您的建议。在listView中调用invalidade()