简体   繁体   中英

ListView not returning to exact scroll position

I'm trying to get my ListFragment to return to the exact same position when I return to it from elsewhere but it's not working. All instances of listView within my code become red and the following error is returned:

Cannot resolve symbol 'listView'

I really don't know why this has happened when I used ListView listView = getListView(); . I even tried following the solutions from these pages:

  1. FutureStudio.io - How to Save and Restore the Scroll Position and State of a Android ListView
  2. Stack Overflow - Eugene Mymrin's answer - Maintain/Save/Restore scroll position when returning to a ListView

I'm finding it very confusing to follow people's suggestions as they do vary.

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/fragmentherbs">

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ListView>

</LinearLayout>

Java

public class FragmentHerbs extends ListFragment {

    public FragmentHerbs() {
        // Required empty constructor
    }

    public static FragmentHerbs newInstance() {
        return new FragmentHerbs();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_herbs, container, false);

        ListView listView = getListView();

        initialize();
        return view;
    }

    List<Herb> list = new ArrayList<>();
    private void initialize() {
        String[] items = getActivity().getResources().getStringArray(R.array.herb_names);
        for (int n = 0; n < items.length; n++){
            Herb herb = new Herb();
            herb.setID();
            herb.setName(items[n]);
            list.add(herb);
        }

        HerbsListAdapter mAdapter = new HerbsListAdapter(list, getActivity());
        setListAdapter(mAdapter);
    }


    @Override
    public void onPause() {
        // Save ListView state @ onPause
        Log.d(TAG, "saving listview state @ onPause");
        state = listView.onSaveInstanceState();
        super.onPause();
    }

    @Override
    public void onViewCreated(final View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // Restore previous state (including selected item index and scroll position)
        if(state != null) {
            Log.d(TAG, "trying to restore listview state..");
            listView.onRestoreInstanceState(state);
        }
    }
}
public class FragmentHerbs extends ListFragment {
   ListView listView;

   public FragmentHerbs() {
       // Required empty constructor
   }

   public static FragmentHerbs newInstance() {
       return new FragmentHerbs();
   }

   @Nullable
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       View view = inflater.inflate(R.layout.fragment_herbs, container, false);

       listView = getListView();
       initialize();
       return view;
   }

   List<Herb> list = new ArrayList<>();
   private void initialize() {
      String[] items = getActivity().getResources().getStringArray(R.array.herb_names);
      for (int n = 0; n < items.length; n++){
         Herb herb = new Herb();
         herb.setID();
         herb.setName(items[n]);
         list.add(herb);
      }

      HerbsListAdapter mAdapter = new HerbsListAdapter(list, getActivity());
      setListAdapter(mAdapter);
   }


   @Override
   public void onPause() {
       // Save ListView state @ onPause
       Log.d(TAG, "saving listview state @ onPause");
       state = listView.onSaveInstanceState();
       super.onPause();
   }

   @Override
   public void onViewCreated(final View view, Bundle savedInstanceState) {
       super.onViewCreated(view, savedInstanceState);

       // Restore previous state (including selected item index and scroll position)
       if(state != null) {
          Log.d(TAG, "trying to restore listview state..");
          listView.onRestoreInstanceState(state);
       }
   }

}

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