适配器更新时如何保持listview的滚动位置

时间:2015-07-23 17:22:50

标签: android listview android-adapter android-scrollview android-scroll

我使用listview绑定适配器,首先我获取10条记录,然后当我滚动结束时再次列出listview中添加的项目,它完美无缺。

但是当我在滚动结束后添加项目时,那时项目成功添加,然后列表视图位置设置为顶部。我想将这个位置设置为新添加的项目。

这是我的代码:

onscroll功能:

public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {               
    new items().execute();
}

异步功能:

class items extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        ...
    }

    protected String doInBackground(String... args) {


        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("id", String.valueOf(id)));

        JSONObject json = jParser.makeHttpRequest(url, "GET",
                params);


        try {


            for (int i = 0; i < products.length(); i++) {

                JSONObject c = products.getJSONObject(i);

                // Storing each json item in variable
                String id = c.getString(TAG_PID);
                String name = c.getString(TAG_NAME);

                Product item = new Product(name, imageurl);
                product_name.add(item);

            }

        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return null;
}

protected void onPostExecute(String file_url) {
    // dismiss the dialog after getting all products
    pDialog.dismiss();


    adapter = new CategoryAdapter(context, product_name);

    category_linear.setAdapter(adapter);

}

Baseadapter:

public class CategoryAdapter extends BaseAdapter {

    Context context;

    // protected List<String> m_List;
    protected List<Product> rowItems;

    public CategoryAdapter(Context context, List<Product> items) {
        this.context = context;
        this.rowItems = items;
    }

    public void addItemAll(List<Product> item) {
        //
        rowItems.addAll(item);
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return rowItems.size();
    }

    @Override
    public Object getItem(int position) {
        return rowItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

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

        // TODO Auto-generated method stub

        if (convertView == null) {
            convertView = LayoutInflater.from(getActivity()).inflate(
                    R.layout.row, null);

        }

        Product rowItem = (Product) getItem(position);

        TextView text = ViewHolderPattern.get(convertView, R.id.item_name);

        text.setText(rowItem.getTitle());

        return convertView;
    }
}

现在,如何在添加新项目时设置滚动的位置。

目前,当添加新项目时,它将显示在列表视图的顶部。

但是我想把位置设置为新项目。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

每次在AsyncTask中执行代码时,都在创建适配器的新实例。即你的onPostExecute函数。而不是做

adapter = new CategoryAdapter(context, product_name);
category_linear.setAdapter(adapter);

你应该这样做

adapter.addItemAll(product_name);
adapter.notifyDataSetChanged();

同时写下以下内容

adapter = new CategoryAdapter(context, new List<Product>());
category_linear.setAdapter(adapter);

在您实例化ListView以使其正常工作之后。

数据将在您的列表中更新,您的位置将保持不变。

干杯!

相关问题