簡體   English   中英

如何在片段中保存復雜列表視圖的滾動狀態

[英]How to save Scrolling state of a complex listview in a fragment

我在一個片段中使用文件資源管理器listview。

但沒有得到如何保存其滾動狀態。

我搜索了過去兩天的Stackoverflow法定人數和Google,但沒有得到任何結果。

我的項目支持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類別中,我呼吁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的開始顯示。

提前感謝:)

這里引用

回答伊恩

 // 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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM