【发布时间】:2019-05-23 14:47:27
【问题描述】:
我正在开发一个具有文本消息界面的应用程序(例如 Facebook Messenger、Whatsapp 等)。我希望能够更改用户在选择新颜色(NavigationView)时发送的所有聊天气泡(ListView 或 TextViews)的背景颜色。
但是,使用我拥有的当前代码,我只能在再次单击用于撰写消息的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 是我创建的自定义类,如下所示:
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()Adapter 的方法:
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;
}
我真的不知道从这里去哪里,所以任何形式的帮助都将不胜感激。如果您希望我提供更多代码,请告诉我,我会的。
谢谢。
【问题讨论】:
-
您必须从适配器更新 getView 中的颜色。从您的适配器共享该 getView 方法
-
为什么必须使用列表视图。有什么东西阻止你使用 recyclerview
-
@W0rmH0le 是的,我已经尝试过了,但不幸的是,这只会在我点击输入消息的
EditText后改变消息的颜色。我希望消息在按下新选择的颜色后立即改变颜色。我在帖子中添加了我的 getView 方法,以供您查看。 -
说实话,我从来没有听说过 RecyclerView,我会看看它是否能解决我的问题。感谢您的建议。
-
在listView中调用invalidade()
标签: java android android-layout android-listview