如何在我的Android应用程序中使用tabhost中的滑动功能?

时间:2013-11-30 16:16:21

标签: android android-tabhost swipe

我在我的应用程序中创建了一个标签主机,因为我想使用3个标签,并且当我点击其选项卡的显示时也工作正常,但现在我想添加一个滑动选项,以便我可以更改标签只是刷它。

请查看我的应用程序的代码,并建议我如何添加刷卡功能,如果你可以PLZ PLZ告诉我详细的回复我显示示例代码

Main_tab.java (当我打开应用时,它是我的第一堂课)

package com.saurabh.developerconsole;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;

public class Main_tab extends TabActivity {     

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //setContentView(R.layout.activity_main);

        TabHost mTabHost = getTabHost();

      //  mTabHost.addTab(mTabHost.newTabSpec("first").setIndicator("First").setContent(new Intent(this  ,play.class )));
       // mTabHost.addTab(mTabHost.newTabSpec("second").setIndicator("Second").setContent(new Intent(this , admob.class )));
       // mTabHost.addTab(mTabHost.newTabSpec("Third").setIndicator("Third",R.getDrawable(R.drawable.ad)).setContent(new Intent(this , anylistic.class )));
        mTabHost.addTab(mTabHost.newTabSpec("play").setIndicator(null, getResources().getDrawable(R.drawable.pla1)).setContent(new Intent().setClass(this, play.class)));
        mTabHost.addTab(mTabHost.newTabSpec("admob").setIndicator(null, getResources().getDrawable(R.drawable.ad12)).setContent(new Intent().setClass(this, admob.class)));
        mTabHost.addTab(mTabHost.newTabSpec("ana").setIndicator(null, getResources().getDrawable(R.drawable.ana1)).setContent(new Intent().setClass(this, anylistic.class)));
      //  mTabHost.addTab(mTabHost.newTabSpec("ana").setIndicator(null, getResources().getDrawable(R.drawable.ana1)).setContent(new Intent().setClass(this, anylistic.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));



        mTabHost.setCurrentTab(0);





    }



}

`

Play.Java(标签1)

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;

public class play extends Activity  {

    ProgressBar pb;
    WebView mWebView;

/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView( R.layout.play );



    // This is Process to Stay In Webview Insted To Gooing To broswer =========================================================================
        setProgressBarIndeterminateVisibility(true);
        pb = (ProgressBar) findViewById(R.id.pb);

        WebViewClient yourWebClient = new WebViewClient()
           {
               // Override page so it's load on my view only
               @Override
               public boolean shouldOverrideUrlLoading(WebView  view, String  url)
               {
                // This line we let me load only pages inside Firstdroid Webpage
                if ( url.contains("google") == true )
                   // Load new URL Don't override URL Link
                   return false;

                // Return true to override url loading (In this case do nothing).
                return true;
               }


           };

           final Activity activity = this;


           mWebView = (WebView) findViewById( R.id.webView1 ); //This is the id you gave
           mWebView.getSettings().setJavaScriptEnabled(true);  
           mWebView.getSettings().setSupportZoom(true);       //Zoom Control on web (You don't need this
                                                             //if ROM supports Multi-Touch     
           mWebView.getSettings().setBuiltInZoomControls(true); //Enable Multitouch if supported by ROM
           mWebView.setWebViewClient(yourWebClient);
         //  mWebView.setBackgroundColor(Color.parseColor("#204467"));

           mWebView.setWebViewClient(new WebViewClient() {
               public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                   Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
               }
           });


           mWebView.setWebChromeClient(new WebChromeClient() {
               public void onProgressChanged(WebView view, int progress) 
                  {
                  if(progress < 100 && pb.getVisibility() == ProgressBar.GONE){
                      pb.setVisibility(ProgressBar.VISIBLE);
                  }
                  pb.setProgress(progress);
                  if(progress == 100) {
                      pb.setVisibility(ProgressBar.GONE);
                  }
               }
           });


          // Load URL
           mWebView.loadUrl("https://play.google.com/apps/publish");

  }


        @Override
        public void onBackPressed() {
           if(mWebView.canGoBack() == true){
               mWebView.goBack();
           } else {

                 }
              }


    }

play.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"
    android:orientation="vertical" >


    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

       <ProgressBar
           android:id="@+id/pb"
           style="?android:attr/progressBarStyleSmall"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_centerHorizontal="true"
           android:layout_centerVertical="true" />

</RelativeLayout> 

Admob.java(标签2类)

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;

public class admob extends Activity  {

    ProgressBar pb;
    WebView mWebView;

/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView( R.layout.abmob);



    // This is Process to Stay In Webview Insted To Gooing To broswer =========================================================================
        setProgressBarIndeterminateVisibility(true);
        pb = (ProgressBar) findViewById(R.id.pb);

        WebViewClient yourWebClient = new WebViewClient()
           {
               // Override page so it's load on my view only
               @Override
               public boolean shouldOverrideUrlLoading(WebView  view, String  url)
               {
                // This line we let me load only pages inside Firstdroid Webpage
                if ( url.contains("google") == true )
                   // Load new URL Don't override URL Link
                   return false;

                // Return true to override url loading (In this case do nothing).
                return true;
               }


           };

           final Activity activity = this;


           mWebView = (WebView) findViewById( R.id.webView1 ); //This is the id you gave
           mWebView.getSettings().setJavaScriptEnabled(true);  
           mWebView.getSettings().setSupportZoom(true);       //Zoom Control on web (You don't need this
                                                             //if ROM supports Multi-Touch     
           mWebView.getSettings().setBuiltInZoomControls(true); //Enable Multitouch if supported by ROM
           mWebView.setWebViewClient(yourWebClient);
         //  mWebView.setBackgroundColor(Color.parseColor("#204467"));

           mWebView.setWebViewClient(new WebViewClient() {
               public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                   Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
               }
           });


           mWebView.setWebChromeClient(new WebChromeClient() {
               public void onProgressChanged(WebView view, int progress) 
                  {
                  if(progress < 100 && pb.getVisibility() == ProgressBar.GONE){
                      pb.setVisibility(ProgressBar.VISIBLE);
                  }
                  pb.setProgress(progress);
                  if(progress == 100) {
                      pb.setVisibility(ProgressBar.GONE);
                  }
               }
           });


          // Load URL
           mWebView.loadUrl("https://play.google.com/apps/publish");

  }


        @Override
        public void onBackPressed() {
           if(mWebView.canGoBack() == true){
               mWebView.goBack();
           } else {

                 }
              }


    }

anylistic.java(Tab3类)

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;

public class anylisticextends Activity  {

    ProgressBar pb;
    WebView mWebView;

/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView( R.layout.anylistic);



    // This is Process to Stay In Webview Insted To Gooing To broswer =========================================================================
        setProgressBarIndeterminateVisibility(true);
        pb = (ProgressBar) findViewById(R.id.pb);

        WebViewClient yourWebClient = new WebViewClient()
           {
               // Override page so it's load on my view only
               @Override
               public boolean shouldOverrideUrlLoading(WebView  view, String  url)
               {
                // This line we let me load only pages inside Firstdroid Webpage
                if ( url.contains("google") == true )
                   // Load new URL Don't override URL Link
                   return false;

                // Return true to override url loading (In this case do nothing).
                return true;
               }


           };

           final Activity activity = this;


           mWebView = (WebView) findViewById( R.id.webView1 ); //This is the id you gave
           mWebView.getSettings().setJavaScriptEnabled(true);  
           mWebView.getSettings().setSupportZoom(true);       //Zoom Control on web (You don't need this
                                                             //if ROM supports Multi-Touch     
           mWebView.getSettings().setBuiltInZoomControls(true); //Enable Multitouch if supported by ROM
           mWebView.setWebViewClient(yourWebClient);
         //  mWebView.setBackgroundColor(Color.parseColor("#204467"));

           mWebView.setWebViewClient(new WebViewClient() {
               public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                   Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
               }
           });


           mWebView.setWebChromeClient(new WebChromeClient() {
               public void onProgressChanged(WebView view, int progress) 
                  {
                  if(progress < 100 && pb.getVisibility() == ProgressBar.GONE){
                      pb.setVisibility(ProgressBar.VISIBLE);
                  }
                  pb.setProgress(progress);
                  if(progress == 100) {
                      pb.setVisibility(ProgressBar.GONE);
                  }
               }
           });


          // Load URL
           mWebView.loadUrl("https://play.google.com/apps/publish");

  }


        @Override
        public void onBackPressed() {
           if(mWebView.canGoBack() == true){
               mWebView.goBack();
           } else {

                 }
              }


    }  

1 个答案:

答案 0 :(得分:0)

我非常确定您可以使用ActionBar并将导航设置为标签导航,轻松完成此操作:http://developer.android.com/guide/topics/ui/actionbar.html#Tabs

这是滑动导航: http://developer.android.com/training/implementing-navigation/lateral.html