ListFragment를 다른 위치에서 돌아 왔을 때 정확한 위치로 되돌리려 고하고 있지만 작동하지 않습니다. 내 코드 내 listView
의 모든 인스턴스는 빨간색이되고 다음과 같은 오류가 반환됩니다ListView가 정확한 스크롤 위치로 돌아 오지 않습니다.
심볼 '목록보기'나는
ListView listView = getListView();
를 사용할 때 이런 일이 왜 난 정말 모르는
를 확인할 수 없습니다. 난이 페이지에서 솔루션을 다음과 같은 시도 :
- FutureStudio.io - How to Save and Restore the Scroll Position and State of a Android ListView
- 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>
자바
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);
}
}
}
FragmentHerbs 클래스 변수로 ListView를 목록보기를 추가하고 onCreateView에서 초기화 - 목록보기 = 위에서 getListView(); – garmax1
@ garmax1 형식을 사용하여 제안 사항을 표시하십시오 – MacaronLover