【问题标题】:Swipable tab is not swiping in android可滑动标签不在android中滑动
【发布时间】:2016-11-08 02:03:48
【问题描述】:

我正在尝试创建一个带有滑动标签的导航抽屉。但是我的浏览器不能正常工作。我只能选择选项卡。 Viewpager 无法加载任何片段。所以我不能刷它。如果我更改 viewpager 的高度,它会隐藏选项卡并加载所有片段。

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<include
    android:id="@+id/app_bar"
    layout="@layout/app_bar" />



<android.support.v4.widget.DrawerLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawer_layout">
    <FrameLayout
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >


        <android.support.design.widget.TabLayout
            android:id="@+id/sliding_tab"
            style="@style/MyCustomTabLayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            app:tabMode="scrollable" />

        <android.support.v4.view.ViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:background="@android:color/white" />

    </FrameLayout>

    <fragment
        android:id="@+id/nav_drawer"
        android:name="com.example.usaukglu.tablayoyt.NavigationDrawer"
        android:layout_width="@dimen/drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:layout="@layout/navigation_drawer"
        tools:layout="@layout/navigation_drawer" />

</android.support.v4.widget.DrawerLayout>


</LinearLayout>

这是我的 MainActivity.java 代码:

package com.example.usaukglu.tablayoyt;

import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
NavigationDrawer navigationDrawer;
DrawerLayout drawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_appbar);

    ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
    TabLayout tabLayout= (TabLayout) findViewById(R.id.sliding_tab);
    toolbar= (Toolbar) findViewById(R.id.app_bar);
    drawerLayout= (DrawerLayout) findViewById(R.id.drawer_layout);

    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    viewPager.setAdapter(new   ViewPagerAdapter(getSupportFragmentManager(),MainActivity.this));
    tabLayout.setupWithViewPager(viewPager);

    navigationDrawer= (NavigationDrawer) getSupportFragmentManager().findFragmentById(R.id.nav_drawer);
    navigationDrawer.setUp(R.id.nav_drawer,drawerLayout,toolbar);

}

}

这是我的 ViewpagerAdapter.java 代码

package com.example.usaukglu.tablayoyt;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;


class ViewPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 3;
private String tabTitles[] = new String[] { "Tab1", "Tab2", "Tab3" };
private Context context;

public ViewPagerAdapter(FragmentManager fm, Context context) {
    super(fm);
    this.context = context;
}

@Override
public int getCount() {
    return PAGE_COUNT;
}

@Override
public Fragment getItem(int position) {
    Fragment fragment=null;
    if (position==0){
        fragment=new FragmentPage();
    }
    if (position==1){
        fragment=new FragmentExpense();
    }
    if (position==2){
        fragment=new FragmentIncome();
    }
    return fragment;
}

@Override
public CharSequence getPageTitle(int position) {
    // Generate title based on item position
    return tabTitles[position];

}
}

【问题讨论】:

  • FragmentPage、FragmentExpense 和 FragmentIncome 所有这些都是用它们的 xml 视图设置的?
  • 是的,所有这些都设置好了

标签: java android android-studio android-fragments android-viewpager


【解决方案1】:

您不应该使用 FrameLayout 作为选项卡 + 寻呼机的容器。 FrameLayout 主要用于将视图置于其他视图之上。要修复它,您应该将此 FrameLayout 更改为具有垂直方向的 LinearLayout。视图页面高度也应该是 match_parent。

【讨论】:

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