Android 在FragmentTabHost中刷卡不工作

Android 在FragmentTabHost中刷卡不工作,android,android-layout,android-fragments,android-intent,android-activity,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 andro

我创建了一个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);
    }
}
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();
    }
}
活动\u 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>
<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>
Fragment\u-recent.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>
<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>

你可以看看这个

此示例演示如何将
ActionBar
选项卡与滑动导航一起使用


如果您想改用
FragmentTabHost
,则需要添加一个
ViewPager
,在适配器检测到刷卡事件时,将适配器与其连接并更改选项卡。

我认为您需要添加一个
ViewPager
,以便添加刷卡导航