欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

android之利用RadioGroup切换fragment

程序员文章站 2022-05-14 21:06:58
...

android之利用RadioGroup切换fragment

activity中:
RadioGroup gh;
FragmentManager fm;
FragmentTransaction ft;

gh = (RadioGroup) this.findViewById(R.id.grouphead);
fm = getSupportFragmentManager();

 	gh.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
       		@Override
       		public void onCheckedChanged(RadioGroup radioGroup, int i) {
				ft = fm.beginTransaction();
                if(libF != null)ft.hide(libF);
                if(dynamicF != null)ft.hide(dynamicF);
                if(mineF != null)ft.hide(mineF);
                if(recommendF != null)ft.hide(recommendF);
                if(i == R.id.libBtn){
                    if(libF == null){
                        libF = new libFragment();
                        ft.add(R.id.lay,libF,"lib_");
                    }
                    ft.show(libF);
                }else if(i == R.id.dynamicBtn){
                    if(dynamicF == null){
                        dynamicF = new dynamicFragment();
                        ft.add(R.id.lay,dynamicF,"dynamic_");
                    }
                    ft.show(dynamicF);
                }else if(i == R.id.recommendBtn){
                    if(recommendF == null){
                        recommendF = new recommendFragment();
                        ft.add(R.id.lay,recommendF,"recommend_");
                    }
                    ft.show(recommendF);
                }else if(i == R.id.mineBtn){
                    if(mineF == null){
                        mineF = new mineFragment();
                        ft.add(R.id.lay,mineF,"mine_");
                    }
                    ft.show(mineF);
                }
                ft.commit();
            }
        });
以其中一个fragment为例:

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link dynamicFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link dynamicFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class dynamicFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    public dynamicFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment dynamicFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static dynamicFragment newInstance(String param1, String param2) {
        dynamicFragment fragment = new dynamicFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_dynamic, container, false);
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}

页面效果:
android之利用RadioGroup切换fragment

相关标签: android java