gpt4 book ai didi

java - 更新适配器后 ListView 失去位置 listView.setAdapter(new MyAdapter(...))

转载 作者:行者123 更新时间:2023-12-01 13:10:55 25 4
gpt4 key购买 nike

当 ListView 中的项目结束时,我上传新的,并在更新适配器后:

ListView lvMain = (ListView) findViewById(R.id.list);
boxAdapter = null;
boxAdapter = new BoxAdapter(this, products);
lvMain.setAdapter(boxAdapter);

但在此之后,元素被加载,但滚动位置位于顶部。即ListView的位置丢失了,再看一下全部的开头

如何解决?

BoxAdapter 代码:

public class BoxAdapter extends BaseAdapter {
private final Context ctx;
private final LayoutInflater lInflater;
private final ArrayList<ItemInfo> objects;
private final int loadCount = 10;
private int count = 10;
private String name, desc;

BoxAdapter(Context context, ArrayList<ItemInfo> products) {

this.ctx = context;
this.objects = products;
this.lInflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}



// кол-во элементов
@Override
public int getCount() {
//return objects.size();
return this.count;
}

// элемент по позиции
@Override
public ItemInfo getItem(int position) {
return objects.get(position);
}

// id по позиции
@Override
public long getItemId(int position) {
return position;
}

public void loadAdditionalItems() {
this.count += this.loadCount;
if (this.count > this.objects.size()) {
this.count = this.objects.size();
}
notifyDataSetChanged();
}



@Override
public View getView(int position, View convertView, ViewGroup parent) {

View view = convertView;
view = lInflater.inflate(R.layout.item, parent, false);

ItemInfo p = getItem(position);
TextView desc_id = (TextView) view.findViewById(R.id.desc);

if (p.username.contains("null"))
{
name = "Автор: Неизвестен";
}
else
{
name = "Автор: " + p.username;
}

if(!p.description.contains("null"))
{
desc = p.description.replaceAll("<br />", "");
desc = desc.replaceAll("&quot;", "");
}
else
{
desc = "";
desc_id.setVisibility(View.GONE);
}


((TextView) view.findViewById(R.id.name)).setText(name);
((TextView) view.findViewById(R.id.desc)).setText(desc);
return view;
}

}

P.S setOnScrollListener 代码:

lvMain.setOnScrollListener(new OnScrollListener()
{

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
ListView lvMain = (ListView) findViewById(R.id.list);
if(firstVisibleItem + visibleItemCount >= totalItemCount) {
boxAdapter.loadAdditionalItems();
loading = false;
}
if (!loading && (lvMain.getLastVisiblePosition() + 10) >= (60))
{
new LoadLastestPost().execute();
loading = true;
}
}

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}

});

最佳答案

最好的解决方案是在 boxAdapter 中创建一个 setProducts 方法,然后调用 boxAdapter.notifyDataSetChanged()。例如:

boxAdapter.setProducts(products);
boxAdapter.notifyDataSetChanged();

如果实现此方法,则无需多次调用lvMain.setAdapter(boxAdapter)

要将 setProducts() 方法添加到您的适配器:

public BoxAdapter extends BaseAdapter {

Context mContext;
ArrayList<ItemInfo> objects;

public BoxAdapter(Context context, ArrayList<ItemInfo> products) {
mContext = context;
objects = products;
}

public View getView(int position, View convertView, ViewGroup parent) {
// inflate and adjust view
}

public int getCount() {
return objects.size();
}

public Object getItem(int position) {
return objects.get(position);
}

public void setProducts(ArrayList<ItemInfo> newData) {
objects = newData;
}
}

此外,我不会使用 count 变量。我将只使用 ArrayList 中的 size 方法。我会完全删除 count

关于java - 更新适配器后 ListView 失去位置 listView.setAdapter(new MyAdapter(...)),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22877964/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com