package safeluck.drive.evaluation.fragment.shop; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; import androidx.annotation.Nullable; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import me.yokeyword.fragmentation.SupportFragment; import me.yokeyword.fragmentation.anim.DefaultNoAnimator; import me.yokeyword.fragmentation.anim.FragmentAnimator; import safeluck.drive.evaluation.R; import safeluck.drive.evaluation.fragment.BaseSettingFragment; import safeluck.drive.evaluation.fragment.rulefragments.FragmentSetting0; import safeluck.drive.evaluation.fragment.rulefragments.FragmentSetting1; /** * Created by YoKeyword on 16/2/9. */ public class ContentFragment extends SupportFragment { private static final String ARG_MENU = "arg_menu"; private static final String ARG_POS ="arg_position"; private TextView mTvContent; private Button mBtnNext; private int currPos = 0; private String mMenu; public static ContentFragment newInstance(String menu,int pos) { Bundle args = new Bundle(); args.putString(ARG_MENU, menu); args.putInt(ARG_POS,pos); ContentFragment fragment = new ContentFragment(); fragment.setArguments(args); return fragment; } @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); Bundle args = getArguments(); if (args != null) { mMenu = args.getString(ARG_MENU); currPos =args.getInt(ARG_POS); } //运用反射来实现各个Fragmetn的初始化 String clsName= "safeluck.drive.evaluation.fragment.rulefragments.FragmentSetting"; try { Class cls = Class.forName(clsName+currPos); Method method= cls.getMethod("newInstance",String.class); SupportFragment invoke = (SupportFragment) method.invoke(null, String.valueOf(currPos)); loadRootFragment(R.id.fl_content_container, invoke,false,false ); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } @Override public FragmentAnimator onCreateFragmentAnimator() { return new DefaultNoAnimator(); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_content, container, false); initView(view); return view; } private void initView(View view) { // mTvContent = (TextView) view.findViewById(R.id.tv_content); } @Override public boolean onBackPressedSupport() { // ContentFragment是ShopFragment的栈顶子Fragment,可以在此处理返回按键事件 return super.onBackPressedSupport(); } }