Примером есть ВьюИтем, заполняемый в адаптере. Там же стоит слушатель на этот ВьюЭлемент. По клику мы должны изменить
фон этого элемента. И вроде бы все хорошо, элементы меняют фон. Но! Не с первого клика. Верней не всегда с первого клика.
Примером есть два элемента, один с одним фоном, другой с другим. И когда я нажимаю на второй элемент, адаптер каким то образом
считывает информацию о предыдущем элементе (там стоит проверка), и возвращает тоже самое, получается из за этого приходится
два раза нажимать. Почему так адаптер делает, я не знаю, и надеюсь кто то подскажет. Сложно объяснить.
Примером дай свой код, как я это использую (Но я пробовал слушатели в самом разном виде, корректно по одному клику
свой вид они НЕ изменяют). Таков код адаптера :
Код: Выделить всё
package com.breezy.Message;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.breezy.Activity.MainActivity;
import com.breezy.R;
import java.util.List;
public class ChatMessageAdapter extends ArrayAdapter<ChatItems> {
private int resource;
private int gravity;
private int messageColor = MainActivity.getColor(R.attr.breezy_text_title);
private int _imageLock = android.R.color.transparent;
Context context;
int layoutResID;
private boolean CHECK_LOCK = true;
private List<ChatItems> chatItems;
public ChatMessageAdapter(Context _context, int _textViewResourceId,
List<ChatItems> _chatItems) {
super(_context, _textViewResourceId, _chatItems);
context = _context;
layoutResID = _textViewResourceId;
chatItems = _chatItems;
}
@Override
public int getCount() {
if(chatItems != null || chatItems.size() > 0)
return chatItems.size();
return 0;
}
@Override
public ChatItems getItem(int index) {
return this.chatItems.get(index);
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
final MessageItemHolder holder;
View row = convertView;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.chat_message_item, parent, false);
holder = new MessageItemHolder();
holder.linerChatLayout = (LinearLayout) row.findViewById(R.id.linerChatLayout);
holder.linerChatLayoutTwo = (LinearLayout) row.findViewById(R.id.linerChatLayoutTwo);
holder.imageLock = (ImageView) row.findViewById(R.id.imageLock);
holder.linerProfileImage = (LinearLayout) row.findViewById(R.id.linerProfileImage);
holder.chatName = (TextView) row.findViewById(R.id.chatName);
holder.chatMessage = (TextView) row.findViewById(R.id.chatMessage);
holder.lastTimeMessage = (TextView) row.findViewById(R.id.lastTimeMessage);
row.setTag(holder);
}
else {
holder = (MessageItemHolder)row.getTag();
}
final ChatItems items = getItem(position);
if (items.getOrientation()){
resource = R.drawable.message_cloud_left;
gravity = Gravity.LEFT;
if (items.getLock()) {
resource = R.drawable.message_cloud_left_lock;
messageColor = android.R.color.transparent;
_imageLock = R.drawable.lock_massage;
}
else {
messageColor = MainActivity.getColor(R.attr.breezy_text_title);
_imageLock = android.R.color.transparent;
}
}
else {
resource = R.drawable.message_cloud_right;
gravity = Gravity.RIGHT;
if (items.getLock()) {
resource = R.drawable.message_cloud_right_lock;
messageColor = android.R.color.transparent;
_imageLock = R.drawable.lock_massage;
}
else {
messageColor = MainActivity.getColor(R.attr.breezy_text_title);
_imageLock = android.R.color.transparent;
}
}
[b] holder.linerChatLayoutTwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animation closeLock = null;
closeLock = AnimationUtils.loadAnimation(context,
R.anim.message_delock);
holder.linerChatLayoutTwo.startAnimation(closeLock);
holder.imageLock.startAnimation(closeLock);
if (CHECK_LOCK) {
resource = items.getOrientation() ?
R.drawable.message_cloud_left : R.drawable.message_cloud_right;
messageColor = MainActivity.getColor(R.attr.breezy_text_title);
_imageLock = android.R.color.transparent;
Toast.makeText(context, "TEST TEST", Toast.LENGTH_SHORT).show();
}
else {
resource = items.getOrientation() ? R.drawable.message_cloud_left_lock :
R.drawable.message_cloud_right_lock;
messageColor = android.R.color.transparent;
_imageLock = R.drawable.lock_massage;
Toast.makeText(context, "TEST", Toast.LENGTH_SHORT).show();
}
holder.chatMessage.setTextColor(messageColor);
holder.linerChatLayoutTwo.setBackgroundResource(resource);
holder.imageLock.setImageResource(_imageLock);
CHECK_LOCK = !CHECK_LOCK;
}
});[/b]
holder.chatName.setText(items.getChatName());
holder.chatMessage.setText(items.getChatMessage());
holder.lastTimeMessage.setText(items.getLastTimeMessage());
holder.imageLock.setImageResource(_imageLock);
holder.linerChatLayoutTwo.setBackgroundResource(resource);
holder.chatMessage.setTextColor(messageColor);
holder.linerChatLayout.setGravity(gravity);
holder.lastTimeMessage.setGravity(gravity);
holder.linerChatLayoutTwo.setGravity(gravity);
holder.chatMessage.setGravity(gravity);
holder.linerProfileImage.setGravity(gravity);
//Animation addMessage = null;
//addMessage = AnimationUtils.loadAnimation(context,
// R.anim.profile_slide_down_1);
//row.startAnimation(addMessage);
return row;
}
private static class MessageItemHolder {
private ImageView imageLock;
private LinearLayout linerChatLayout;
private LinearLayout linerChatLayoutTwo;
private LinearLayout linerProfileImage;
private TextView chatName, chatMessage, lastTimeMessage;
}
}