滚动时ListView自动移动到顶部

时间:2013-11-07 10:44:06

标签: android listview android-listview timer android-asynctask

我正在处理TCP socketListView,其中包含20个列表项。 从服务器我每5秒收到一些数据集,我需要在listview中显示。

我的问题是。

当我滚动列表并从服务器收到第二组数据时,我需要更新列表项,然后我的listview自动移至顶部。我需要阻止它。

我的代码正在更新我的列表视图。此代码位于AsyncTask的onPostExecute中。

protected void onPostExecute(ArrayList<User> result) {
    adb = new ArrayAdapter<User>(DefaultMarketWatch.this, R.layout.rssitemview, result) {
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View view = convertView;
            try {
                if (null == view) {
                    LayoutInflater vi = (LayoutInflater) DefaultMarketWatch.this
                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    view = vi.inflate(R.layout.rssitemview, null);
                }
                final User u = list.get(position);
                if (null != u) {
                    TextView title = (TextView) view.findViewById(R.id.symbol);
                    TextView ltp = (TextView) view.findViewById(R.id.ltp);
                    TextView high = (TextView) view.findViewById(R.id.high);
                    TextView low = (TextView) view.findViewById(R.id.low);
                    TextView persendBold = (TextView) view.findViewById(R.id.persent_bold);

                    persendBold.setText(u.getAskPrice());
                    ltp.setText(u.getLtp());
                    title.setText(u.getSymbol());
                    high.setText(u.getHigh());
                    low.setText(u.getLow());
                } 
            }
            catch (Exception e) {
                e.printStackTrace();
            }
            return view;
        }

    };

    if (adb != null) {
        lv.setAdapter(adb);
        adb.notifyDataSetChanged();
        int currentPos = lv.getFirstVisiblePosition();
        lv.setSelection(currentPos);
    }
    super.onPostExecute(result);
}

2 个答案:

答案 0 :(得分:1)

问题是:您更喜欢什么行为?如果列表项完全更改,则显示第一个元素是合理的。但是,如果您只想要与新数据相同的位置,您可以执行以下操作:

protected void onPostExecute(ArrayList<User> result) {
    int currentPos = lv.getFirstVisiblePosition();
    adb = new ArrayAdapter<User>(DefaultMarketWatch.this, R.layout.rssitemview, result) {
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View view = convertView;
            try {
                if (null == view) {
                    LayoutInflater vi = (LayoutInflater) DefaultMarketWatch.this
                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    view = vi.inflate(R.layout.rssitemview, null);
                }
                final User u = list.get(position);
                if (null != u) {
                    TextView title = (TextView) view.findViewById(R.id.symbol);
                    TextView ltp = (TextView) view.findViewById(R.id.ltp);
                    TextView high = (TextView) view.findViewById(R.id.high);
                    TextView low = (TextView) view.findViewById(R.id.low);
                    TextView persendBold = (TextView) view.findViewById(R.id.persent_bold);

                    persendBold.setText(u.getAskPrice());
                    ltp.setText(u.getLtp());
                    title.setText(u.getSymbol());
                    high.setText(u.getHigh());
                    low.setText(u.getLow());
                } 
            }
            catch (Exception e) {
                e.printStackTrace();
            }
            return view;
        }

    };

     //previous null check completely unnessesary, because you initialized it just before
    lv.setAdapter(adb);
    adb.notifyDataSetChanged();

    lv.setSelection(currentPos);

    super.onPostExecute(result);
}

答案 1 :(得分:0)

我认为问题在于您每次为adapter设置一个新的listView。我的解决方案是在运行时实现支持update列表的自定义数组适配器。

步骤1)定义自定义ArrayAdapter,如下所示:

public class MyArrayAdapter extends ArrayAdapter<User>
{
    ArrayList<User> list;

    public MyArrayAdapter (Context context, int textViewResourceId, ArrayList<User> result) {
       super (context, textViewResourceId, objects);
       this.list = result;
    }

    public void updateList(ArrayList<User> updatedList) {
        this.list = updatedList;
    }

    @Override
    public View getView (int position, View convertView, ViewGroup parent)
    {
        View view = convertView;
        try {
            if (null == view) {
                LayoutInflater vi = (LayoutInflater) DefaultMarketWatch.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = vi.inflate(R.layout.rssitemview, null);
            }
            final User u = list.get(position);
            if (null != u) {
                TextView title = (TextView) view.findViewById(R.id.symbol);
                TextView ltp = (TextView) view.findViewById(R.id.ltp);
                TextView high = (TextView) view.findViewById(R.id.high);
                TextView low = (TextView) view.findViewById(R.id.low);
                TextView persendBold = (TextView) view.findViewById(R.id.persent_bold);

                persendBold.setText(u.getAskPrice());
                ltp.setText(u.getLtp());
                title.setText(u.getSymbol());
                high.setText(u.getHigh());
                low.setText(u.getLow());
            } 
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return view;
    }
}

按以下方式初始化adb(在onCreate或启动AsyncTask之前的位置:

adb = new MyArrayAdapter(DefaultMarketWatch.this, R.layout.rssitemview, new ArrayList<User>());
lv.setAdapter(adb);
adb.notifyDataSetChanged();

您的帖子执行现在只告诉适配器列表已更新,并且调用通知数据集已更改。

protected void onPostExecute(ArrayList<User> result) {
    adb.updateList(result);
    adb.notifyDataSetChanged();
    super.onPostExecute(result);
}