0

Let's say following is my fragment.

public class FieldsFragment extends Fragment {

    LinearLayout linearLayout;
    ScrollView scrollView;
    ViewGroup rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        rootView = (ViewGroup) inflater.inflate(
                   R.layout.single_field_fragment ,null);
        scrollView = (ScrollView) rootView.findViewById(R.id.single_field_scrollView);
        return rootView;
    }

    public void setLinearLayout(LinearLayout linearLayout){
        scrollView.removeAllViews();
        scrollView.addView(linearLayout);
    }

    public LinearLayout getLinearLayout(){
        return linearLayout;
    }

}

What I want to do is ...

ArrayList<FieldsFragment> fragmentList = new ArrayList<FieldsFragment>();
fragment = new FieldsFragment();
fragment.setLinearLayout(lL);
fragmentList.add(fragment);

just want to parse my linear layouts to the fragment from other activity in order to use in FragmentStatePagerAdapter...

the problem is I can't setLinearLayout(myLayout1) without initiating the scrollView (findViewByID).

My work flow is so simple... (1) to pull linearLayout's from server.. (2) set them to each FieldsFragment and add them to ArrayList (3) and use that ArrayList to put FragmentStatePagerAdapter. The purpose is to slide all linearlayout got from server.

mkj
  • 2,761
  • 5
  • 24
  • 28
Zin Win Htet
  • 2,448
  • 4
  • 32
  • 54

1 Answers1

0

I got a solution and it's quite simple. Following is the way I put linearlayout to fragment and it works.

public class MyFragment extends Fragment {

ScrollView scrollView;
private LinearLayout linearLayout;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    ViewGroup rootView = (ViewGroup) inflater.inflate(
           R.layout.my_fragment , container, false);
    scrollView = (ScrollView) rootView.findViewById(R.id.svMyFragment);
    scrollView.addView(linearLayout);
    return rootView;
}

public static final MyFragment newInstance(LinearLayout linear_Layout){
    MyFragment myfragment = new MyFragment();
    myfragment.setLinearLayout(linear_Layout);
    return myfragment; 
}

public void setLinearLayout(LinearLayout linearLayout) {
    this.linearLayout = linearLayout;
}

}

In FragmentActivity class, instantiate like this

        MyFragment fooFragment = MyFragment.newInstance(myLinearLayout);
Zin Win Htet
  • 2,448
  • 4
  • 32
  • 54