Java ListView未返回到确切的滚动位置

Java ListView未返回到确切的滚动位置,java,android,xml,listview,android-listfragment,Java,Android,Xml,Listview,Android Listfragment,我试图让我的ListFragment在从其他地方返回时返回到完全相同的位置,但它不起作用。我的代码中的所有listView实例都变为红色,并返回以下错误: 无法解析符号“listView” 当我使用ListView-ListView=getListView()时,我真的不知道为什么会发生这种情况。我甚至尝试了以下这些页面中的解决方案: 我发现跟随人们的建议是非常令人困惑的,因为他们的建议千差万别 XML <?xml version="1.0" encoding="utf-8"?>

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

无法解析符号“listView”

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

  • 我发现跟随人们的建议是非常令人困惑的,因为他们的建议千差万别

    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);
            }
        }
    }
    
    公共类片段{
    公共碎片(){
    //必需的空构造函数
    }
    公共静态碎片实例(){
    返回新的碎片();
    }
    @可空
    @凌驾
    CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
    视图=充气机。充气(R.layout.fragment_,容器,错误);
    ListView ListView=getListView();
    初始化();
    返回视图;
    }
    列表=新的ArrayList();
    私有void初始化(){
    String[]items=getActivity().getResources().getStringArray(R.array.herb_名称);
    对于(int n=0;n
    公共类片段{
    列表视图列表视图;
    公共碎片(){
    //必需的空构造函数
    }
    公共静态碎片实例(){
    返回新的碎片();
    }
    @可空
    @凌驾
    CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
    视图=充气机。充气(R.layout.fragment_,容器,错误);
    listView=getListView();
    初始化();
    返回视图;
    }
    列表=新的ArrayList();
    私有void初始化(){
    String[]items=getActivity().getResources().getStringArray(R.array.herb_名称);
    对于(int n=0;n

    }

    添加ListView ListView作为类变量,并在onCreateView-ListView=getListView()中初始化它@garmax1请显示您的建议,但格式仍然不起作用。每当我返回列表视图时,它总是显示列表视图的顶部。
    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);
           }
       }