【问题标题】:How to disable viewpager when navigation drawer is open导航抽屉打开时如何禁用viewpager
【发布时间】:2015-11-16 00:29:20
【问题描述】:

我编写了一个带有导航抽屉和 3 个固定选项卡的简单代码,但我面临这两个错误 1>即使导航抽屉打开并且当我点击它时......隐藏在导航抽屉后面的同一位置的选项卡也会被点击。 2> 当我点击导航抽屉上的项目时...动作被执行但片段未被替换或更精确地被视图页面隐藏。

这里是代码.. MainActivity.java

public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener {

private static String TAG = MainActivity.class.getSimpleName();

private Toolbar mToolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private FragmentDrawer drawerFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mToolbar = (Toolbar) findViewById(R.id.toolbar);

    setSupportActionBar(mToolbar);
    //getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    drawerFragment = (FragmentDrawer)
            getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
    drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
    drawerFragment.setDrawerListener(this);

    // display the first navigation drawer view on app launch
    displayView(0);

    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
}

private void setupViewPager(ViewPager viewPager){
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new TabOne(), getString(R.string.tabOne));
    adapter.addFragment(new TabTwo(), getString(R.string.tabTwo));
    adapter.addFragment(new TabThree(), getString(R.string.tabThree));
    viewPager.setAdapter(adapter);
}


class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}


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

    if(id == R.id.action_search){
        Toast.makeText(getApplicationContext(), "Search action is selected!", Toast.LENGTH_SHORT).show();
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onDrawerItemSelected(View view, int position) {
    displayView(position);
}

private void displayView(int position) {
    Fragment fragment = null;
    String title = getString(R.string.app_name);
    switch (position) {
        case 0:
            fragment = new HomeFragment();
            title = getString(R.string.title_home);
            break;
        case 1:
            fragment = new FriendsFragment();
            title = getString(R.string.title_friends);
            break;
        case 2:
            fragment = new MessagesFragment();
            title = getString(R.string.title_messages);
            break;
        default:
            break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.container_body, fragment);
        fragmentTransaction.commit();

        // set the toolbar title
        getSupportActionBar().setTitle(title);
    }
}
}

ActivityMain.xml

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

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


            <android.support.design.widget.AppBarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
                <include
                    android:id="@+id/toolbar"
                    layout="@layout/toolbar" />
                <android.support.design.widget.TabLayout
                    android:id="@+id/tabs"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:tabMode="fixed"
                    app:tabGravity="fill"/>

            </android.support.design.widget.AppBarLayout>

        </LinearLayout>

        <FrameLayout
            android:id="@+id/container_body"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"  />


    </LinearLayout>

</android.support.design.widget.CoordinatorLayout>
<fragment
    android:id="@+id/fragment_navigation_drawer"
    android:name="com.coastallabs.chat.devchat.activity.FragmentDrawer"
    android:layout_width="@dimen/nav_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:layout="@layout/fragment_navigation_drawer"
    tools:layout="@layout/fragment_navigation_drawer" />

【问题讨论】:

  • 正如我之前提到的,Fragment 的容器 FrameLayout 的高度为零,因为 ViewPager 占用了所有垂直空间。在 ViewPager 上设置 layout_height="0dp"layout_weight="1"。此外,您现在有六个不同的 Fragment 类。这就是你想要的吗?
  • 好的,如果我使用 layout_height="0dp" 和 layout_weight="1" 它将在 viewpager 和 framelayout 之间平均分割屏幕。但是我想要一种方式,当我单击导航抽屉项目时,只需使用framelayout即可打开一个新活动,而没有ViewPager(我不希望选择导航项目时这些选项卡和ViewPager)。)。
  • 对。我只是让你这样做,这样你至少可以看看你的 FragmentTransactions 是否正常工作。现在,你真的要打开一个新的 Activity 吗?如果你这样做了,那么进行 FragmentTransactions 或拥有家庭、消息和朋友 Fragment 就没有意义了。
  • 好的。因此,如果要创建一个聊天应用程序,其中包含联系人、消息、组等选项卡以及个人资料、设置和注销等导航项......那么我该如何将上述代码修改为?
  • 如果我这样做,我会将标签和 ViewPager 放在一个片段中,并为此添加第四个导航项。

标签: android navigation android-tablayout


【解决方案1】:

在你的fragment_navigation_drawer.xml:

设置 rootview clickable = true。

【讨论】:

  • 你抽屉的布局是什么?
  • 我在相关布局中使用了一个recyclerView。
  • 将 attr android:clickable = "true" 添加到 relativeLayout
  • 是的,现在它没有在后台被点击。但我想知道当用户点击导航抽屉的项目时如何显示不同的活动
  • 抽屉处于活动状态。您不能显示不同的活动
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多