TabLayout让Fragment在ViewPager中的滑动切换更优雅

首先扯点别的:最近感觉自己腐败了,学习也没劲了,锻炼也不积极了,整天吹牛扯淡,意志渐渐消磨,理想慢慢模糊,眼看就要堕入混吃等死的地步了。但是,经过痛苦的挣扎,激烈的内心斗争,我还是决定依然往前走,去迎接生命中的挑战与逆境,欢乐与惊喜。“弃我去者,昨日之日不可留。”今天就应该战斗,努力坚持,为了自己的理想,为了自己的责任。
今天是说一个Android Design Support Library中的TabLayout,我是在洪洋的一篇博客中看到的,网址:http://blog.csdn.net/lmj623565791/article/details/38902805
进入正题之前想问一问谁知道怎么用简单的方法生成gif图片啊,也是艰难,效果图只能是静态的。
TabLayout让Fragment在ViewPager中的滑动切换更优雅_第1张图片

不会制作gif图片也是艰难,效果不好,接下来看看代码吧,我是用的是android studio

第一 看看 build.gradle文件,要在dependencies{}中添加一行compile ‘com.android.support:design:23.2.0’

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.dmw.tablayoutviewpagerdemo"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.1'
    compile 'com.android.support:design:23.2.0'
}

第二:activity_main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">

    <TextView  android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TabLayout让Fragment在ViewPager中的滑动切换更优雅" android:textColor="#ff3344" />

    <android.support.design.widget.TabLayout  android:id="@+id/tabLayout" android:layout_width="match_parent" android:layout_height="wrap_content" />

    <android.support.v4.view.ViewPager  android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" />
</LinearLayout>

第三MainActivity.java文件

package com.dmw.tablayoutviewpagerdemo;

import android.graphics.Color;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

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

public class MainActivity extends AppCompatActivity {

    private TabLayout tabLayout;
    private ViewPager viewPager;
    private List<Fragment> fragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //设置tabLayout的属性
        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        tabLayout.setBackgroundColor(Color.parseColor("#ffffff"));
        tabLayout.setTabTextColors(Color.parseColor("#000000"),Color.parseColor("#0ddcff") );//设置tab上文字的颜色,第一个参数表示没有选中状态下的文字颜色,第二个参数表示选中后的文字颜色
        tabLayout.setSelectedTabIndicatorColor(Color.parseColor("#0ddcff"));//设置tab选中的底部的指示条的颜色

        viewPager = (ViewPager) findViewById(R.id.viewPager);

        fragments=new ArrayList<>();
        //给fragments 添加三个fragment
        fragments.add(new Fragmnet1());
        fragments.add(new Fragment2());
        fragments.add(new Fragment3());

        //给viewPager设置适配器
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

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

            @Override
            public CharSequence getPageTitle(int position) {
                switch (position) {
                    case 0:
                        return "fragment1 Title";
                    case 1:
                        return "fragment2 Title";
                    case 2:
                        return "fragment3 Title";

                }
                return null;
            }
        });

        //然后让TabLayout和ViewPager关联,只需要一句话,简直也是没谁了.
        tabLayout.setupWithViewPager(viewPager);
    }
}

三个Fragment及其xml文件
Fragment1.java

package com.dmw.tablayoutviewpagerdemo;

import android.os.Bundle;
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.ArrayAdapter;
import android.widget.ListView;

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

/** * Created by Administrator on 2016/4/6. */
public class Fragmnet1 extends Fragment {

    private View view;
    private ListView listView;
    private List<String>list;
    private ArrayAdapter adapter;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        list=new ArrayList<>();
        for(int i=0;i<50;i++){
            list.add("listView item "+i);
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.fragment1,container,false);
        listView= (ListView) view.findViewById(R.id.listView);
        adapter=new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1,list);
        listView.setAdapter(adapter);
        return view;
    }
}

fragment1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">

    <ListView  android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="match_parent"></ListView>
</LinearLayout>

Fragment2.java和Fragment3.java都一样

package com.dmw.tablayoutviewpagerdemo;

import android.os.Bundle;
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 android.widget.Toast;

/** * Created by Administrator on 2016/4/6. */
public class Fragment2 extends Fragment {

    private View view;
    private TextView textView;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.fragment2,container,false);
        textView= (TextView) view.findViewById(R.id.textView2);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(),"i am Fragment2",Toast.LENGTH_SHORT).show();
            }
        });
        return view;
    }
}

fragment2和fragment3.xml 两者只有textview的id不一样

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">

    <TextView  android:id="@+id/textView2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Fragment2" android:gravity="center" android:layout_marginTop="100dp" android:textColor="#ff3344" android:textSize="20sp"/>
</LinearLayout>

行,效果有了,代码也贴了,工程的完整代码链接http://download.csdn.net/detail/leilifengxingmw/9482257
结尾:android 新特性自己了解的很少,自己要多多关注,坚持学下去!也希望和我一样的android小白,不放弃,不抛弃,坚持学习,终会有收获!

你可能感兴趣的:(viewpager)