菜单选项卡不响应

18

目前我正在为我的应用程序制作用户界面。我使用滑动上面板滑动选项卡 (教程及其实现说明)功能。两者都运行良好。我甚至成功将它们实现在一个项目中,但问题就出在这里 - 主页和活动选项卡在我点击它们时无响应(应该打开它代表的页面),但当我在选项卡之间滑动时它们会切换。

输入图像描述

这是我的布局代码。这就是问题所在。我确定它。

<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">

<include
    android:id="@+id/tool_bar"
    layout="@layout/tool_bar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    />

 <com.sothree.slidinguppanel.SlidingUpPanelLayout
    xmlns:sothree="http://schemas.android.com/apk/res-auto"
    android:id="@+id/sliding_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom"
    sothree:umanoPanelHeight="68dp"
    sothree:umanoShadowHeight="4dp"
    sothree:umanoParalaxOffset="100dp"
    sothree:umanoDragView="@+id/dragView">

      <!-- MAIN CONTENT -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#ff0000">
        <com.ui.test.SlidingTabLayout
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:background="@color/ColorPrimary"/>

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:layout_weight="1">
 </android.support.v4.view.ViewPager>
    </RelativeLayout>

      <!-- SLIDING LAYOUT -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#55eeeeee"
        android:orientation="vertical"
        android:clickable="true"
        android:focusable="false"
        android:id="@+id/dragView">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="68dp"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/name"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:textSize="14sp"
                android:gravity="center_vertical"
                android:paddingLeft="10dp"/>

            <Button
                android:id="@+id/follow"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:textSize="14sp"
                android:gravity="center_vertical|right"
                android:paddingRight="10dp"
                android:paddingLeft="10dp"/>

        </LinearLayout>

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:scaleType="fitStart"
            android:src="@drawable/daimage" >
        </ImageView>
    </LinearLayout>
</com.sothree.slidinguppanel.SlidingUpPanelLayout>

如果我去掉添加滑动面板功能的部分,点击功能就会生效。

<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">

<include
    android:id="@+id/tool_bar"
    layout="@layout/tool_bar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    />


        <com.ui.test.SlidingTabLayout
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:background="@color/ColorPrimary"/>

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:layout_weight="1">
 </android.support.v4.view.ViewPager>

主活动(MainActivity.java)

public class MainActivity extends ActionBarActivity implements OnTouchListener{

    // Declaring Your View and Variables

    Toolbar toolbar;
    ViewPager pager;
    ViewPagerAdapter adapter;
    SlidingTabLayout tabs;
    CharSequence Titles[]={"Side","Front", "Result"};
    int Numboftabs =3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        // Creating The Toolbar and setting it as the Toolbar for the activity

        toolbar = (Toolbar) findViewById(R.id.tool_bar);
        setSupportActionBar(toolbar);


        // Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
        adapter =  new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs);

        // Assigning ViewPager View and setting the adapter
        pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(adapter);

        // Assiging the Sliding Tab Layout View
        tabs = (SlidingTabLayout) findViewById(R.id.tabs);
        tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width



        // Setting Custom Color for the Scroll bar indicator of the Tab View
        tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
            @Override
            public int getIndicatorColor(int position) {
                return getResources().getColor(R.color.tabsScrollColor);
            }
        });



        // Setting the ViewPager For the SlidingTabsLayout
        tabs.setViewPager(pager);

        tabs.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                v.getParent().requestDisallowInterceptTouchEvent(false);
                return false;
            }   
        });




    }




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.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);
    }

        @Override
public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    return false;
}


}

嗨,Oleksandr,你尝试过从Design Support库切换到TabLayout吗?可能会更加响应。 - muruthi
展示Java代码。我能说的是它已经覆盖了它,你的库中是否有onTabselected或类似的内容?如果有,请绕过那个区域进行处理。 - Elltz
我在这个问题上花费了150声望。如果它仍然没有被回答,那将是可惜的。 - Oleksandr Firsov
@OleksandrFirsov 为什么不试试 fernaMuruthi 的评论? - Emil
这真的并没有那么不同。 - Oleksandr Firsov
显示剩余3条评论
3个回答

6
建立在Amy的回答的基础上,假设slidingUpPanel阻止了触摸选项卡按钮的onTouch事件,那么您可以使用onTouch事件监听器来禁止父布局拦截任何触摸事件,代码如下:
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    // Creating The Toolbar and setting it as the Toolbar for the activity

    toolbar = (Toolbar) findViewById(R.id.tool_bar);
    setSupportActionBar(toolbar);


    // Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
    adapter =  new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs);

    // Assigning ViewPager View and setting the adapter
    pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(adapter);

    // Assiging the Sliding Tab Layout View
    tabs = (SlidingTabLayout) findViewById(R.id.tabs);
    tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width



    // Setting Custom Color for the Scroll bar indicator of the Tab View
    tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
        @Override
        public int getIndicatorColor(int position) {
            return getResources().getColor(R.color.tabsScrollColor);
        }
    });



    // Setting the ViewPager For the SlidingTabsLayout
    tabs.setViewPager(pager);

    // THIS PART, ADD CODE HERE !
    tabs.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
        v.getParent().requestDisallowInterceptTouchEvent(false);
        return false;
        }
    });

}

编辑1:

滑动面板就像覆盖在其子元素之上的叠加层,因此如果您将选项卡布局保留在子元素中,则滑动面板的条带视图将直接放在选项卡条带上,这将阻止选项卡条带接收任何点击或触摸事件,所以当您试图触摸或点击选项卡时,实际上并未实际触摸/点击选项卡,因为滑动面板在其上方。您需要做的是将滑动选项卡布局放在滑动面板布局之外。

<LinearLayout 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:orientation="vertical"
              tools:context=".MainActivity">

    <include
        android:id="@+id/tool_bar"
        layout="@layout/tool_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <!--this is outside sliding up panel layout-->
    <com.example.www.slidinguppanelapp.SlidingTabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:background="#f0f0f0"/>
    <com.sothree.slidinguppanel.SlidingUpPanelLayout
        android:id="@+id/sliding_layout"
        xmlns:sothree="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="bottom"
        sothree:umanoDragView="@+id/dragView"
        sothree:umanoPanelHeight="68dp"
        sothree:umanoShadowHeight="4dp">

        <!-- MAIN CONTENT -->
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#ff0000"
            android:orientation="vertical">

            <android.support.v4.view.ViewPager
                android:id="@+id/pager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1">
            </android.support.v4.view.ViewPager>
        </RelativeLayout>

        <!-- SLIDING LAYOUT -->
        <LinearLayout
            android:id="@+id/dragView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#55eeeeee"
            android:clickable="true"
            android:focusable="false"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="68dp"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/name"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="center_vertical"
                    android:paddingLeft="10dp"
                    android:textSize="14sp"/>

                <Button
                    android:id="@+id/follow"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:gravity="center_vertical|right"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    android:textSize="14sp"/>

            </LinearLayout>

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:scaleType="fitStart"
                android:src="@drawable/blur_bg">
            </ImageView>
        </LinearLayout>
    </com.sothree.slidinguppanel.SlidingUpPanelLayout>
</LinearLayout>

这些看起来像是语法错误,你能贴出整个类的代码吗?我需要查看完整的类代码才能看到花括号结构。 - Bhargav
我在问题中提供了文本。 - Oleksandr Firsov
1
请将代码放在onCreate的结尾处,而不是实际访问tabs变量的位置。 - Bhargav
如果我把它放在onCreate的末尾 - 我会得到很多错误,要求我彻底更改代码。你确定这是正确的吗? - Oleksandr Firsov
有什么想法吗?也许我听起来很烦,但我已经试图解决这个问题一个星期了,这让我感到沮丧。 - Oleksandr Firsov
显示剩余15条评论

3

从文档中得知:

默认情况下,整个面板将作为拖动区域并截取点击和拖动事件。您可以使用setDragView方法或umanoDragView属性将拖动区域限制在特定视图中。

因此我认为,Sliding Up Panel会消费您在选项卡上的onClick事件 - 只需按照文档建议设置属性即可。


е∞Ж umanoDragView жЈїеК†еИ∞ com.ui.test.SlidingTabLayout дЄ≠еєґж≤°жЬЙжФєеПШдїїдљХдЄЬи•њгАВ - Oleksandr Firsov
add it to your viewpager - yennsarah
sothree:umanoDragView="@+id/dragView1" 移动到 ViewPager - 没有任何更改。 - Oleksandr Firsov
好的,ViewPager是错的。您必须将umanoDragView添加到要启用拖动的视图中 - 因为您想要拥有ViewPager,我建议在第一个相对布局底部添加简单的<View/>元素,并确保您没有可点击的内容。我不知道它是否有效,但您可以尝试将属性添加到第二个子元素(LinearLayout),这样您只能从那里拖动? - yennsarah

0
tabs.setOnTouchListener(new View.OnTouchListener {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        v.getParent().requestDisallowInterceptTouchEvent(false);
        return false;
    }   
});

把这个放在你的onCreate()里。

有很多错误。请检查更新后的问题,包括代码和截图。 - Oleksandr Firsov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接