TabLayout+ViewPage+Fragment的使用

这是第二次写博客,感觉离一次写已经过了好久了。咱直奔主题,

TabLayout是谷歌于15年下半年发布的Android Support Design库里头的好东西,所以你得在gradle里头加入这个库才行(Android Studio),不然在布局里头,你是找不到TabLayout的。

1,先从布局开始:本次涉及到两个布局,一个是activity_main.xml,还有一个是fragment中用到的布局,fragment_test(所有的fragment都用的这个布局,让我偷个懒哈);

(一)activity_main.xml:

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

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="?attr/actionBarSize" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="scrollable"
            app:tabGravity="center"
            app:tabSelectedTextColor="#FFF"/>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPage"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>
(二)fragment_test:

<?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"
    android:gravity="center">
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/black"/>
</LinearLayout>
布局就这么多了,看起来很简单哈.

2.然后接下来便进入到MainActivity.java中了

public class MainActivity extends AppCompatActivity {

    @Bind(R.id.tabs)
    TabLayout mTabs;
    @Bind(R.id.viewPage)
    ViewPager mViewPage;
    @Bind(R.id.toolbar)
    Toolbar mToolbar;

    private List<Fragment> fragments;
    private List<String> tabs;
    private FragmentAdapter mAdapter;
    private Fragment fragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        mToolbar.setTitle("myTabs");
        setSupportActionBar(mToolbar);
        initData();
        initEvent();
    }

    private void initData() {
        fragments = new ArrayList<>();
        tabs = new ArrayList<>();
        for (int i=0;i<8;i++){
            fragment = new TabFragment();
            Bundle bd = new Bundle();
            bd.putString("theme","tab"+i);
            fragment.setArguments(bd);
            fragments.add(fragment);
            tabs.add("Tab"+i);
        }
    }

    private void initEvent() {
        mAdapter = new FragmentAdapter(getSupportFragmentManager(),fragments,tabs);
        mViewPage.setAdapter(mAdapter);
        mTabs.setupWithViewPager(mViewPage);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
}
然后就是MainActivity.java中所涉及到的fragment了

package com.example.administrator.fragmentsimple;

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 butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/3/20.
 */
public class TabFragment extends Fragment {

    @Bind(R.id.tv)
    TextView tv;

    private String theme;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_test, container, false);
        ButterKnife.bind(this, view);
        initEvent();
        return view;
    }

    private void initEvent() {
        theme = getArguments().getString("theme");
        tv.setText(theme);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
以及fragmentAdapter:

package com.example.administrator.fragmentsimple;

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

import java.util.List;

/**
 * Created by Administrator on 2016/3/20.
 */
public class FragmentAdapter extends FragmentPagerAdapter{

    private List<Fragment> fragments;
    private List<String> titles;

    public FragmentAdapter(FragmentManager fm,List<Fragment> fragments,List<String> titles) {
        super(fm);
        this.fragments = fragments;
        this.titles = titles;
    }

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

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }
}
这差不多就是全部内容了哈,感觉挺简单挺容易也没啥要讲的。



  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值