1. 程式人生 > >Viewpager配合Tablayout滑動

Viewpager配合Tablayout滑動

新增依賴
compile ‘com.android.support:design:28+’

佈局檔案

    <android.support.design.widget.TabLayout
        android:id="@+id/frag_tab"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></android.support.design.widget.TabLayout>
    <android.support.v4.view.ViewPager
        android:id="@+id/frag_pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"></android.support.v4.view.ViewPager>

Java

package com.exam.week3_02.Frag;

import android.content.Context;
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.ImageView;

import com.exam.week3_02.R;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;

public class Frag_02_tab extends Fragment {

    private Banner banner;

    @Nullable
    @Override
    public View onCreateView(@NonNull final LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frag_02_tab,container,false);

        ArrayList<String> listu = new ArrayList<>();
        listu.add("http://ww4.sinaimg.cn/large/006uZZy8jw1faic21363tj30ci08ct96.jpg");
        listu.add("http://ww4.sinaimg.cn/large/006uZZy8jw1faic259ohaj30ci08c74r.jpg");
        listu.add("http://ww4.sinaimg.cn/large/006uZZy8jw1faic2b16zuj30ci08cwf4.jpg");
        //放標題的集合
        ArrayList<String> list_title = new ArrayList<>();
        list_title.add("好好學習");
        list_title.add("天天向上");
        list_title.add("熱愛勞動");
        list_title.add("不搞物件");
        banner = view.findViewById(R.id.mbanner);
        banner.setImages(listu);
        banner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
                com.nostra13.universalimageloader.core.ImageLoader instance = com.nostra13.universalimageloader.core.ImageLoader.getInstance();
                instance.displayImage((String) path,imageView);
            }
        });


        banner.setBannerTitles(list_title);
        banner.setDelayTime(3000);
        banner.start();
        return view;
    }
}