having a condition to fragment adding

左心房为你撑大大i 提交于 2019-12-10 11:50:25

问题


for the last 4 hours ive been trying to understand how to do it. to be clear, i need to add a fragment under a certain condition and for my purpose the condition can be either: a. if the parent's id matches to what i seek. b. if the fragment's id matches to what i seek.

i tried to put the condition inside the fragment itself:

public class BlockFrag extends Fragment{
SharedPreferences sp;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    sp =  getActivity().getSharedPreferences("myOptions", Context.MODE_PRIVATE);
    int id = container.getId();//didn't work(i think the container is null for some reason
    switch (id)
    {
        default: return inflater.inflate(R.layout.nothing, container, false);

        case (R.id.redFrame): {
            if (sp.getBoolean("redBlock", true)) {
                return inflater.inflate(R.layout.block_frag, container, false);
            }
        }
        case (R.id.greenFrame): {
            if (sp.getBoolean("greenBlock", true)) {
                return inflater.inflate(R.layout.block_frag, container, false);

        }

i also tried to get the condition programatically but i cant understand how to prevent the fragment to automatically generate when i call the layout it's placed.

this is the layout it's placed for example

<FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/redFrame"
        android:layout_weight="0.33333">


        <fragment
            android:name="com.example.dor.myapplication.BlockFrag"
            android:id="@+id/blockRed"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:layout="@layout/block_frag"/>

this is the fragment's layout if it matters...

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">

<ImageView
    android:layout_width="118dp"
    android:layout_height="match_parent"
    android:id="@+id/blockFrag"
    android:src="@drawable/block"
    android:layout_weight="0.33333"
    android:layout_above="@+id/linearLayout"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_gravity="left"
    android:scaleType="fitXY"/>

thank you for helping, any kind of way to do this will help me.


回答1:


I did something similar earlier, I had to decide based on a value from PickHeaderFragment which Fragment to show inside my root_frame_body, here's how I achieved it:

My Parent or Host Fragment:

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

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:background="#000"
        android:layout_height="wrap_content"
        android:id="@+id/root_frame_header" />

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:background="#000"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:id="@+id/root_frame_body" />

</LinearLayout>

Inside my Host Fragment:

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

    super.onCreate(savedInstanceState);

    View rootView = inflater.inflate(R.layout.fragment_pick, container, false);
    headerFragment = PickHeaderFragment.newInstance("");
    getChildFragmentManager().beginTransaction().replace(R.id.root_frame_header, headerFragment).commit();
}

My PickHeaderFragment has an OnPickHeaderFragmentInteractionListener Interface implemented which looks like this:

public interface OnPickHeaderFragmentInteractionListener {
    public void onFragmentInteraction(int res);
}

I ensure my Host Fragment will implement this interface by adding it during onAttach (inside PickHeaderFragment) like so:

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    try {
        Fragment f = getParentFragment();
        mListener = (OnPickHeaderFragmentInteractionListener) getParentFragment();
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString() + " must implement OnFragmentInteractionListener");
    }
}

Now, when the enter Button inside my PickHeaderFragment is clicked I call this event to let my Host Fragment know to change the root_frame_body:

Inside my Host Fragment I have:

@Override
public void onFragmentInteraction(int res) {
    if (res == R.id.btnEnter) {
        getChildFragmentManager().beginTransaction().replace(R.id.root_frame_body, new PickByItemBodyFragment()).commit();;
    }
}

I am now able to change the layout (or which Fragment is displayed) in my Host Fragment after an event occurs inside a child Fragment.

Note: In my example I have nested Fragments, but you could just as easily have several Fragments within an Activity.

In summary I would recommend you follow a similar pattern of having a host Activity or Fragment and ensuring you have a FrameLayout added to your view to allow you to inflate whatever Fragment you need.

An added benefit to doing it this way is that you can segregate your code into separate Fragments also.




回答2:


Leigh thank you for your answer but i don't know if you didn't understand my question or i didn't understand your answer...

anyway, i solved my problem through programatically adding the fragment in r=the onCreate of the activity and inserting the adding to an "if"

    BlockFrag rb = new BlockFrag();
    FragmentManager fragmentManager = getFragmentManager ();//declaring Fragment Manager
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction ();// declaring Fragment Transaction

    if(condition)
    {
        editor.putBoolean("redBlock", true);
        fragmentTransaction.add(R.id.redFrame, rb);//adding the fragment
    }

needless to say i didn't declare the fragment in my activity layout and didn't perform any changes in the fragment class or fragment xml layout.



来源:https://stackoverflow.com/questions/28079669/having-a-condition-to-fragment-adding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!