viewpage配合fragment和tablayout的使用

一  主页面上

<android.support.design.widget.TabLayout

    android:id="@+id/tabs"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    app:tabIndicatorColor="@color/grjf_blue"

    app:tabSelectedTextColor="@color/grjf_blue"

    app:tabIndicatorHeight="1dp"/>



<android.support.v4.view.ViewPager

    android:id="@+id/vp_jf_detail"

    android:layout_width="match_parent"

    android:layout_height="match_parent"/>

 

备注:

//选中前颜色和选中后颜色

app:tabTextColor="@color/no_select_text_green"
app:tabSelectedTextColor="@color/white"

 

 

二  两个fragment的layout的xml布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:background="#ffffff">



    <android.support.v7.widget.RecyclerView

    android:id="@+id/recycler_grad"

    android:layout_width="match_parent"

    android:layout_height="match_parent" />



LinearLayout>

 

三  activity中的控件声明

 

引用:

import android.support.v4.app.Fragment;

import android.support.v4.view.ViewPager;

 

 

//viewpagetabs部分

private TabLayout tabs;

private List titles = new ArrayList<>();

private ViewPager vp;

private FragAdapter adapter;

private List fragments;
 
tabs = (TabLayout) findViewById(R.id.tabs);

titles.add("核查详情");

titles.add("积分详情");

//构造适配器

fragments = new ArrayList<>();

fragments.add(new FragmentJfListView(police_id,savedInstanceState));

fragments.add(new FragmentJfDetailView());

adapter = new FragAdapter(getSupportFragmentManager(),titles,fragments);

//设定适配器

vp = (ViewPager) findViewById(R.id.vp_jf_detail);

vp.setAdapter(adapter);

//TabLayout设置ViewPager

tabs.setupWithViewPager(vp);

tabs.setTabsFromPagerAdapter(adapter);

tabs.setTabTextColors(ColorStateList.valueOf(getColor(R.color.gray)));
 
 
  fragmentadapter
 
import android.support.v4.app.Fragment;

import android.support.v4.app.FragmentManager;

import android.support.v4.app.FragmentPagerAdapter;



import java.util.List;



/**

 * Created by miao on 2016/10/24.

 */



public class FragAdapter extends FragmentPagerAdapter {



    private List titles;

    private List mFragments;



    public FragAdapter(FragmentManager fm,List titles, List list) {

        super(fm);

        this.titles = titles;

        mFragments = list;

    }





    @Override

    public Fragment getItem(int position) {

        return mFragments.get(position);

    }



    @Override

    public int getCount() {

        return mFragments.size();

    }



    @Override

    public CharSequence getPageTitle(int position) {

        return titles.get(position);

    }

}
 

五  fragment的类

import android.support.v4.app.Fragment;

 

/**

 * Created by miao on 2016/10/24.

 */



public class FragmentJfDetailView extends Fragment {

    @Nullable

    @Override

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_jf_detail,container,false);

       //所有的操作都在这其间完成 

        return view;

    }



}

 

六  存放viewpager的activity必须继承AppCompatActivity

或FragmentActivity

 

七  fragment中涉及到数据传递的时候,构造函数有要求。不然打包的时候会报错(为了防止传值的时候数据丢失)

 

 

 

private String mjjh;
public static Fragment newInstance(String mjjh){

    FragmentJfDetailView instance = new FragmentJfDetailView();

    instance.mjjh = mjjh;

    return instance;

}
 
 
public static Fragment newInstance(Uri uri)
    {
        if (uri == null)
        {
            return null;
        }
 
        ImageFragment fragment = new ImageFragment();
        Bundle        args     = new Bundle();
        args.putParcelable("uri", uri);
        fragment.setArguments(args);
        return fragment;
    }

 

 

vp.setCurrentItem(0);是显示特定的fragment

 

PagerAdapter 有四个重要方法:
(1) void destroyItem(ViewGroup container, int position, Object object): 销毁
(2)Object instantiateItem(ViewGroup container, int position) : 添加View
(3) int getCount() : 获得View总是
(4) boolean isViewFromObject(View view, Object obj) : View比较

 

 

监听器: setOnPageChangeListener :页面发生改变
(1) void onPageSelected(int position) :View进行切换 :position 新的页面位置

这个debug的时候会有延迟,先是0,再是1.不建议使用
(2) void onPageScrolled(int arg0, float arg1, int arg2) :页面正在滚动的时候[滑动偏移信息],回调

与顶部或者tab的联动最好放在这里
(3) void onPageScrollStateChanged(int arg0) :页面状态发生变化的时候,回调

 

 

ListView 因为使用相同的View,所以采用复用。
而ViewPage ,是基于View ,子类有许多不同,所以要注销.

public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
View view= imageVisws.get(position % images.length);
container.removeView(view); //
重复加入会引起异常
container.addView(view);
return view;

导致异常:java.lang.IllegalStateException The specified child already has a parent. You must call removeView()
原因: 被加入的View 有父类, 所以添加到另外一个集合中之前,必须清除父类.

你可能感兴趣的:(IT,android)