Android Fragment制作Tab

效果图

这里我仅仅使用了fragment,没有使用viewpager,所以是没有办法滑动的。

这里的fragment的xml布局我就不再上了,直接上top、bottom和activity_main;

bottom.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="55dp"
    android:background="@drawable/bottom_bar"
    android:orientation="horizontal" >

    <LinearLayout
        android:id="@+id/tab_weixin"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical" >

        <ImageButton
            android:id="@+id/tab_weixin_img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/tab_weixin_pressed"
            android:clickable="false" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="微信"
            android:textColor="#ffffff" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/tab_frd"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical" >

        <ImageButton
            android:id="@+id/tab_frd_img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/tab_find_frd_normal"
            android:clickable="false" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="朋友"
            android:textColor="#ffffff" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/tab_address"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical" >

        <ImageButton
            android:id="@+id/tab_address_img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/tab_address_normal"
            android:clickable="false" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="通讯录"
            android:textColor="#ffffff" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/tab_setting"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical" >

        <ImageButton
            android:id="@+id/tab_setting_img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/tab_settings_normal"
            android:clickable="false" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="设置"
            android:textColor="#ffffff" />
    </LinearLayout>

</LinearLayout>

top.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="wrap_content"
    android:background="@drawable/title_bar"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textStyle="bold"
        android:textColor="#ffffff"
        android:text="微信" />

</LinearLayout>

activity_main.xml

<LinearLayout 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"
    android:orientation="vertical"
    tools:context="${packageName}.${activityClass}" >

    <include layout="@layout/top" />

    <FrameLayout
        android:id="@+id/myFrame"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
    </FrameLayout>

    <include layout="@layout/bottom" />

</LinearLayout>

MainActivity.java

package com.example.fragemtdemo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;

public class MainActivity extends FragmentActivity implements OnClickListener {

    private LinearLayout weixinLayout, frdLayout, addressLayout, settingLayout;
    private ImageButton weixinButton, frdButton, addressButton, settingButton;

    private Fragment weixinFragment, frdFragment, addressFragment, settingFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        initControl();
        initEvent();
        setSeceted(0);
    }

    private void initEvent() {
        weixinLayout.setOnClickListener(this);
        frdLayout.setOnClickListener(this);
        addressLayout.setOnClickListener(this);
        settingLayout.setOnClickListener(this);
    }

    private void initControl() {
        weixinLayout = (LinearLayout) findViewById(R.id.tab_weixin);
        frdLayout = (LinearLayout) findViewById(R.id.tab_frd);
        addressLayout = (LinearLayout) findViewById(R.id.tab_address);
        settingLayout = (LinearLayout) findViewById(R.id.tab_setting);

        weixinButton = (ImageButton) findViewById(R.id.tab_weixin_img);
        frdButton = (ImageButton) findViewById(R.id.tab_frd_img);
        addressButton = (ImageButton) findViewById(R.id.tab_address_img);
        settingButton = (ImageButton) findViewById(R.id.tab_setting_img);
    }

    private void setSeceted(int i) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);
        switch (i) {
        case 0:
            if (weixinFragment == null) {
                weixinFragment = new WeiXinFragment();
                transaction.add(R.id.myFrame, weixinFragment);
            } else {
                transaction.show(weixinFragment);
            }
            weixinButton.setImageResource(R.drawable.tab_weixin_pressed);
            break;
        case 1:
            if (frdFragment == null) {
                frdFragment = new FrdFragment();
                transaction.add(R.id.myFrame, frdFragment);
            } else {
                transaction.show(frdFragment);
            }
            frdButton.setImageResource(R.drawable.tab_find_frd_pressed);
            break;
        case 2:
            if (addressFragment == null) {
                addressFragment = new AddressFragment();
                transaction.add(R.id.myFrame, addressFragment);
            } else {
                transaction.show(addressFragment);
            }
            addressButton.setImageResource(R.drawable.tab_address_pressed);
            break;
        case 3:
            if (settingFragment == null) {
                settingFragment = new SettingFragment();
                transaction.add(R.id.myFrame, settingFragment);
            } else {
                transaction.show(settingFragment);
            }
            settingButton.setImageResource(R.drawable.tab_settings_pressed);
            break;
        }
        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (weixinFragment != null)
            transaction.hide(weixinFragment);
        if (frdFragment != null)
            transaction.hide(frdFragment);
        if (addressFragment != null)
            transaction.hide(addressFragment);
        if (settingFragment != null)
            transaction.hide(settingFragment);
    }

    @Override
    public void onClick(View v) {
        resetImage();
        switch (v.getId()) {
        case R.id.tab_weixin:
            setSeceted(0);
            break;
        case R.id.tab_frd:
            setSeceted(1);
            break;
        case R.id.tab_address:
            setSeceted(2);
            break;
        case R.id.tab_setting:
            setSeceted(3);
            break;
        }
    }

    private void resetImage() {
        weixinButton.setImageResource(R.drawable.tab_weixin_normal);
        frdButton.setImageResource(R.drawable.tab_find_frd_normal);
        addressButton.setImageResource(R.drawable.tab_address_normal);
        settingButton.setImageResource(R.drawable.tab_settings_normal);
    }
}

DEMO下载地址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Android Tab + RecyclerView + Fragment 是一种常见的 Android 应用程序设计模式,它可以让开发者在一个屏幕上显示多个选项卡,并在每个选项卡使用 RecyclerView 和 Fragment 来显示不同的数据和视图。这种设计模式可以帮助开发者更好地组织和管理应用程序的内容,提高用户体验和应用程序的可维护性。 ### 回答2: Android平台上的Tab,RecyclerView和Fragment都是重要的组件。它们都可以用来构建强大的Android应用程序。 Tab是一个用户界面组件,它可以用来向用户展示多个内容区域。它通常被用作一个底部选项卡或者顶部标签栏。对于具有多个子页面的应用程序,Tab非常有用,因为它可以让用户快速访问不同的子页面。 RecyclerView是一个强大的视图组件,它可以用来列表或网格形式展示大量的数据。它是ListView的替代品,并且提供了更多的控制和灵活性。使用RecyclerView,我们可以构建出高度可定制的列表视图。 Fragment是一个可以重复使用的UI组件,它可以被动态地添加到Activity,以实现更多的灵活性和可重用性。与框架的Activity不同的是,Fragment可以嵌套在其他Fragment,从而实现更高级的UI设计。 Android开发者可以将这三个组件结合在一起,创建一个灵活,可扩展和强大的应用程序。我们可以将RecyclerView放在Fragment,以便将列表视图嵌入到我们的应用程序。使用Tab,我们可以在不同的列表视图之间切换。我们可以动态地添加或删除Fragment,以实现更具序列化的UI设计。 总而言之,Android tab,RecyclerView和Fragment是类型强大的UI组件,可以帮助Android开发者构建出高度可定制的应用程序。特别是在构建需要展示大量数据的应用程序时,它们可以大大简化开发过程。 ### 回答3: Android开发的Recycler View是一个强大的组件,可用于创建复杂的列表视图。而Fragment则是Android用于创建多个窗口的组件。Tab则表示选项卡视图,可在其切换不同的Fragment,使得应用程序变得更加灵活和具有可扩展性。 在开发Android应用程序时,我们经常会用到带有多个选项卡的布局,这时就需要使用TabLayout。在使用TabLayout时,需要配合ViewPager来使用,通过一个PagerAdapter将FragmentTab关联起来。在Fragment,我们通常会使用RecyclerView来展示列表视图。通过使用RecyclerView的Adapter和ViewHolder,我们可以轻松地创建我们所需的列表视图。主要特点如下: 1. RecyclerView的Adapter和ViewHolder可以轻松地创建复杂的列表视图,并提供了更好的性能和内存管理。 2. FragmentAndroid用于创建多个窗口的组件,与TabLayout结合使用,可以轻松地实现多个Fragment的切换。 3. TabLayout和ViewPager可以轻松地实现多个选项卡的布局,使得应用程序变得更加灵活和可扩展。 综上所述,Android Tab RecyclerView Fragment可以实现非常丰富的应用程序交互和视图设计。开发者可以根据不同的需求和场景选择合适的组件,灵活设计应用程序,提供更好的用户体验。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值