How to use Fragments in Android

前端 未结 4 1058
我寻月下人不归
我寻月下人不归 2020-11-28 13:49

EDIT ::: Please refer to my answer below...

Question:::

I am very new to using Fragments in Android and I am completely messed up.

I am just trying t

相关标签:
4条回答
  • 2020-11-28 14:13

    Okie... Finally found a solution. Probably, it wasn't much of a change.

    Check out the code below...

    activity_main.XML

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    
        <fragment
            android:id="@+id/fragment_content_1"
            android:name="com.example.fragmentexample.Fragment_1"
            android:layout_width="0dip"
            android:layout_weight="0.50"
            android:layout_height="fill_parent" >
        </fragment>
    
        <fragment
            android:id="@+id/fragment_content_2"
            android:name="com.example.fragmentexample.Fragment_2"
            android:layout_width="0dip"
            android:layout_weight="0.50"
            android:layout_height="fill_parent" >
    
            <!-- Preview: layout=@layout/fragment_basic -->
        </fragment>
    
    </LinearLayout>
    

    The layouts of fragment_fragment_1 and fragment_fragment_2 remain the same.

    Fragment_1.Java

    public class Fragment_1 extends Fragment{
    
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
                // TODO Auto-generated method stub      
    
                View view = inflater.inflate(R.layout.fragment_fragment_1, container, false);
    
                final EditText edtxtPersonName_Fragment = (EditText) view.findViewById(R.id.edtxtPersonName);
                Button btnSayHi_Fragment = (Button) view.findViewById(R.id.btnSayHi);
    
                btnSayHi_Fragment.setOnClickListener(new OnClickListener() {
    
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
    
                        String name = edtxtPersonName_Fragment.getText().toString();
    
                        FragmentManager fm = getFragmentManager();
                        Fragment_2 f2 = (Fragment_2) fm.findFragmentById(R.id.fragment_content_2);
    
                        if(f2 != null && f2.isInLayout())
                        {
                            f2.setName(name);
                        }
    
                        Activity activity = getActivity();
    
                        if(activity != null)
                        {
                            Toast.makeText(activity, "Say&ing Hi in Progress...", Toast.LENGTH_LONG).show();
                        }
                    }
                });
    
                return view;
    
    
            }
    
        }
    

    Fragment_2.Java

    public class Fragment_2 extends Fragment{
    
        View view;
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // TODO Auto-generated method stub
    
            view = inflater.inflate(R.layout.fragment_fragment_2, container, false);        
            return view;
        }
    
        public void setName(String name)
        {
            TextView txtName = (TextView) view.findViewById(R.id.txtViewResult);
            txtName.setText("Hi " + name);
        }
    
    }
    

    Here is the ScreenShot...

    enter image description here

    0 讨论(0)
  • 2020-11-28 14:21

    Please edit your activity_main.XML AS as follows.

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" >
    
    <fragment
        android:id="@+id/fragment_content_1"
        android:name="com.example.fragmentexample.fragment_fragment_1"
        class="com.example.fragmentexample.fragment_fragment_1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </fragment>
    
    <fragment
        android:id="@+id/fragment_content_2"
        android:name="com.example.fragmentexample.fragment_fragment_2"
        class="com.example.fragmentexample.fragment_fragment_2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    
        <!-- Preview: layout=@layout/fragment_basic -->
    </fragment>
    </LinearLayout>
    
    0 讨论(0)
  • 2020-11-28 14:25

    This is the classic case of Communication between two Fragments.
    But let's cover more basic things first.

    Skeleton of the Fragments

    FirstFragment

    EditText
    Button

    public class FirstFragment extends Fragment{
    
          private EditText editext;
          private Button button;
    
          public static FirstFragment getInstance(){  
               return new FirstFragment();
          }
    
          public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle bundle){
               View view = inflater.inflate(R.Layout.fragment1,parent,false);
               editext = (EditText)view.findViewById(R.id.editText)
               button = (Button)view.findViewById(R.id.button)
               return view;               
    
          }
    
    }
    

    SecondFragment

    TextView

    public class SecondFragment extends Fragment{
    
          private TextView textView;
    
          public static SecondFragment getInstance(){  
               return new SecondFragment();
          }
    
          public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle bundle){
               View view = inflater.inflate(R.Layout.fragment2,parent,false);
               textView = (TextView)view.findViewById(R.id.editText)
               return view;               
    
          }
    
          public void showMessage(String msg)
          {
               textView.setText(msg)
          }
    
    }
    

    How to createInstance of Fragments

    FirstFragment fragmne1 = FirstFragment.getInstance();
    SecondFragment fragmne1 = SecondFragment.getInstance();
    

    How to Add them in the Activity?

    getSupportFragmentManager()
             .beginTransaction()
             .add(R.id.placeholder1,fragment1,"TAG1")
             .add(R.id.placeholder2,fragment2,"TAG2")
             .commit();
    

    Now the communication Part

    Create an Interface

    public interface FragmentListener{
         public void buttonClicked();
    }
    

    Implement this interface in the Activity and override the method

    public MyActivity extends AppcompatActivity implements MyFragmentListener{
    
         @Override
         public void buttonClicked(){
              fragment2.showMessage(String msg);
         }
    
    }
    

    Send data from Fragment

    1. initialize the listener in the FirstFragment
    2. call interface method on Button click.

    The final structure of FirstFragment

    public class FirstFragment extends Fragment{
    
          private EditText editext;
          private Button button;
          private MyFragmentListener listener;
    
          public static FirstFragment getInstance(){  
               return new FirstFragment();
          }
    
          public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle bundle){
               View view = inflater.inflate(R.Layout.fragment1,parent,false);
               editext = (EditText)view.findViewById(R.id.editText)
               button = (Button)view.findViewById(R.id.button)
    
               // button click method 
               listener.buttonClicked(editText.getText().toString());
    
               return view;               
    
          }
    
          @Override 
          public void onAttach(Context context){
               listener = (MyFragmentListener)context;
          }
    
    }
    
    0 讨论(0)
  • 2020-11-28 14:27

    You need to add an empty public constructor to your Fragments, like it says in the stack trace:

    public class Fragment_2 extends Fragment{
    
        public Frament_2() {
            //BLAH!
        }
    
        // The rest of your code
    
    }
    
    0 讨论(0)
提交回复
热议问题