Android开发经验谈Android开发Android开发

TabLayout+ViewPager联合使用

2017-08-25  本文已影响830人  Wocus

使用起来很方便,接下来介绍使用步骤

1.依赖注入该文件

    根据自己工程的编译版本选择依赖哪个版本的design库
    例如工程compile sdk 为23,则依赖
    compile 'com.android.support:design:23.2.0'

2.xml布局代码

<FrameLayout 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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="youju.com.shopnert.fragment.MessageFragment">

      //上拉刷新下拉加载
    <com.scwang.smartrefresh.layout.SmartRefreshLayout
        android:id="@+id/refreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <android.support.design.widget.TabLayout
                android:id="@+id/tabs_msg"
                android:layout_height="50dp"
                android:layout_width="match_parent"
                app:tabIndicatorColor="@color/colorPrimary"
                app:tabSelectedTextColor="@color/colorPrimary"
                app:tabTextColor="#000000"/>

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

            </android.support.v4.view.ViewPager>


        </LinearLayout>


    </com.scwang.smartrefresh.layout.SmartRefreshLayout>

</FrameLayout>

3.Fragment文件

package youju.com.shopnert.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

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

import youju.com.shopnert.R;

public class MessageFragment extends Fragment {

    private List<String> list_title=new ArrayList<>();
    private List<View> list_view=new ArrayList<>();

    private TabLayout tabLayout;
    private ViewPager viewPager;
    private RefreshLayout refreshLayout;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_message,container,false);
        tabLayout= (TabLayout) view.findViewById(R.id.tabs_msg);
        viewPager= (ViewPager) view.findViewById(R.id.vp_msg);
        refreshLayout= (RefreshLayout) view.findViewById(R.id.refreshLayout);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(2000);
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadmore(2000);
            }
        });
        refreshLayout.setRefreshHeader(new MaterialHeader(getActivity()).setShowBezierWave(true));
        refreshLayout.setRefreshFooter(new BallPulseFooter(getActivity()).setSpinnerStyle(SpinnerStyle.Scale));
        list_title.add("动态");
        list_title.add("与我相关");
        list_title.add("私信");
        tabLayout.addTab(tabLayout.newTab().setText(list_title.get(0)));
        tabLayout.addTab(tabLayout.newTab().setText(list_title.get(1)));
        tabLayout.addTab(tabLayout.newTab().setText(list_title.get(2)));

        ImageView img1=new ImageView(getActivity());
        img1.setBackgroundResource(R.mipmap.img_login1);
        ImageView img2=new ImageView(getActivity());
        img2.setBackgroundResource(R.mipmap.img_login2);
        ImageView img3=new ImageView(getActivity());
        img3.setBackgroundResource(R.mipmap.img_login3);

        list_view.add(img1);
        list_view.add(img2);
        list_view.add(img3);

        viewPager.setAdapter(new MyAdapter());
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setTabsFromPagerAdapter(new MyAdapter());

        return view;
    }

    /**
     * viewpager适配器
     */
    private class MyAdapter extends PagerAdapter{
        @Override
        public int getCount() {
            return list_view.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(list_view.get(position));
            return list_view.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(list_view.get(position));
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return list_title.get(position);
        }
    }


}

关于书中提到的上拉加载下拉刷新的控件可以参考我这一片文章SwipeRefreshLayout上拉下拉框架简单使用

上一篇下一篇

猜你喜欢

热点阅读