如何在片段

时间:2015-08-27 18:53:22

标签: java android listview android-fragments android-listview

我在片段中使用文件资源管理器列表视图。

但没有得到如何保存其滚动状态。

我搜索了Stackoverflow quora和谷歌最近2天,但没有得到任何结果。

我的项目支持Api 14 +。

在我的片段资源管理器中,

我正在onCreateview和onclick这样

public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    ViewGroup root = (ViewGroup) inflater.inflate(R.layout.artist,container, false);

 //My coding stuff here

        lv=(ListView) root.findViewById(android.R.id.list);
        lv.setAdapter(adaptor);

        return root;
}

onclick如下: -

public void onActivityCreated(Bundle savedInstanceState) {
                super.onActivityCreated(savedInstanceState);
              lv=getListView();
            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                            public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
             gtuMcaBean1= gtuMcaBean.get(position);
                                 lv.setAdapter(adaptor);
}

在我的Fragmentactivity clas中,我调用onBackpressed

    @Override
     public void onBackPressed() {
        // super.onBackPressed();
         if(pager.getCurrentItem()==0)//0 is item no for explorer view
         {
//My coding stuff here
        lv.setadaptor(adaptor);
         }

在我的Mainactivity中我也有一个onbackressed,当我来一个活动片段视图

@Override
   public void onBackPressed() {
        // super.onBackPressed();
         if(pager.getCurrentItem()==0)//0 is item no for explorer view
         {
//My coding stuff here
        lv.setadaptor(adaptor);
         }

现在我想保存Listview的滚动状态,所以如果用户按下后退按钮,它应该显示上一个滚动视图,而不是从ListView的开始。

Thanx事先:)

1 个答案:

答案 0 :(得分:0)

摘自here

ian回答

 // save index and top position
int index = mList.getFirstVisiblePosition();
View v = mList.getChildAt(0);
int top = (v == null) ? 0 : (v.getTop() - mList.getPaddingTop());

// ...

// restore index and position
mList.setSelectionFromTop(index, top);
相关问题