我正在使用BaseAdapter来填充ListView
但是当它更新时它会重置为新项目。我怎样才能保持现状。我搜索过并找到了一些代码,但它对我没用。
这是我的代码:
public void appendToMessageHistory(ArrayList username, ArrayList message) {
if (username != null && message != null) {
if(lv.getAdapter()==null){
adapter = new ChatListAdapter(this, name, messages);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();}
else{
//int i=lv.getFirstVisiblePosition();
adapter.updateData(this, name, messages);
adapter.notifyDataSetChanged();
//lv.smoothScrollToPosition(i);
}
}
}
和我的BaseAdapter:
public class ChatListAdapter extends BaseAdapter {
private Context context;
private ArrayList<String> names;
private ArrayList<String> messages;
static class ViewHolder {
public TextView name, message;
public ImageView image;
}
public ChatListAdapter(Context context, ArrayList<String> names, ArrayList<String> messages) {
this.context = context;
this.names = names;
this.messages = messages;
}
public void updateData(Context context, ArrayList<String> names, ArrayList<String> messages){
this.context = context;
this.names = names;
this.messages = messages;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = LayoutInflater.from(context);
rowView = inflater.inflate(R.layout.layout,
null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.name = (TextView) rowView.findViewById(R.id.name);
viewHolder.message=(TextView) rowView.findViewById(R.id.firstLine);
rowView.setTag(viewHolder);
}
ViewHolder holder = (ViewHolder) rowView.getTag();
String name = names.get(position);
String message = messages.get(position);
holder.message.setTextColor(context.getResources().getColor(this.getTextColorID()));
return rowView;
}
@Override
public int getCount() {
return names.size();
}
@Override
public Object getItem(int position) {
return names.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getViewTypeCount() {
// you have two different row types
return 2;
}
@Override
public int getItemViewType(int position) {
// You need to tell to adapter which row is needed at given position
return names.get(position).equals(own) ? 0 : 1;
}
}
我也在使用stackFromBottom = true
我已经使用了smoothScrollToPosition(),但是在重置listview之后它会回滚到位置,即使适配器更新,我也希望坚持当前位置。
答案 0 :(得分:0)
您是否尝试过setSelectionFromTop
方法?
在appendToMessageHistory方法的else
块中,尝试:
int index = lv.getFirstVisiblePosition();
View v = lv.getChildAt(0);
int top = (v == null) ? 0 : v.getTop();
adapter.updateData(this, name, messages);
adapter.notifyDataSetChanged();
lv.setSelectionFromTop(index, top);
可在此处找到对此的解释:Maintain/Save/Restore scroll position when returning to a ListView
答案 1 :(得分:0)
我解决了我的错误。
非常简单
它将notifyDataSetChanged()
函数绑定到特定条件
这是我的代码:
public void appendToMessageHistory(ArrayList username, ArrayList message) {
if (username != null && message != null) {
if(lv.getAdapter()==null){
adapter = new ChatListAdapter(this, name, messages);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
else{
int i = lv.getFirstVisiblePosition();
int last = lv.getLastVisiblePosition();
View v = lv.getChildAt(0);
int top = (v == null) ? 0 : v.getTop();
if (top < 0 && lv.getChildAt(1) != null) {
i++;
v = lv.getChildAt(1);
top = v.getTop();
}
adapter.updateData(this, name, messages);
if(last==messages.size()-2)
adapter.notifyDataSetChanged();
lv.setSelection(i);
lv.setSelectionFromTop(i, top);
}
}
}
如果它没有显示最后一个项目并且它是否显示最后一个项目,那么它不会保留在当前位置,那么它将通知。