Using android inflate outside of onCreateView

I have a snippet. In my Create application, I set up my inflatable as follows.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    v= inflater.inflate(R.layout.dashboard_two, container, false);  
    getActivity().getActionBar().setTitle("NV Technologies");
    new HttpRequestFaultList().execute();
    return v;
}   

      

I want to change the display of the splitter. Different layout outside of onCreate view. I tried:

v= inflater.inflate(R.layout.custom_dash, container, false);

      

custom_dash.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:id="@+id/testFrag"
  android:layout_height="match_parent"
  android:orientation="vertical" >

   <ImageView
      android:id="@+id/imageView1"
      android:layout_width="269dp"
      android:layout_height="387dp"
      android:layout_gravity="center"
      android:scaleType="centerInside"
      android:src="@drawable/message" />

</LinearLayout>

      

But since it is outside of onCreate, there is no inflator or container. I want to change the layout in the catch block, and if there is a catch, the layout should change. Is it possible to change the layout in a Fragment but outside of the onCreate method? And How

+3


source to share


1 answer


You can bloat inside your fragment like this:

FrameLayout container = (FrameLayout) getActivity().findViewById(R.id.fragment_container);
LayoutInflater.from(getActivity())
        .inflate(R.layout.custom_dash, container, false);

      



If that doesn't work for you (for some reason), you can always save references to both the inflatable element and the container in your method onCreateView

and use them every time:

private LayoutInflater mInflater;
private ViewGroup mContainer;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mInflater = inflater;
    mContainer = container;
    v = inflater.inflate(R.layout.dashboard_two, container, false);  
    getActivity().getActionBar().setTitle("NV Technologies");
    new HttpRequestFaultList().execute();
    return v;
}

public View someOtherMethod() {
    mInflater.inflate(R.layout.custom_dash, mContainer, false);
}

      

+6


source







All Articles