android 自带的design包中tablayout使用

在studio的Project Structure=>app=>dependencies ==>搜索design的依赖,如下:


compile 'com.android.support:design:26.0.0-alpha1'
先看实现的效果

     android 自带的design包中tablayout使用_第1张图片

先看一下布局吧(这个是activity中的主布局,也就是tabyaout,替换的布局是fragment )

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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#F8F8F8"
    android:id="@+id/linear"
    tools:context="com.yangzhuokeji.myapplication.MainActivity">

    <android.support.design.widget.TabLayout
        android:background="@android:color/white"
        android:layout_marginTop="5dp"
        android:id="@+id/tab"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabIndicatorColor="#3082B4"
        android:enabled="false"
        app:tabTextColor="@android:color/black"
       app:tabSelectedTextColor="#3082B4"
        app:tabMode="fixed" />

    <android.support.v4.view.ViewPager
        android:layout_marginTop="10dp"
        android:background="@android:color/white"
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

LinearLayout>

MainActivity.class

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {
    private LinearLayout linearLayout;
    Fragment[] fragments = {new BlankFragment(), new Blank2Fragment(), new Blank3Fragment(), new Blank4Fragment()};
    String[] titles = {"全部", "已通过", "审核中", "未通过"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TabLayout tab = (TabLayout) findViewById(R.id.tab);
        linearLayout = (LinearLayout) findViewById(R.id.linear);
        //添加tablayout中的竖线
        LinearLayout linearLayout = (LinearLayout) tab.getChildAt(0);
        linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        linearLayout.setDividerDrawable(ContextCompat.getDrawable(this,
                R.drawable.divider));
        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {

                return fragments[position];
            }

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

            @Override
            public CharSequence getPageTitle(int position) {
                return titles[position];
            }
        });
        tab.setupWithViewPager(viewPager);
    }
}

viewPager.setCurrentItem(tabposition);
tab.getTabAt(tabposition).select();
跳转到指定的位置


tablayout每个item的分割线(竖线)


在drawable中创建divier.xml



    
    
fragment就给一个例子吧,其他的都一样,自己做服务器对接交互就行了

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * A simple {@link Fragment} subclass.
 */
public class BlankFragment extends Fragment {


    public BlankFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_blank, container, false);
    }

}
自己做个记录,有需要的,自己看哈,挺简单的

你可能感兴趣的:(移动开发)