【问题标题】:ListView not returning to exact scroll positionListView 没有返回到确切的滚动位置
【发布时间】:2017-04-26 19:19:01
【问题描述】:

当我从其他地方返回时,我试图让我的 ListFragment 返回到完全相同的位置,但它不起作用。我的代码中listView 的所有实例都变为红色并返回以下错误:

无法解析符号“listView”

当我使用ListView listView = getListView(); 时,我真的不知道为什么会发生这种情况。我什至尝试遵循这些页面中的解决方案:

  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

我发现遵循人们的建议非常令人困惑,因为他们的建议各不相同。

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

【问题讨论】:

  • 将ListView listView添加为FragmentHerbs类变量并在onCreateView中初始化-listView = getListView();
  • @garmax1 请用格式显示您的建议

标签: java android xml listview android-listfragment


【解决方案1】:
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);
       }
   }

}

【讨论】:

  • 还是不行。每当我返回它时,它都会一直显示列表视图的顶部。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-01
  • 2011-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多