gpt4 book ai didi

java - 将 DrawerLayout 与 ListView 一起使用

转载 作者:行者123 更新时间:2023-11-29 05:38:57 24 4
gpt4 key购买 nike

我正在尝试编写一个 Android 应用程序,其中包含几个不同项目的 ListView 以及一个可以从屏幕左侧拖动的抽屉布局。这就是我的意思....

这是主屏幕的样子: enter image description here

下面是抽屉菜单的样子: enter image description here

我遇到的问题是,当我打开侧边抽屉菜单并点击一个选项时,它不起作用,菜单就关闭了。但是我能够与主 ListView 页面进行交互。这是我的代码的样子:

String[] homeArray = { "Test", "Test", "Test", "Test", "Test", "Test", "Test", "Test", "Test", "Test", "Test", "Test", "Test", "Test", "Test" };

private ListView homeListView;
private ArrayAdapter arrayAdapter;



private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;

private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mDrawerTitles;

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



mTitle = mDrawerTitle = getTitle();
mDrawerTitles = getResources().getStringArray(R.array.Menu);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);

// Set the adapter for the list view
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mDrawerTitles));

和 activity_main.xml:

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_height="match_parent"
android:layout_width="match_parent"
>

<FrameLayout
android:id="@+id/content_frame"
android:layout_height="match_parent"
android:layout_width="match_parent" />

<ListView
android:id="@+id/left_drawer"
android:layout_height="match_parent"
android:layout_width="240dp"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/darker_gray"
android:dividerHeight="1dp"
android:background="#111"
/>
<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=".ListActivity" >

<ListView
android:id="@+id/homeListView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>

</LinearLayout>

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

我感觉问题出在 xml 中,但我不确定。

最佳答案

请发布包含 NavigationDrawer 的 Activity 的完整代码。

此外,我强烈建议(Google 也会)为各个 NavigationDrawer 部分使用 fragment (在您的案例中为“设置”、“提交错误”和“帮助”)。

Do not put all the layout components into the layout file of your Activity.

因此布局应如下所示:

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 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/left_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"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>

FrameLayout content_frame 是包含 ListView 的 Fragment 所在的位置。只需创建一个包含您在图片中显示的 ListView 的自定义 fragment (或使用 ListFragment)并将其插入“content_frame”。

使用它来替换内容框架内的 fragment :(当您切换您的部分时)

/** Swaps fragments in the main content view */
private void selectItem(int position) {
// Create a new fragment and specify the planet to show based on position
Fragment fragment = new YourListFragment(); // this fragment contains the list with all the "test" items

// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment)
.commit();

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

您的 Fragment 的示例:YourListFragment.java布局文件“list_fragment.xml”只包含您想要的任何布局组件。例如一个 ListView 。在 Fragments 的 onCreateView() 方法中初始化并填充 ListView。

public class YourListFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.list_fragment, container, false);

return rootView;
}
}

全部取自 Google 关于如何创建 NavigationDrawer 的示例:

http://developer.android.com/training/implementing-navigation/nav-drawer.html

关于java - 将 DrawerLayout 与 ListView 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18412395/

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