【问题标题】:Android Swipe in TabhostAndroid 在 Tabhost 中滑动
【发布时间】:2015-11-02 05:33:33
【问题描述】:

如何在tabhost中实现我的标签之间的滑动谢谢,以下是我的xml文件和java文件。

请帮我在这个tabhost中实现滑动,了解更多tabhost的朋友请分享所有tabhost选项。

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<TabHost
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@android:id/tabhost"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true">

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

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bRecharge">

    </FrameLayout>
</TabHost>

java 文件

package com.veristics.tijo.smartrecharge;

import android.app.ActionBar;
import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TabHost;
import android.view.Menu;
import android.widget.TextView;

import com.veristics.san.smartrecharge.net.AsyncNetCall;

import java.util.HashMap;

public class MainActivity extends TabActivity{
float lastX;
AsyncNetCall asyncNetCall;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
    tabHost.setup();

    TabHost.TabSpec mobile = tabHost.newTabSpec("Mobile");
    mobile.setIndicator("mobile");
    Intent mob = new Intent(MainActivity.this, MobileActivity.class);
    mobile.setContent(mob);

    TabHost.TabSpec dth = tabHost.newTabSpec("DTH");
    dth.setIndicator("DTH");
    Intent d = new Intent(MainActivity.this, DTHActivity.class);
    dth.setContent(d);

    TabHost.TabSpec datacard = tabHost.newTabSpec("Datacard");
    datacard.setIndicator("datacard");
    Intent dc = new Intent(MainActivity.this, DataCardActivity.class);
    datacard.setContent(dc);

    tabHost.addTab(mobile);
    tabHost.addTab(dth);
    tabHost.addTab(datacard);

    ActionBar mActionBar = getActionBar();
    mActionBar.setDisplayShowHomeEnabled(false);
    mActionBar.setDisplayShowTitleEnabled(false);
    LayoutInflater mInflater = LayoutInflater.from(this);

    View mCustomView = mInflater.inflate(R.layout.activity_custom_actionbar, null);

    mActionBar.setCustomView(mCustomView);
    mActionBar.setDisplayShowCustomEnabled(true);

    new AsyncNetCall(this).execute("getUserBalance");
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}



@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    switch (id) {
        case R.id.action_account_log:
            Intent account_log = new   Intent(this,AccountLogActivity.class);
            startActivity(account_log);
            return true;
        case R.id.action_recharge_log:
            Intent recharge = new Intent(this,RechargeLogActivity.class);
            startActivity(recharge);
            return true;
        case R.id.action_logout:
            Intent login = new Intent(this, LoginActivity.class);
            login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(login);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
}

【问题讨论】:

标签: java android


【解决方案1】:

@Tijo 约翰:

不好的方法

  1. 使用已弃用的代码TabActivity

此类在 API 级别 13 中已弃用。新应用程序应使用 片段而不是这个类;继续在旧设备上运行, 您可以使用 v4 支持库,它提供了 片段API

最佳方式

  1. 使用ViewPagerFragmentTabHost

ViewPager 最常与 Fragment 结合使用,Fragment 是一个 提供和管理每个页面的生命周期的便捷方式。那里 是为使用片段而实现的标准适配器 ViewPager,涵盖最常见的用例。

Demo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多