风山晓 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条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料