我正在创建一个包含片段列表(ArrayList)的FragmentStatePager。 但例外情况是“指定的孩子已经有父母。你必须首先在孩子的父母上调用removeView()”。 请检查我的代码。
这是 my_fragment.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" >
<ScrollView
android:id="@+id/svMyFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ScrollView>
</LinearLayout>
以下是适配器。
public class SlidePagerAdapter extends FragmentStatePagerAdapter {
private int noOfPages;
private List<MyFragment> myFragmentList;
public SlidePagerAdapter(android.support.v4.app.FragmentManager fm, ArrayList<MyFragment> fragmentList){
super(fm);
myFragmentList = fragmentList;
noOfPages = fragmentList.size();
}
@Override
public Fragment getItem(int position) {
return myFragmentList.get(position);
}
@Override
public int getCount() {
return noOfPages;
}}
我制作一些片段用于测试并将它们放入FragmentList中,然后简单地启动适配器。
public class PagerActivity extends FragmentActivity {
public static SlidePagerAdapter mPagerAdapter;
private ViewPager mPager;
int pageTotal;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.screen_slide);
ArrayList<MyFragment> fragmentList = getFooFragmentList();
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new SlidePagerAdapter(getSupportFragmentManager(), fragmentList);
mPager.setAdapter(mPagerAdapter);
pageTotal = mPagerAdapter.getCount();
mPager.setCurrentItem(0);
}
private ArrayList<MyFragment> getFooFragmentList() {
final ArrayList<MyFragment> fooFragmentList = new ArrayList<MyFragment>();
MyFragment fooFragment = MyFragment.newInstance(produceALayout("Inside first fragment"));
fooFragmentList.add(fooFragment);
MyFragment fooFragment2 = MyFragment.newInstance(produceALayout("Inside second fragment"));
fooFragmentList.add(fooFragment2);
LinearLayout linearLayout = new LinearLayout(this);
Button button = new Button(this);
button.setText("Submit");
button.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
MyFragment myFrag = null;
for(int i=0; i<fooFragmentList.size()-1; i++){
myFrag = fooFragmentList.get(i);
LinearLayout lL = myFrag.getLinearLayout();
for(int j=0; j<lL.getChildCount(); j++){
String name = lL.getChildAt(j).getClass().getName();
Log.i("class name : ", name);
}
}
}
});
linearLayout.addView(button);
MyFragment submitFragment = MyFragment.newInstance(linearLayout);
fooFragmentList.add(submitFragment);
return fooFragmentList;
}
private LinearLayout produceALayout(String label){
LinearLayout lL = new LinearLayout(this);
lL.setOrientation(LinearLayout.VERTICAL);
lL.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, 70);
TextView tv = new TextView(this);
tv.setLayoutParams(lp);
tv.setText(label);
EditText editText = new EditText(this);
editText.setLayoutParams(lp);
editText.setHint(label);
lL.addView(tv);
lL.addView(editText);
return lL;
}}
当我跑步时,从左向右滑动似乎有效。 但是当我向后滑动(从右到左)时,问题就出现了。 它是说“指定的孩子有父母。你必须首先在孩子的父母上调用removeView()”。我不知道在哪里删除视图,我不想这样做。 因为当用户单击提交时,我需要从每个片段用户输入每个数据。 这就是我无法删除任何视图的原因。你有更好的方法吗?