【问题标题】:An example of how to create tabs within a fragment?如何在片段中创建选项卡的示例?
【发布时间】:2017-05-05 07:39:47
【问题描述】:

我正在尝试在片段中创建选项卡。我有一个名为“Food_layout.xml”的片段

这里是xml文件。

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

    <TextView
        android:text="Food Layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="149dp"
        android:layout_marginStart="149dp"
        android:layout_marginTop="199dp"
        android:id="@+id/textView3" />

</RelativeLayout>

这里是片段的java类

package com.example.crims.plfitness;

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

public class FoodFragment extends Fragment{
    View myView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        myView = inflater.inflate(R.layout.food_layout, container, false);
        return myView;
    }
}

我尝试添加一个 TabHost,但是当我运行程序时它没有显示。有人告诉我我需要使用子片段管理器。我调查了它,但我真的没有找到任何关于如何实现它的东西。我是android开发的新手。

【问题讨论】:

标签: android android-fragments tabs


【解决方案1】:

我是这样做的, 如您所见,此片段包含两个选项卡。(使用适配器)

public class TabFragment extends Fragment {

public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 2;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    /**
     *Inflate tab_layout and setup Views.
     */
        View x =  inflater.inflate(R.layout.tab_layout,null);
        tabLayout = (TabLayout) x.findViewById(R.id.tabs);
        viewPager = (ViewPager) x.findViewById(R.id.viewpager);

    /**
     *Set an Apater for the View Pager
     */
    viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));


    /**
     * Now , this is a workaround ,
     * The setupWithViewPager dose't works without the runnable .
     * Maybe a Support Library Bug .
     */

    tabLayout.post(new Runnable() {
        @Override
        public void run() {
                tabLayout.setupWithViewPager(viewPager);
               }
    });

    return x;

}

class MyAdapter extends FragmentPagerAdapter{

    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

    /**
     * Return fragment with respect to Position .
     */

    @Override
    public Fragment getItem(int position)
    {
      switch (position){
          case 0: return new Fragment1();
          case 1 : return new Fragment2();

      }
    return null;
    }

    @Override
    public int getCount() {

        return int_items;

    }

    /**
     * This method returns the title of the tab according to the position.
     */

    @Override
    public CharSequence getPageTitle(int position) {

        switch (position){
            case 0 :
                return "frg1";
            case 1 :
                return "frg2";

        }
            return null;
    }
}

}

这是 TabFragment 的layout

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.TabLayout
    android:id="@+id/tabs"
    app:tabGravity="fill"
    app:tabMode="fixed"
    android:background="@color/green_1"
    app:tabIndicatorColor="@color/green_1"
    app:tabSelectedTextColor="@color/white"
    app:tabTextColor="@color/green_2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</android.support.design.widget.TabLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</android.support.v4.view.ViewPager>

您可以添加您的Fragment

【讨论】:

  • 谢谢。我对“TabFragment”有点困惑,是那个片段的java文件吗?即那会是我的“FoodFragment.java”吗?
  • TabFragment 是您要添加两个 Fragments/Tab 的那个。
  • 什么是 R.id.tabs 和 R.id.viewpager?
  • xml文件复制到你的
  • 我的 java 文件仍然有问题。 ViewPager 中的 setAdapter (android.support.v4.view.PagerAdapter) 无法应用于 (com.example.crims.plfitness.FoodFragment.MyAdapter)
猜你喜欢
  • 2020-06-15
  • 1970-01-01
  • 2014-05-13
  • 1970-01-01
  • 2013-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多