Fragment Intermediate(III): Creating a activity that alternate between fragments onclick of respective button

别说谁变了你拦得住时间么 提交于 2019-12-02 04:17:48

Just have the FrameLayout and EditText in activity_main.xml. FrameLayout is a container (ViewGroup) to which you add or replace fragments. Your TableLayout can be in fragment layout.

You are adding/replacing fragment programatically so what is the need to have the same in xml.

Have this in activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <EditText
        android:id="@+id/input"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <FrameLayout
        android:id="@+id/container" // id is container
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/button1"
        android:layout_below="@+id/input"
      >
    </FrameLayout>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/input"
        android:onClick="selectFrag"
        android:text="Button1" />

    <Button
        android:id="@+id/button2"
        android:onClick="selectFrag"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/input"
        android:layout_alignParentBottom="true"
        android:text="Button2" />

</RelativeLayout>

Then in MainActivity

   public class MainActivity extends Activity {
    Fragment fr; 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    public void selectFrag(View view) {

        if(view == findViewById(R.id.button2)) {
            fr = new FragmentTwo();

        }
        else {
            fr = new FragmentOne();
        }
        FragmentManager fm = getFragmentManager();
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        fragmentTransaction.replace(R.id.container, fr);
        fragmentTransaction.commit();
   }
}

FragmentOne

public class FragmentOne extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) 
    {
        View view = inflater.inflate(R.layout.frag1, container, false);
        TextView warcraft=  (TextView) view.findViewById(R.id.moargold);
        EditText moargold = (EditText) getActivity().findViewById(R.id.input);
        Double vespenegas = Double.parseDouble(moargold.getText().toString());

        warcraft.setText(String.valueOf(vespenegas));
        Toast toast = Toast.makeText(getActivity(),String.valueOf(vespenegas) , Toast.LENGTH_SHORT);
        toast.show();
        return view;
    }
}

frag1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

             <TableLayout
                android:id="@+id/TableLayout01"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:stretchColumns="1" >

                <TableRow
                    android:id="@+id/tableRow4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >

                    <TextView
                        android:id="@+id/dis_moargold"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Gold Count:" />

                    <TextView
                        android:id="@+id/moargold"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="TextView" />
                </TableRow>
 </TableLayout>
</LinearLayout>

FragmentTwo

public class FragmentTwo extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) 
    {
        View view = inflater.inflate(R.layout.frag2, container, false);
        EditText input = (EditText) getActivity().findViewById(R.id.input);



        TableLayout tl=(TableLayout) view.findViewById(R.id.TableLayout01);


        TableRow tr = new TableRow(getActivity());
        tr.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

        TextView textview1 = new TextView(getActivity());
        textview1.setText("happy");
        tr.addView(textview1);

        TextView textview2 = new TextView(getActivity());
        textview2.setText("unhappy");
//###############To insert text from editview to table
//      Double buygas = Double.parseDouble(input.getText().toString());
//        textview2.setText(new Double(buygas).toString());
        tr.addView(textview2);

        tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        return view;
    }
}

frag2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

             <TableLayout
                android:id="@+id/TableLayout01"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:stretchColumns="1" >

                <TableRow
                    android:id="@+id/tableRow4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >

                    <TextView
                        android:id="@+id/dis_moargold"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Gold Count:" />

                    <TextView
                        android:id="@+id/moargold"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="TextView" />
                </TableRow>
 </TableLayout>
</LinearLayout>

Snap

While you add your Fragments dynamically, you keep your FragmentTwo inside your layout. Try to remove this in your layout:

<fragment
    android:name="com.example.sample.FragmentTwo"
    android:id="@+id/fragment_place"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>  

Also, in your code, you add dynamically a Fragment and with replace method, you set the container as id fragment_place:

fragmentTransaction.replace(R.id.fragment_place, fr);  

However, this id is using by your fragment (FragmentTwo, the first piece of code above) and you cannot replace a fragment which is not added dynamically. You need to host your fragment inside a FrameLayout but yours has no id, try to add this:

<FrameLayout
    android:id="@+id/fragment_place"     /// this line to create and id
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >  

Then, you will be able to host, add and replace your fragments into your framelayout as you want.

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