Fragment复用(精华版)

一、在AFragment的Xml和AFragment代码中:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.asus.xiangmu_01.fragment.AFragment">
    <LinearLayout
        android:orientation="horizontal"
        android:background="#f60"
        android:layout_width="match_parent"
        android:layout_height="40dp">
    <android.support.design.widget.TabLayout
        android:id="@+id/mTabLayout"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content">

    </android.support.design.widget.TabLayout>
    <Button
        android:id="@+id/mTianJia"
        android:background="@drawable/ic_add"
        android:layout_width="40dp"
        android:layout_height="40dp" />
</LinearLayout>


<android.support.v4.view.ViewPager
android:id="@+id/mViewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content">


</android.support.v4.view.ViewPager>
</LinearLayout>
public class AFragment extends Fragment {


    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private Button mTianJia ;
    private ArrayList<Fragment> mFragment = new ArrayList<>() ;
    private ArrayList<String> mString = new ArrayList<>() ;
    private String[] aa = new String[]{
            "http://c.m.163.com/nc/article/headline/T1348647909107/0-20.html",
            "http://c.m.163.com/nc/article/list/T1348648756099/0-20.html",
            "http://c.m.163.com/nc/article/list/T1348648141035/0-20.html",
            "http://c.m.163.com/nc/article/list/T1348649079062/0-20.html",
            "http://c.m.163.com/nc/article/list/T1348649079062/0-20.html"};
    private MyPagerAdapter myPagerAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_a, container, false);
        initView(inflate);
        return inflate;
    }
    @Override
    public void onStart() {
        super.onStart();
        //注册EventBus
        EventBus.getDefault().register(this);
    }
    //接收EventBus传来信息的方法,
    @Subscribe(threadMode = ThreadMode.MAIN ,sticky = true)
    public  void  tobList(List<String> list){
        //  Log.e( "tobList: ", list.toString());
        mString.addAll(list);
        mTabLayout.setupWithViewPager(mViewPager);
        myPagerAdapter.notifyDataSetChanged();
    }
    @Override
    public void onStop() {
        super.onStop();
        //接触注册EventBus  节省空间
        EventBus.getDefault().unregister(this);
    }

    protected void initView(View inflate) {
        initData() ;
        mTabLayout = (TabLayout) inflate.findViewById(R.id.mTabLayout);
        mViewPager = (ViewPager) inflate.findViewById(R.id.mViewPager);
        mTianJia = (Button) inflate.findViewById(R.id.mTianJia);
        mTianJia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getContext(), TianJiaActivity.class));
            }
        });
        for (int i = 0; i <mString.size() ; i++) {
            OneFragment oneFragment = new OneFragment();
            //传值
            Bundle bundle1 = new Bundle();
            bundle1.putString("爱你哒2", aa[i]);
            oneFragment.setArguments(bundle1);

            mFragment.add(oneFragment);
            //复用Fragment

        }
        myPagerAdapter = new MyPagerAdapter(getChildFragmentManager(), mFragment, mString);
        mViewPager.setAdapter(myPagerAdapter);
        mTabLayout.setupWithViewPager(mViewPager);


    }
    private void initData() {
        mString.add("头条");
        mString.add("科技");
        mString.add("财经");
        mString.add("军事");
        mString.add("体育");

    }
}

二、在复用的OneFragment的Xml和代码中:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.asus.xiangmu_01.fragment.OneFragment">

    <!-- TODO: Update blank fragment layout -->
    <android.support.v7.widget.RecyclerView
        android:id="@+id/mRecyShouYe"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </android.support.v7.widget.RecyclerView>

</LinearLayout>
public class OneFragment extends BaseFragment<LoginPresenter, LoginModel> implements LoginContract.View {
    
    private List<ShouYeBean.T1348647909107Bean> list;
    private RecyclerView mRecyShouYe;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_one;
    }

    @Override
    protected void initView(View inflate) {
        Bundle arguments = getArguments();
        mRecyShouYe = (RecyclerView) inflate.findViewById(R.id.mRecyShouYe);
        mRecyShouYe.setLayoutManager(new LinearLayoutManager(getContext()));
        mPersenter.LoginShouYePre(arguments.getString("爱你哒2"));
    }

    @Override
    public void loginShouYe(ShouYeBean string) {
        list = string.getT1348647909107();
        MyShouYeAdapter myShouYeAdapter = new MyShouYeAdapter(R.layout.shouye, list);
        mRecyShouYe.setAdapter(myShouYeAdapter);
        myShouYeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(getContext(), WebActivity.class);
                intent.putExtra("list",list.get(position));
                startActivity(intent);
            }
        });
    }

    @Override
    public void loginTuPian(TuPianBean string) {

    }

    @Override
    public void loginShiPin(ShiPinBean string) {

    }
}

三、在实体类里添加:

@SerializedName(value = "T1348647909107",alternate = {"T1348648756099","T1348648141035","T1348649079062","T1399700447917"})

猜你喜欢

转载自blog.csdn.net/qq_42749901/article/details/81327817