Android : How to create FragmentStatePager with multiple custom fragment?

on Thursday, September 4, 2014


I'm creating a FragmentStatePager that include a fragment list (ArrayList). But the exception's saying "The specified child already has a parent. You must call removeView() on the child's parent first". Please check my codes.


Here is 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>


Following is the adapter.



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


I make some fragments for testing and put them in a FragmentList and simply initiate the adapter.



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


When I run , it seems works when swiping left to right. But the problem become when I swipe back (right to left). It is saying "The specified child has a parent. You must call removeView() on the child's parent first". I don't know where to remove view and I don't want to do it. Because when the user click submit, I need to get every data from each fragment user typed. That's why I can't remove any view. Do you have any better way to do so?


0 comments:

Post a Comment