简体   繁体   中英

How to save Scrolling state of a complex listview in a fragment

I am using a file explorer listview in a fragment.

But not getting how to save its scrolled state .

I searched Stackoverflow quora and google for last 2 days but didnt got any result.

My project support Api 14 +.

In my Fragment Explorer,

I am making onCreateview and onclick like this

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 is as follows:-

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

In my Fragmentactivity clas i call 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);
         }

In my Mainactivity also i have an onbackressed which works when i come to fragment view frm an activity

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

Now i want to save the scroll state of Listview so if user presses back button it should display previous scroll view not from starting of ListView.

Thanx In advance :)

Referred from here

Answered by 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM