保留列表视图滚动位置

时间:2014-06-13 11:44:13

标签: android android-fragments

我有一个列表片段和一个细节片段。滚动列表视图的内容后,单击任何项​​目,它将重定向到详细信息片段。从详细信息片段返回时,我的列表片段应保留滚动位置。我试过这个:但是它重新加载了列表片段:

public void onSaveInstanceState(Bundle outState) {
        // TODO Auto-generated method stub

        mFirstVisiblePosition = mListview.getFirstVisiblePosition();
        // get offset
        View v = mListview.getChildAt(0);
        mTopOffset = (v == null) ? 0 : v.getTop();
        outState.putInt("currentScrollPOsition", mTopOffset);
        super.onSaveInstanceState(outState);
    }

我正在保存列表滚动位置,然后在创建我正在检索它:

@Override
    public void onCreate(Bundle savedInstanceState) {

        if (savedInstanceState != null) {
            mTopOffset = savedInstanceState.getInt("currentScrollPOsition");
            mListview.setSelectionFromTop(mFirstVisiblePosition, mTopOffset);
        }
        super.onCreate(savedInstanceState);
    }

这是我的适配器:

public class Adapter extends ArrayAdapter<Item> {

    private LayoutInflater mInflater;

    public RssAdapter(Context context, List<Item> itemsList) {

        super(context, R.layout.news_items, itemsList);

        mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    private static class Placeholder {
        TextView Textview1, Textview2;

    }

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

        Placeholder holder;

        if (convertView == null) {
            holder = new Placeholder();
            convertView = mInflater.inflate(R.layout.news_items, parent, false);
            holder.Textview1 = (TextView) convertView
                    .findViewById(R.id.title);
            holder.Textview2 = (TextView) convertView
                    .findViewById(R.id.date);

            convertView.setTag(holder);
        } else {
            holder = (Placeholder) convertView.getTag();
        }

        Item currentItem = getItem(position);
        if (currentItem != null) {
            String title = currentItem.title;
            holder.mTitleTextview.setText(title);

            String date = currentItem.pubDate;
            holder.mDateTextview.setText(date);

        } else {
            Toast.makeText(getContext(), "No item to display",
                    Toast.LENGTH_LONG).show();
        }
        return convertView;
    }

}

但它不起作用。任何人都可以帮助我..提前致谢..

1 个答案:

答案 0 :(得分:0)

保存第一个可见位置和顶部偏移:

public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    mFirstVisiblePosition = mListview.getFirstVisiblePosition();
    // get offset
    View v = mListview.getChildAt(0);
    mTopOffset = (v == null) ? 0 : v.getTop();
    outState.putInt("firstVisiblePosition",mFirstVisiblePosition); // Save first visible position
    outState.putInt("currentScrollPOsition", mTopOffset);
}

然后,恢复第一个可见位置:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if (savedInstanceState != null) {
        mTopOffset = savedInstanceState.getInt("currentScrollPOsition");
        mFirstVisiblePosition = savedInstanceState.getInt("firstVisiblePosition"); // restore first visible position
        mListview.setSelectionFromTop(mFirstVisiblePosition, mTopOffset);
    }
}