Android 子片段未正确还原
我认为我的问题相当难,因为我最后的一些问题没有得到答案。我正在使用Android 子片段未正确还原,android,android-fragments,Android,Android Fragments,我认为我的问题相当难,因为我最后的一些问题没有得到答案。我正在使用Fragment,并为他们维护一个backstack。一切都很好。我在的帮助下解决了backstack任务 唯一的问题是-有一个嵌套片段包含一个子片段。当我启动它时,一切正常。但是当我从backstack恢复它时,列表视图会附加上一个列表项。我使用了notifyDataSetChanged,但没有任何效果 这是嵌套的片段代码: package me.kaidul.uhunt; import java.io.IOException
Fragment
,并为他们维护一个backstack
。一切都很好。我在的帮助下解决了backstack
任务
唯一的问题是-有一个嵌套片段
包含一个子片段
。当我启动它时,一切正常。但是当我从backstack
恢复它时,列表视图
会附加上一个列表项
。我使用了notifyDataSetChanged
,但没有任何效果
这是嵌套的片段代码:
package me.kaidul.uhunt;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import me.kaidul.uhunt.ChaptersListFragment.OnChapterSelectListener;
import me.kaidul.uhunt.SubChaptersListFragment.OnSubChapterSelectListener;
import com.devspark.progressfragment.SherlockProgressFragment;
import com.google.gson.stream.JsonReader;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class CompetitiveProgramming extends SherlockProgressFragment implements
OnChapterSelectListener, OnSubChapterSelectListener {
View mContentView;
public static List<Chapter> chapterList = new ArrayList<Chapter>();
private ProcessTask processTask = null;
Fragment chapterFragment = new ChaptersListFragment();
Fragment subChapterFragment = new SubChaptersListFragment();
Fragment subSubChapterFragment = new SubSubChaptersListFragment();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRetainInstance(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mContentView = inflater.inflate(
R.layout.competitive_programming_exercise, container, false);
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setContentShown(false);
setContentView(mContentView);
processTask = new ProcessTask();
processTask.execute();
}
protected class ProcessTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
InputStream inputStream = null;
try {
inputStream = getSherlockActivity().getAssets().open(
CommonUtils.FILE_COMPETITIVE_PROGRAMMING_3);
JsonReader reader = new JsonReader(new InputStreamReader(
inputStream));
reader.beginArray(); // array #1
while (reader.hasNext()) {
String chapterTitle = null;
List<SubChapter> subList = new ArrayList<SubChapter>();
reader.beginObject(); // object #2
while (reader.hasNext()) {
reader.skipValue();
chapterTitle = reader.nextString();
reader.skipValue();
reader.beginArray(); // array #3
while (reader.hasNext()) {
String subChapterTitle = null;
List<SubSubChapter> subSubList = new ArrayList<SubSubChapter>();
reader.beginObject(); // object #4
while (reader.hasNext()) {
reader.skipValue();
subChapterTitle = reader.nextString();
reader.skipValue();
reader.beginArray(); // array #5
while (reader.hasNext()) {
reader.beginArray(); // array #6
String subSubChapterTitle = reader
.nextString(); // sub-sub-category
// title
List<ProblemList> problemsList = new ArrayList<ProblemList>();
while (reader.hasNext()) {
int signedProblemID = reader.nextInt(); // problemNo
String title = reader.nextString();
if (signedProblemID < 0)
problemsList.add(new ProblemList(
Math.abs(signedProblemID),
title, true));
else
problemsList.add(new ProblemList(
signedProblemID, title,
false));
}
reader.endArray(); // array #6
subSubList.add(new SubSubChapter(
subSubChapterTitle, problemsList));
}
reader.endArray(); // array #5
}
reader.endObject(); // object #4
subList.add(new SubChapter(subChapterTitle,
subSubList));
}
reader.endArray(); // array #3
}
reader.endObject(); // object #2
chapterList.add(new Chapter(chapterTitle, subList));
}
reader.endArray(); // array #1
reader.close();
} catch (IOException e) {
// nothing
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
// nothing
}
}
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
FragmentTransaction transaction = getChildFragmentManager()
.beginTransaction();
if (mContentView.findViewById(R.id.fragment_container) != null) {
transaction.replace(R.id.fragment_container,
new ChaptersListFragment());
} else {
transaction.replace(R.id.category_fragment, chapterFragment);
transaction.replace(R.id.sub_category_fragment,
subChapterFragment);
transaction.replace(R.id.sub_sub_category_fragment,
subSubChapterFragment);
}
transaction.commit();
setContentShown(true);
}
}
static protected class Chapter {
String chapterTitle;
List<SubChapter> subchapterList;
public Chapter(String chapterTitle, List<SubChapter> subchapterList) {
this.chapterTitle = chapterTitle;
this.subchapterList = subchapterList;
}
}
static protected class SubChapter {
String subChapterTitle;
List<SubSubChapter> subsubchapterList;
public SubChapter(String subChapterTitle,
List<SubSubChapter> subsubchapterList) {
this.subChapterTitle = subChapterTitle;
this.subsubchapterList = subsubchapterList;
}
}
static protected class SubSubChapter {
String subSubChapterTitle;
List<ProblemList> problemList;
public SubSubChapter(String subSubChapterTitle,
List<ProblemList> problemList) {
this.subSubChapterTitle = subSubChapterTitle;
this.problemList = problemList;
}
}
static public class ProblemList {
Integer problemNo;
String problemTitle;
boolean isStarred;
public ProblemList(Integer problemNo, String problemTitle,
boolean isStarred) {
this.problemNo = problemNo;
this.isStarred = isStarred;
this.problemTitle = problemTitle;
}
}
@Override
public void onChapterSelected(int position) {
SubChaptersListFragment subChaptersListFrag = (SubChaptersListFragment) getChildFragmentManager()
.findFragmentById(R.id.sub_category_fragment);
if (subChaptersListFrag != null) {
subChaptersListFrag.updateList(position);
} else {
subChapterFragment = new SubChaptersListFragment();
Bundle args = new Bundle();
args.putInt(SubChaptersListFragment.CHAPTER_POSITION, position);
subChapterFragment.setArguments(args);
FragmentTransaction transaction = getChildFragmentManager()
.beginTransaction();
transaction.replace(R.id.fragment_container, subChapterFragment);
transaction.commit();
}
}
@Override
public void onSubChapterSelected(int prev, int position) {
SubSubChaptersListFragment subSubChaptersListFrag = (SubSubChaptersListFragment) getChildFragmentManager()
.findFragmentById(R.id.sub_sub_category_fragment);
if (subSubChaptersListFrag != null) {
subSubChaptersListFrag.updateList(prev, position);
} else {
subSubChapterFragment = new SubSubChaptersListFragment();
Bundle args = new Bundle();
args.putIntArray(SubSubChaptersListFragment.POSITIONS, new int[] {
prev, position });
subSubChapterFragment.setArguments(args);
FragmentTransaction transaction = getChildFragmentManager()
.beginTransaction();
transaction.replace(R.id.fragment_container, subSubChapterFragment);
transaction.commit();
}
}
@Override
public void onStop() {
super.onStop();
if (processTask != null
&& processTask.getStatus() != AsyncTask.Status.FINISHED) {
processTask.cancel(true);
}
}
@Override
public void onDestroyView() {
try {
FragmentTransaction transaction = getChildFragmentManager()
.beginTransaction();
transaction.remove(chapterFragment);
transaction.commit();
} catch (Exception e) {
}
super.onDestroyView();
}
@Override
public void onDetach() {
super.onDetach();
try {
Field childFragmentManager = Fragment.class
.getDeclaredField("mChildFragmentManager");
childFragmentManager.setAccessible(true);
childFragmentManager.set(this, null);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
package me.kaidul.uhunt;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.lang.reflect.Field;
导入java.util.ArrayList;
导入java.util.List;
导入me.kaidul.uhunt.ChaptersListFragment.OnChapterSelectListener;
导入me.kaidul.uhunt.SubChaptersListFragment.OnSubChapterSelectListener;
导入com.devspark.progressfragment.SherlockProgressFragment;
导入com.google.gson.stream.JsonReader;
导入android.support.v4.app.Fragment;
导入android.support.v4.app.FragmentTransaction;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
公共类CompetitiveProgramming扩展了SherlockProgressFragment实现
OnChapterSelectListener,OnSubChapterSelectListener{
视图mContentView;
public static List chapterList=new ArrayList();
private ProcessTask ProcessTask=null;
Fragment chapterFragment=新的ChaptersListFragment();
Fragment subChapterFragment=新的SubChaptersListFragment();
Fragment SubChapterFragment=新的SubChapterSlistFragment();
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
此.setRetainInstance(真);
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
mContentView=充气机。充气(
R.layout.competitive_programming_exercise,container,false);
返回super.onCreateView(充气机、容器、savedInstanceState);
}
@凌驾
已创建ActivityState上的公共无效(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
SetContentShowed(假);
setContentView(mContentView);
processTask=新的processTask();
processTask.execute();
}
受保护的类ProcessTask扩展了AsyncTask{
@凌驾
受保护的Void doInBackground(Void…参数){
InputStream InputStream=null;
试一试{
inputStream=getSherlockActivity().getAssets().open(
CommonUtils.FILE\u COMPETITIVE\u PROGRAMMING\u 3);
JsonReader=新的JsonReader(新的InputStreamReader(
输入流);
reader.beginArray();//数组#1
while(reader.hasNext()){
字符串chapterTitle=null;
列表子列表=新的ArrayList();
reader.beginObject();//对象#2
while(reader.hasNext()){
reader.skipValue();
chapterTitle=reader.nextString();
reader.skipValue();
reader.beginArray();//数组#3
while(reader.hasNext()){
字符串subChapterTitle=null;
List subpublist=new ArrayList();
reader.beginObject();//对象#4
while(reader.hasNext()){
reader.skipValue();
subChapterTitle=reader.nextString();
reader.skipValue();
reader.beginArray();//数组#5
while(reader.hasNext()){
reader.beginArray();//数组#6
String subchapterTitle=读取器
.nextString();//子类别
//头衔
List problemsList=new ArrayList();
while(reader.hasNext()){
int signedProblemID=reader.nextInt();//problemNo
字符串title=reader.nextString();
if(signedProblemID<0)
problemsList.add(新问题列表(
Math.abs(签名问题ID),
头衔,真实姓名);
其他的
problemsList.add(新问题列表(
签名的问题ID、标题、,
假),;
}
reader.endArray();//数组#6
subpublist.add(新的SubSubChapter(
子章节标题,问题列表);
}
reader.endArray();//数组#5
}
reader.endObject();//对象#4
子列表添加(新的分章)(分章标题,
次出版商);
}
reader.endArray();//数组#3
}
reader.endObject();//obj
package me.kaidul.uhunt;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.actionbarsherlock.app.SherlockListFragment;
public class ChaptersListFragment extends SherlockListFragment {
OnChapterSelectListener mCallback;
ArrayAdapter<String> mAdapter;
List<String> items = new ArrayList<String>();
public interface OnChapterSelectListener {
public void onChapterSelected(int position);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState == null) {
items.clear();
for (int i = 0; i < CompetitiveProgramming.chapterList.size(); i++) {
items.add(CompetitiveProgramming.chapterList.get(i).chapterTitle);
}
mAdapter = new ArrayAdapter<String>(getSherlockActivity(),
R.layout.list_layout, items);
setListAdapter(mAdapter);
}
}
@Override
public void onStart() {
super.onStart();
if (getFragmentManager().findFragmentById(R.id.sub_category_fragment) != null) {
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mCallback = (OnChapterSelectListener) getParentFragment();
} catch (ClassCastException e) {
throw new ClassCastException(getParentFragment().toString()
+ " must implement OnChapterSelectListener");
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
mCallback.onChapterSelected(position);
getListView().setItemChecked(position, true);
}
}
chapterList = new ArrayList<Chapter>();