android tab选项卡实现 仿知乎App

27 篇文章 0 订阅

效果

在这里插入图片描述

TabActivity

package com.coral3.ah.ui.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.coral3.ah.R;
import com.coral3.ah.ui.fragment.tab.TabFragment1;
import com.coral3.ah.ui.fragment.tab.TabFragment2;
import com.coral3.ah.ui.fragment.tab.TabFragment3;
import com.coral3.common_module.utils.InitUtil;

public class TabActivity extends AppCompatActivity implements View.OnClickListener{
    private ViewPager viewPager;
    private Fragment[] views;
    private LinearLayout llTabLayout;
    private int childCount;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab);
        initView();
        initListener();
    }
    public void initView(){
        viewPager = findViewById(R.id.vp_view_tabs);
        views = new Fragment[3];
        views[0] = new TabFragment1();
        views[1] = new TabFragment2();
        views[2] = new TabFragment3();
        viewPager.setAdapter(new MyAdapter(getSupportFragmentManager(), 1));
        llTabLayout = findViewById(R.id.ll_tabs_layout);
        childCount = llTabLayout.getChildCount();
        chooseTab(0);
    }
    public void chooseTab(int pos){
        for(int i = 0, len = childCount; i < len; i++){
            LinearLayout childAt = (LinearLayout) llTabLayout.getChildAt(i);
            ((TextView) childAt.getChildAt(1)).setTextColor(InitUtil.getContext().getResources().getColor(R.color.text_no_select));
            childAt.getChildAt(0).setEnabled(true);
        }
        LinearLayout childAt = (LinearLayout) llTabLayout.getChildAt(pos);
        ((TextView) childAt.getChildAt(1)).setTextColor(InitUtil.getContext().getResources().getColor(R.color.black));
        childAt.getChildAt(0).setEnabled(false);
    }
    public void initListener(){
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                chooseTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        for(int i = 0; i < childCount; i++){
            llTabLayout.getChildAt(i).setTag(i);
            llTabLayout.getChildAt(i).setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View view) {
        Log.d("yue-tag", view.getTag().toString());
        viewPager.setCurrentItem((int)view.getTag());;
    }

    private class MyAdapter extends FragmentPagerAdapter {

        public MyAdapter(@NonNull FragmentManager fm, int behavior) {
            super(fm, behavior);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return views[position];
        }

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

activity_tab.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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#F0F0F0"
    tools:context=".ui.activity.TabActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="#fff"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true">
       <LinearLayout
           android:id="@+id/ll_tabs_layout"
           android:layout_width="match_parent"
           android:orientation="horizontal"
           android:layout_height="wrap_content">
           <LinearLayout
               android:layout_width="0dp"
               android:layout_weight="1"
               android:gravity="center"
               android:orientation="horizontal"
               android:layout_height="wrap_content">
               <ImageView
                   android:src="@drawable/tab_selector"
                   android:layout_width="wrap_content"
                   android:layout_marginLeft="@dimen/dp_10"
                   android:layout_height="wrap_content"/>
               <TextView
                   android:text="推荐"
                   android:layout_marginLeft="3dp"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"/>
           </LinearLayout>
           <LinearLayout
               android:layout_width="0dp"
               android:layout_weight="1"
               android:gravity="center"
               android:orientation="horizontal"
               android:layout_height="wrap_content">
               <ImageView
                   android:src="@drawable/tab_selector"
                   android:layout_width="wrap_content"
                   android:layout_marginLeft="@dimen/dp_10"
                   android:layout_height="wrap_content"/>
               <TextView
                   android:text="热榜"
                   android:layout_marginLeft="3dp"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"/>
           </LinearLayout>
           <LinearLayout
               android:layout_width="0dp"
               android:layout_weight="1"
               android:gravity="center"
               android:orientation="horizontal"
               android:layout_height="wrap_content">
               <ImageView
                   android:src="@drawable/tab_selector"
                   android:layout_width="wrap_content"
                   android:layout_marginLeft="@dimen/dp_10"
                   android:layout_height="wrap_content"/>
               <TextView
                   android:text="高赞"
                   android:layout_marginLeft="3dp"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"/>
           </LinearLayout>
       </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:gravity="center_vertical">
            <TextView
                android:background="@drawable/bg_radius_5"
                android:layout_marginLeft="53dp"
                android:layout_width="30dp"
                android:layout_height="3dp"/>
        </LinearLayout>
    </LinearLayout>
    <LinearLayout
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <androidx.viewpager.widget.ViewPager
            android:id="@+id/vp_view_tabs"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </LinearLayout>
</LinearLayout>

图片素材

tab_img1
tab_img2
tab_img3
tab_blue
tab_red

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="purple_200">#FFBB86FC</color>
    <color name="purple_500">#FF6200EE</color>
    <color name="purple_700">#FF3700B3</color>
    <color name="teal_200">#FF03DAC5</color>
    <color name="teal_700">#FF018786</color>
    <color name="black">#FF000000</color>
    <color name="white">#FFFFFFFF</color>
    <color name="tabbar_no_act">#bfbfbf</color>
    <color name="tabbar_act">#ffffff</color>
    <color name="devide_line">#F0F0F0</color>
    <color name="text_no_select">#515050</color>
</resources>

bg_radius_5.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <corners android:radius="5dp"/>
    <solid android:color="#03A9F4"/>
</shape>

tab_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:drawable="@drawable/tab_red"/>
    <item android:state_enabled="true" android:drawable="@drawable/tab_blue"/>
</selector>

TabFragment1

package com.coral3.ah.ui.fragment.tab;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.coral3.ah.R;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link TabFragment1#newInstance} factory method to
 * create an instance of this fragment.
 */
public class TabFragment1 extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

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

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment TabFragment1.
     */
    // TODO: Rename and change types and number of parameters
    public static TabFragment1 newInstance(String param1, String param2) {
        TabFragment1 fragment = new TabFragment1();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

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

tab_fragment2

package com.coral3.ah.ui.fragment.tab;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.coral3.ah.R;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link TabFragment2#newInstance} factory method to
 * create an instance of this fragment.
 */
public class TabFragment2 extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

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

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment TabFragment2.
     */
    // TODO: Rename and change types and number of parameters
    public static TabFragment2 newInstance(String param1, String param2) {
        TabFragment2 fragment = new TabFragment2();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

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

tab_fragment3

package com.coral3.ah.ui.fragment.tab;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.coral3.ah.R;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link TabFragment3#newInstance} factory method to
 * create an instance of this fragment.
 */
public class TabFragment3 extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

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

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment TabFragment3.
     */
    // TODO: Rename and change types and number of parameters
    public static TabFragment3 newInstance(String param1, String param2) {
        TabFragment3 fragment = new TabFragment3();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

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

fragment_tab1.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".ui.fragment.tab.TabFragment1">

   <ImageView
       android:src="@drawable/tab_img1"
       android:scaleType="fitXY"
       android:layout_width="match_parent"
       android:layout_height="match_parent"/>

</FrameLayout>

fragment_tab2.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".ui.fragment.tab.TabFragment2">

    <ImageView
        android:src="@drawable/tab_img2"
        android:scaleType="fitXY"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</FrameLayout>

fragment_tab3.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".ui.fragment.tab.TabFragment3">

    <ImageView
        android:src="@drawable/tab_img3"
        android:scaleType="fitXY"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</FrameLayout>

吃饭去,底部蓝色移动有兴趣的同学自己去实现下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值