现在我正在进行聊天功能。我正在扩展 ArrayAdapter 。仅借助这些ArrayAdapter,我可以在聊天中实现许多功能。所以我无法更改此ArrayAdapter。
但我必须使用另一个Adapter(BaseSwipeListAdapter.java)来向左滑动(启用或不启用)功能。
ChatActivity.java:(在此课程中使用ChatAdapter):
static public class ChatAdapter extends ArrayAdapter<ChatMessage> {
private final List<ChatMessage> chatMessages;
private Activity context;
String loggedUserId, loggedUserName;
public ChatAdapter(Activity context, int resource, List<ChatMessage> chatMessages) {
super(context, resource, chatMessages);
this.context = context;
this.chatMessages = chatMessages;
}
}
我需要在ChatActivity.java中使用下面的代码来实现我的结果。但我无法扩展BaseSwipeListAdapter。因为我已经在使用ArrayAdapter。
@Override
public boolean getSwipEnableByPosition(int position) {
if(position % 2 == 0){
return false;
}
return true;
}
BaseSwipeListAdapter.java:
import android.widget.BaseAdapter;
public abstract class BaseSwipListAdapter extends BaseAdapter {
public boolean getSwipEnableByPosition(int position){
return true;
}
}
SwipeMenuAdapter.java :(图书馆类):
public class SwipeMenuAdapter implements WrapperListAdapter,
OnSwipeItemClickListener {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
SwipeMenuLayout layout = null;
if (mAdapter instanceof BaseSwipListAdapter) {
boolean swipEnable = (((BaseSwipListAdapter) mAdapter).getSwipEnableByPosition(position));
layout.setSwipEnable(swipEnable); --->This line is used to enable and disable the swipe left in listview.I need to use this method
}
return layout;
}
如何在ChatActivity.class中调用此方法setSwipEnable(swipEnable)
我认为单一类中不可能使用两个适配器。有没有其他方法可以解决这个问题。
答案 0 :(得分:0)
如果列表视图大小大于0,您可以通过条件进行检查。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
SwipeMenuLayout layout = null;
if (mAdapter instanceof BaseSwipListAdapter) {
boolean swipEnable = (((BaseSwipListAdapter) mAdapter).getSwipEnableByPosition(position));
if( listviewsize > 0)
layout.setSwipEnable(swipEnable); --->This line is used to enable and disable the swipe left in listview.I need to use this method
else
layout.setSwipEnable(swipDisable);
}
return layout;
}
您可以通过
查看listviewsize@Override
public int getCount() {
return listview.size();
}
答案 1 :(得分:0)
你可以在库中对它进行一些修改。在你的ChatMessage bean类中,你可以为你想要使刷卡选项启用或不启用布尔变量。之后在SwipeMenuListView类中设置适配器时你需要一些在那里写了一个ChatMessage类的ArrayList。在那里写了mTouchPosition代码之后,你可以检查刷卡布尔值为true或false的ChatMessage类的相同位置。如果swipe boolean为false,则在mTouchPosition赋值后返回false,否则处理所有触摸事件。
答案 2 :(得分:0)
从ArrayAdapter
实施BaseAdapter
功能并不困难。
static public class ChatAdapter extends BaseAdapter { // or BaseSwipListAdapter if you like
private final List<ChatMessage> chatMessages;
private Activity context;
private int layoutId;
String loggedUserId, loggedUserName;
public ChatAdapter(Activity context, int resource, List<ChatMessage> chatMessages) {
super(context, resource, chatMessages);
this.context = context;
layoutId = resource;
this.chatMessages = chatMessages;
}
@Override
public int getCount() {
return chatMessages == null ? 0 : chatMessages.size();
}
@Override
public Object getItem(int position) {
return chatMessages == null ? null : chatMessages.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(layoutId, parent, false);
}
// assuming the view is a TextView
((TextView) view).setText(getItem(position).toString());
return view;
}
public void add(ChatMessage chatMessage) {
chatMessages.add(chatMessage);
notifyDataSetChanged();
}
public void addAll(Collection<? extends ChatMessage> col) {
chatMessages.addAll(col);
notifyDataSetChanged();
}
public void clear() {
chatMessages.clear();
notifyDataSetChanged();
}
}