ViewPager适配器用法

适配器

1、两种适配器的区别

FragmentPagerAdapter() fragment没有被销毁
FragmentStatePagerAdapter() fragment被销毁

2、FragmentManager 的获取在Activity中通过getSupportFragmentManager()方法获取 ,在fragment中通过getChildFragmentManager()方法获取

示例代码

常规写法

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.ArrayList;

public class FragmentAdapter extends FragmentPagerAdapter {
    private ArrayList list;
    public FragmentAdapter(FragmentManager fm,ArrayList list) {
        super(fm);
        //获取从activaty中传过来的fragment集合
         this.list = list;
    }
    @Override
    public Fragment getItem(int i) {
    //取出fragment并反回
        return list.get(i);
    }

    @Override
    public int getCount() {
        return list.size();
    }
}

正确写法

import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.SparseArray;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import turbosnail.cc.viewpager_tab.fragment.TabFragment;

public class TabFragmentAdapter extends FragmentPagerAdapter {
    private List mTitles = new ArrayList<>(Arrays.asList("微信", "通讯录", "发现", "我"));
    private SparseArray mFragments = new SparseArray<>();      //方便管理fragment对象
    public TabFragmentAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        /**
         * 错误写法
         * 这里很多人会去直接通过get()直接获取
         * 一口气new多个fragment对象
         * 屏幕旋转之后onCreate会重新执行getItem不会执行   FragmentPagerAdapter会用之前的Fragment对象
         * 通过TabFragment中的newInstance()方法去new一个对象参数 看你需要,我这里是演示所有就随意
         * 如果每个fragment不一样就需要自个判断去new对象  我也是小白
         */
        TabFragment tabFragment = TabFragment.newInstance(mTitles.get(i));
        return tabFragment;
    }

    @Override
    public int getCount() {
        return mTitles.size();
    }

    /**
     * 通过重写下面两个方法就可以实现界面上有几个fragment我们的mFragments就有几个而且一一对应
     * @param container
     * @param position
     * @return
     */
    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        TabFragment tabFragment = (TabFragment) super.instantiateItem(container, position);
        mFragments.put(position,tabFragment);
        return tabFragment;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        mFragments.remove(position);
        super.destroyItem(container, position, object);
    }
}

TabFragment类的代码

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.administrator.demolist.R;

public class TabFragment extends Fragment {
    private static final String BUNDLE_KEY_TITLE = "key_title";
    private TextView tvTitle;
    private String title;
	 //前面这接口就不用看了
    public static interface OnTitleClickListener {
        void onClickListener(String title);
    }

    private OnTitleClickListener mListener;

    public void setOnTitleClickListener(OnTitleClickListener listener) {
        mListener = listener;
    }

    public static TabFragment newInstance(String title) {
        Bundle bundle = new Bundle();
        bundle.putString(BUNDLE_KEY_TITLE, title);
        TabFragment tabFragment = new TabFragment();
        //通过setArguments()方法设置进fragment在通过getArguments()方法获取
        tabFragment.setArguments(bundle);
        return tabFragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //获取Bundle通过key取出值
        Bundle arguments = getArguments();
        if (arguments != null) {
            title = arguments.getString(BUNDLE_KEY_TITLE);
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_tab, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        tvTitle = view.findViewById(R.id.tv_title);
        tvTitle.setText(title);
//        tvTitle.setOnClickListener(new View.OnClickListener() {
////            @Override
////            public void onClick(View v) {
////                if (mListener != null) {
////                    mListener.onClickListener("微信 change");
////                }
////            }
////        });
    }

    /**
     * Activity和Fragment的通信
     *
     * @param title activity传过来的参数用来更新ui
     */
    public void changgeTitle(String title) {
        if (!isAdded()) {
            return;
        }
        tvTitle.setText(title);
    }
}

你可能感兴趣的:(android,ViewPager,适配器)