java android listview更新后位置变动

文章标签: android,android-listview
2014-11-3 21:09:28     人阅读    

android开发项目中是用了 ListView,当我更新ListView的时候出现了位置变动,又被初始化到最顶部了

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


滚动条滚动到了最顶部,没更新前位置被拉倒了中间,

r如何才能让滚动条在更新后不一起刷新?

 

P.S setOnScrollListener code:
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 code:
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;

}

}
}



处理方法

最好的方法是创建一个 setProducts 方法,然后在你的适配器中调用 boxAdapter.notifyDataSetChanged().刷新,参考代码

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

 

如果你实现了该方法,就不会重新调用lvMain.setAdapter ,所以你需要添加 setProducts() 方法

public BoxAdapter extends BaseAdapter {



    Context mContext;
 
    ArrayList objects;



    public BoxAdapter(Context context, ArrayList 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 newData) {
 
        objects = newData;
 
    }
}
}


原文地址:http://www.itmmd.com/201411/114.html
该文章由 萌萌的IT人 整理发布,转载须标明出处。

android 重新加载listview数据   上一篇
下一篇   我实现了一下简答的集合widget,我想让listview在我的widget 中展示,但是 listView一直处于加载状态
精彩回复
我要追加问题,请求站长解决!
姓名:       

《程序员app》专门为程序员量身定做!