我正在开发一个具有文本消息界面的应用程序(例如Facebook Messenger,Whatsapp等)。我希望能够更改用户在df %>%
filter(
(str_detect(id, "^M.+(KIT|FLEECE)") & between(f1, 300, 400) & between(f2, 1300, 1400)) |
(str_detect(id, "^M.+(GOOSE)") & between(f1, 200, 350) & between(f2, 1200, 1400))
)
中选择新颜色时发送的所有聊天气泡(ListView
中的TextView
个)的背景颜色。
但是,使用当前的代码,仅在再次单击用于撰写邮件的NavigationView
后才能更改颜色。或者,我只能编辑发送的第一个气泡,但是一旦颜色更改即可。
这就是我尝试过的:
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()
我真的不知道从这里去哪里,所以我们将不胜感激。如果您希望我提供更多代码,请告诉我,我会的。
谢谢。
答案 0 :(得分:1)
我正在分享我将如何做。也许可以帮到您。
有一个奇怪的问题,因为您正在呼叫notifyDataSetChanged()
。这足以重新绘制所有消息气泡。
我的想法是:
向适配器类(int
)添加mColorResource
变量。此变量将指向应使用的适当可绘制对象(例如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();
}
});
此外,在您的活动中,还使用最后使用的颜色创建适配器。像这样:
@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);
}