在FragmentTabHost中滑动不起作用

时间:2015-08-28 07:48:30

标签: android android-layout android-fragments android-intent android-activity

我创建了一个fragmenttabhost选项卡,并且选项卡仅在选项卡上单击,而不是在滑动上。如何在此启用滑动功能。 这是我的代码

MainActivity.java

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;


public class MainActivity extends FragmentActivity {

    private FragmentTabHost mainTabs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mainTabs = (FragmentTabHost)findViewById(android.R.id.tabhost);
        mainTabs.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);

        mainTabs.addTab(mainTabs.newTabSpec("tab1").setIndicator("Tab 1"),
                RecentFragment.class, null);
        mainTabs.addTab(mainTabs.newTabSpec("tab2").setIndicator("Tab 2"),
                ArchiveFragment.class, null);
        mainTabs.addTab(mainTabs.newTabSpec("tab3").setIndicator("Tab 3"),
                ArchiveFragment.class, null);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

activity_main.xml中

<android.support.v4.app.FragmentTabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f6f4ec">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#c0413c">

        <TextView android:text="@string/app_name"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="29sp"
            android:textColor="#ffffff" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="30dp"
        android:orientation="vertical" >

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"/>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0" />

    </LinearLayout>

</android.support.v4.app.FragmentTabHost>

RecentFragment.java

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

public class RecentFragment extends Fragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

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

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}

Fragment_recent.xml

<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"
    android:background="#c0413c"
    >

    <!-- TODO: Update blank fragment layout -->
    <TextView android:text="@string/app_name"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="29sp"
        android:textColor="#ffffff" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:background="#c0413c">

        <TextView android:text="@string/app_name"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="29sp"
            android:textColor="#ffffff" />

    </RelativeLayout>


</FrameLayout>

1 个答案:

答案 0 :(得分:0)

你可以看看这个 http://developer.android.com/training/implementing-navigation/lateral.html

此示例演示如何使用滑动导航使用ActionBar标签。

如果您想使用FragmentTabHost,则需要添加ViewPager,附加适配器,并在适配器检测到滑动事件时更改标签。