当我从列表视图中选择一个项目时,它会变成灰色,但是当我在列表视图中选择另一个项目时,上一个突出显示的项目会变成默认颜色,而新项目会变成灰色。我想让它们都保持突出显示状态,并且当单击突出显示的项目时,它们也应该恢复为正常状态。
我的代码:
listView = (ListView) findViewById(R.id.conversationList);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listView.setMultiChoiceModeListener(new conversationsMultipleChoiceListener());
listView.setAdapter(conversationsAdapter);
((ConversationsAdapter) conversationsAdapter).updateConversations(conversationsList);
listView.setLongClickable(true);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Conversation item = (Conversation) conversationsAdapter.getItem(position);
startChat(item, false);
}
});
private class conversationsMultipleChoiceListener implements ListView.MultiChoiceModeListener {
@Override
public void onItemCheckedStateChanged(android.view.ActionMode mode, int position, long id, boolean checked) {
final int checkedCount = listView.getCheckedItemCount();
switch (checkedCount) {
case 0:
mode.setSubtitle(null);
break;
default:
mode.setSubtitle("" + checkedCount + " konuşma seçildi");
break;
}
}
@Override
public boolean onCreateActionMode(android.view.ActionMode mode, Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.conversation_actions, menu);
mode.setTitle("Konuşmalarınız");
return true;
}
@Override
public boolean onPrepareActionMode(android.view.ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(android.view.ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.sil:
Log.w("action","sil");
mode.finish();
break;
case R.id.blokla:
Log.w("action","blokla");
mode.finish();
break;
default:
break;
}
return true;
}
@Override
public void onDestroyActionMode(android.view.ActionMode mode) {
}
}
适配器:
static class ViewHolder {
TextView nameView;
TextView bioView;
CircleImageView imageView;
TextView badge;
ProgressBar idleTimeBar;
public ImageView eyeView;
ImageView instagramVerified;
RelativeLayout badgeBg;
ImageView solOk;
TextView tarihText;
ImageView fotoIcon;
ImageView sesIcon;
}
public static class ConversationsAdapter extends BaseAdapter {
List<Conversation> conversations;
LayoutInflater inflater;
Context context;
private ConversationsAdapter(Context context, List<Conversation> conversations) {
this.conversations = conversations;
this.inflater = LayoutInflater.from(context);
this.context = context;
}
public void updateConversations(List<Conversation> conversations) {
this.conversations = conversations;
notifyDataSetChanged();
}
@Override
public int getCount() {
return conversations.size();
}
@Override
public Conversation getItem(int position) {
return conversations.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.conversation_bars_v2, parent, false);
holder = new ViewHolder();
holder.nameView = (TextView) convertView.findViewById(R.id.nameText);
holder.badge = (TextView) convertView.findViewById(R.id.unreadMessageCount);
holder.bioView = (TextView) convertView.findViewById(R.id.bioText);
holder.imageView = (CircleImageView) convertView.findViewById(R.id.image);
holder.badgeBg = (RelativeLayout) convertView.findViewById(R.id.unreadMessageBg);
holder.solOk = (ImageView) convertView.findViewById(R.id.imageView22);
holder.tarihText = (TextView) convertView.findViewById(R.id.textView16);
holder.sesIcon = (ImageView) convertView.findViewById(R.id.imageView22);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Conversation item = conversations.get(position);
String name = item.getName();
String image_url = "http://application.domain.com/photos/profile/main/" + item.getPicture();
if (item.getLastMessage().equals("Fotoğraf")) {
holder.sesIcon.setImageResource(R.drawable.send_photo);
holder.sesIcon.setVisibility(View.VISIBLE);
}else if (item.getLastMessage().equals("Ses")) {
holder.sesIcon.setImageResource(R.drawable.send_voice_record);
holder.sesIcon.setVisibility(View.VISIBLE);
}else{
holder.sesIcon.setVisibility(View.INVISIBLE);
}
Long messageTimestamp = item.getLastMessageTime();
long nowTimestamp = timeHolder.toMillis(false);
Calendar messageTimeCal = Calendar.getInstance();
Calendar currentTimeCal = Calendar.getInstance();
messageTimeCal.setTimeInMillis(messageTimestamp);
currentTimeCal.setTimeInMillis(nowTimestamp);
boolean sameDay = messageTimeCal.get(Calendar.DAY_OF_YEAR) == currentTimeCal.get(Calendar.DAY_OF_YEAR) &&
messageTimeCal.get(Calendar.YEAR) == currentTimeCal.get(Calendar.YEAR);
boolean lastDay = messageTimeCal.get(Calendar.DAY_OF_YEAR) == currentTimeCal.get(Calendar.DAY_OF_YEAR) -1 &&
messageTimeCal.get(Calendar.YEAR) == currentTimeCal.get(Calendar.YEAR);
String dateText = "";
if (sameDay) {
//Mesaj bugün mü gönderildi?
SimpleDateFormat dateFormat1 = new SimpleDateFormat("HH:mm");
dateText = dateFormat1.format(messageTimeCal.getTime());
}else{
if (lastDay) {
//Bugün değilse mesaj dün mü gönderilmiş?
dateText = "Dün";
}else{
//Dün de değilse kaç gün önce gönderilmiş?
long diff = Math.abs(nowTimestamp - messageTimestamp);
int bolum = (int) (diff / (24 * 60 * 60 * 1000));
if (bolum <= 3) {
//Eğer geçen gün sayısı 3 veya daha az ise gün adını ekrana yaz
dateText = String.format("%tA", messageTimeCal);
}else{
//Yoksa direk tarihi yaz
SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
dateText = dateFormat.format(messageTimeCal.getTime());
}
}
}
holder.tarihText.setText(dateText);
holder.nameView.setText(name);
if (item.getLastMessageFromMe() == 0) {
holder.bioView.setTextColor(Color.parseColor("#7A7A7A"));
//holder.solOk.setColorFilter(Color.parseColor("#666666"), android.graphics.PorterDuff.Mode.MULTIPLY); -- mehmet değiştirdi
holder.bioView.setTypeface(Typeface.SANS_SERIF, Typeface.NORMAL);
} else {
holder.bioView.setTextColor(Color.parseColor("#7A7A7A"));
//holder.solOk.setColorFilter(Color.parseColor("#A8A8B7"), android.graphics.PorterDuff.Mode.MULTIPLY); -- mehmet değiştirdi
holder.bioView.setTypeface(Typeface.SANS_SERIF, Typeface.NORMAL);
}
holder.bioView.setText(item.getLastMessage());
if (item.getLastMessage().equals("")) {
holder.bioView.setVisibility(View.GONE);
} else {
holder.bioView.setVisibility(View.VISIBLE);
}
if (item.getPicture().equals("default.jpg")) {
Picasso.with(context).load(R.drawable.default_picture).into(holder.imageView);
}else if (item.getPicture().equals("default_1.jpg")) {
Picasso.with(context).load(R.drawable.default_color_1).into(holder.imageView);
}else if (item.getPicture().equals("default_2.jpg")) {
Picasso.with(context).load(R.drawable.default_color_2).into(holder.imageView);
}else if (item.getPicture().equals("default_3.jpg")) {
Picasso.with(context).load(R.drawable.default_color_3).into(holder.imageView);
}else if (item.getPicture().equals("default_4.jpg")) {
Picasso.with(context).load(R.drawable.default_color_4).into(holder.imageView);
}else if (item.getPicture().equals("default_5.jpg")) {
Picasso.with(context).load(R.drawable.default_color_5).into(holder.imageView);
}else{
Picasso.with(context).load(image_url).into(holder.imageView);
}
holder.badge.setText(String.valueOf(item.getUnread()));
if (item.getUnread() != 0) {
holder.badgeBg.setVisibility(View.VISIBLE);
} else {
holder.badgeBg.setVisibility(View.GONE);
}
return convertView;
}
}
Listview xml:
<ListView
android:id="@+id/conversationList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="0dp"
android:layout_marginRight="10dp"
android:divider="@android:color/transparent"
android:dividerHeight="7.0sp"
android:scrollbars="none"
android:listSelector="@drawable/conversations_selector"></ListView>
我如何实现该功能?
答案 0 :(得分:0)
首先,在conversationSelected
类中添加字段Conversation
。
您应该使用RecyclerView
而不是ListView
。
您可以参考official documentation来开始使用RecyclerView
。
拥有RecyclerView
后,您可以通过以下方式定义Adapter
:
public class ConversationAdapter extends RecyclerView.Adapter<ConversationAdapter.ViewHolder> {
List<Conversation> conversations;
public ConversationAdapter(List<Conversation> conversations) {
this.conversations = conversations;
}
public ConversationAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
TextView v = (TextView) LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_view, parent, false);
...
MyViewHolder vh = new MyViewHolder(v);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Conversation conversation = conversations.get(position);
holder.mTextView.setText(conversation.getConversationText());
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return conversations.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView mTextView;
public MyViewHolder(TextView v) {
super(v);
mTextView = v;
}
}
下一步:将项目点击侦听器添加到RecyclerView
:在看到此答案之前,我有点挣扎,这可以帮助我解决问题:RecyclerView click Listener
在clickListener中,您必须将conversationSelected
列表(不在Conversation
内部)中conversations
的状态ViewHolder
更改为true
。
然后,当您需要检索选定的对话时,只需检查此值即可。