风山晓 2015-04-18 07:26 采纳率: 71.4%
浏览 1954
已采纳

tabHost的滑动效果监听不到

我的tabHost有三栏,都是listView,想加一个滑动切换tab的效果,结果发现不行,于是
用了个简单的来测试,发现如下问题。

情况一

   android:id="@+id/tab2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >

                <EditText
                    android:id="@+id/editText1"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"

                    android:ems="10" />
            </LinearLayout> 

情况二

 <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >

                <EditText
                    android:id="@+id/editText1"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"

                    android:ems="10" />
            </LinearLayout>  

这个tab里面只有这个EditText,就android:layout_height不同

然后就发现滑动效果在fill_parent的时候不反应了!

这是什么问题???
下面是ACtivity的内容

 package com.mine.tabhostdemo2;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class TabHostActivity extends FragmentActivity {
    private static TabHost tabHost;
    private  GestureDetector detector=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_host);
        // tabHost = (TabHost) findViewById(R.id.tabhost);
        init();
        if (detector==null){

            detector= new GestureDetector(this,  
                    new MySimpleOnGestureListener());  
        }
    }

    private void init() {
        // TODO Auto-generated method stub
        tabHost = (TabHost) findViewById(R.id.tabhost);
        tabHost.setup();
        TabSpec spec1 = tabHost.newTabSpec("1");
        spec1.setIndicator("1",
                getResources().getDrawable(R.drawable.ic_launcher));
        spec1.setContent(R.id.tab1);
        TabSpec spec2 = tabHost.newTabSpec("2");
        spec2.setIndicator("2",
                getResources().getDrawable(R.drawable.ic_launcher));
        spec2.setContent(R.id.tab2);
        TabSpec spec3 = tabHost.newTabSpec("3");
        spec3.setIndicator("3",
                getResources().getDrawable(R.drawable.ic_launcher));
        spec3.setContent(R.id.tab3);
        tabHost.addTab(spec1);
        tabHost.addTab(spec2);
        tabHost.addTab(spec3);


//method one
     /*detector = new GestureDetector(this,
            new GestureDetector.SimpleOnGestureListener() {

                @Override
                public boolean onFling(MotionEvent e1, MotionEvent e2,
                        float velocityX, float velocityY) {
                    if ((e2.getRawX() - e1.getRawX()) > 80) {
                        showNext();
                        return true;
                    }

                    if ((e1.getRawX() - e2.getRawX()) > 80) {
                        showPre();
                        return true;
                    }
                    return super.onFling(e1, e2, velocityX, velocityY);
                }

            });*/

    }
    //method two
    private static class MySimpleOnGestureListener extends GestureDetector.SimpleOnGestureListener {  
        public MySimpleOnGestureListener() {  
            super();     
        }  
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {  
            if ((e2.getRawX() - e1.getRawX()) > 80) {
                showNext();
                return true;
            }

            if ((e1.getRawX() - e2.getRawX()) > 80) {
                showPre();
                return true;
            }
            //Log.e(TAG, "onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)");  
            return super.onFling(e1, e2, velocityX, velocityY);  
        }  
    }



    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(detector!=null){
        detector.onTouchEvent(event);
        }else{
            Log.i("nimeide1", "no detector!!!!!!!!!!!!");
        }
        return super.onTouchEvent(event);
    }

    static int i = 0;

    protected static void showPre() {
        // TODO Auto-generated method stub
        tabHost.setCurrentTab(i = i == 2 ? i = 0 : ++i);
        Log.i("kennet", i + "");
    }

    protected static void showNext() {
        // TODO Auto-generated method stub
        tabHost.setCurrentTab(i = i == 0 ? i = 2 : --i);
        Log.i("kennet", i + "");
    }
}

  • 写回答

2条回答 默认 最新

  • 风山晓 2015-04-25 07:07
    关注

    已经找到解决方法了
    解决touch事件的冲突

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码