gpt4 book ai didi

android - 如何使抽屉布局内容可滑动

转载 作者:行者123 更新时间:2023-11-30 02:23:02 28 4
gpt4 key购买 nike

我在抽屉导航的抽屉布局中有一个 ViewPager。当我通过向左滑动打开抽屉时,如果打开,但是当我尝试滑动 ViewPager 的 View 时,抽屉在从左向右滑动时关闭。所以我希望抽屉导航只有在我们从抽屉布局外部开始滑动时才能关闭。我尝试了一些但无法找到答案。这是我想要的屏幕截图:-->

https://drive.google.com/file/d/0B2IuJRFj9vQCS2szRlVWWTlfWVE/view?usp=sharing

最佳答案

我认为你应该尝试实现一个简单的抽屉导航(从左到右),然后你改变滑动的方向。这是一个简单的抽屉导航代码:

将您的布局修改为抽屉式布局:

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<!-- The navigation drawer -->
<ListView
android:id="@+id/drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"/>

实现一个ListView,它将成为你的抽屉导航的项目

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textView"/>

然后您应该实现您的 Activity 的 java 代码,将 ListView 与您的 NavigationDrawer 监听器和 Activity 代码连接起来。

public class MainActivity extends Activity {

private String[] mPlanetTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private CharSequence mTitle;
private ActionBarDrawerToggle mDrawerToggle;

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

mTitle = "test";

mPlanetTitles = new String[]{"one", "two", "three"};
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.drawer);

// Set the adapter for the list view
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mPlanetTitles));
// Set the list's click listener
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);

}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Pass the event to ActionBarDrawerToggle, if it returns
// true, then it has handled the app icon touch event
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle your other action bar items...

return super.onOptionsItemSelected(item);
}

/**
* Swaps fragments in the main content view
*/
private void selectItem(int position) {
Toast.makeText(this, R.string.app_name, Toast.LENGTH_SHORT).show();

// Highlight the selected item, update the title, and close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}

@Override
public void setTitle(CharSequence title) {
mTitle = title;
getSupportActionBar().setTitle(mTitle);
}

private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
selectItem(position);
}
}

关于android - 如何使抽屉布局内容可滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28214660/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com