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>

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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
知乎是一个知识分享和交流平台,为了满足用户的需求,开发知乎app需求分析文档非常重要。 首先,在需求分析文档中,需要明确知乎app的用户群体和主要功能。知乎的用户群体包括广大的知识爱好者、专业人士以及对特定领域感兴趣的人群。主要功能包括浏览问题和回答、发布问题和回答、关注和收藏他人、搜索感兴趣的话题和用户等。这些功能需要详细描述其具体操作和效果,以及实现的技术要求。 其次,需求分析文档还需要考虑用户体验和界面设计。知乎app的用户界面应该简洁、直观,让用户能够方便地找到自己感兴趣的内容。用户应该能够个性化设置自己喜欢的话题和作者,以及接收相关推送通知。另外,考虑到知乎的特点,用户还应该可以匿名发布问题和回答,保护自己的隐私。 第三,需求分析文档还需要考虑到知乎app在不同平台上的适配性。知乎可以在手机、平板和电脑等多个平台上使用,因此需求分析文档需要确保知乎app在不同尺寸和分辨率的设备上都能够正常工作,并提供一致性的用户体验。 最后,需求分析文档还需要考虑系统的性能要求以及安全性要求。知乎app需要能够同时支持大量的并发用户访问,因此需要进行性能测试和优化。另外,知乎app需要保证用户数据的安全性和隐私保护,需在需求分析文档中详细列出安全性措施和加密方式。 通过编写清晰、详细的知乎app需求分析文档,可以帮助开发团队更好地理解用户需求,确保产品开发过程的顺利进行,并最终开发出符合用户期望的优质知乎app
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值