【问题标题】:NavigationDrawer onItemClick is not firedNavigationDrawer onItemClick 未触发
【发布时间】:2014-02-04 12:13:56
【问题描述】:

在 FragmentActivity 内部,我正在尝试将 NavigationDrawer 与 ArrayAdapter 一起使用。 尽管 onItemClick 从未被触发(在单击抽屉简单关闭的任何位置之后),但这一切似乎都可以正常工作。

我已经阅读了几乎无限数量的具有类似问题的线程,通常通过添加来解决:

android:focusable="false"

对于按钮等所有项目。我尝试将其添加到所有内容中。我也尝试添加

android:descendantFocusability="blocksDescendants"

到我的父布局。为了确保专注于 DrawerLayout,我调用了:

hashTagDrawerLayout.requestFocus();

在 onDrawerOpened 里面。你可以猜到......到目前为止没有任何效果,所以我怀疑原因可能不是重点。

在活动中初始化我的抽屉:

private ArrayList<String> hashTagValues; //values into list are loaded before  initializeHashTagNavigationDrower is called
private void initializeHashTagNavigationDrower() {
    hashTagDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    hashTagListView = (ListView) findViewById(R.id.left_drawer);
    hashTagListView.setAdapter(new ArrayAdapter<String>(this,
            R.layout.hashtag_list_item, R.id.textView1, hashTagValues
                    .toArray(new String[hashTagValues.size()])));
    hashTagListView.setOnItemClickListener(new NavigationDrawerListener(
            hashTagListView));// to nie działa :/
    getActionBar().setDisplayHomeAsUpEnabled(true);
    hashTagDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
    hashTagDrawerLayout, R.drawable.icon, R.string.open_drawer,
            R.string.close_drawer) {
        public void onDrawerClosed(View view) {
            invalidateOptionsMenu(); // creates call to
                                        // onPrepareOptionsMenu()
        }

我的 main.xml 是:

<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"
    android:focusable="false" />
<!-- The navigation drawer -->

<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#111"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp" />

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:focusable="false"
    android:orientation="vertical" >

    <fragment
        android:id="@+id/selectionFragment"
        android:name="com.example.SelectionFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:focusable="false" />

    <fragment
        android:id="@+id/splashFragment"
        android:name="com.example.SplashFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:focusable="false" />

    <fragment
        android:id="@+id/userSettingsFragment"
        android:name="com.facebook.widget.UserSettingsFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:focusable="false" />
</LinearLayout>

我的 hashtag_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textIsSelectable="false" />

</LinearLayout>

我的操作栏布局: action_bar.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:id="@+id/add_photo"
    android:focusable="false"
    android:icon="@drawable/action_photo"
    android:showAsAction="always"
    android:title="@string/take_photo">
</item>

如您所见,我正在使用 facebook 小部件 - 尽管焦点甚至不可见,但是否有可能被 facebook 片段中的某些东西“偷走”? 我将提供有关如何使其正常工作的任何提示。

【问题讨论】:

  • 为什么你的xml中也有片段。?我猜你很困惑。还有你在哪里填充hashTagValues
  • 这样我就可以使用我的 FragmentManager 找到它们。我正在学习基于 Facebook 的 Android 示例,这就是他们在 Scrumptious 应用程序中处理它的方式。这不是一个正确的解决方案吗?
  • 不,你有一个框架布局。那是你的容器。您将片段添加到容器中
  • 现在我很困惑。我可以删除多余的LinearLayout,片段会直接在FrameLayout下,但是我看不出我怎么能不把它们放到xml中...

标签: android listview onclicklistener navigation-drawer


【解决方案1】:

我有两节课:

OnDrawerItemClickListener implements ListView.OnItemClickListener 

DrawerAdapter extends BaseAdapter

我在抽屉中只有 2 个列表项,因此我在 DrawerAdapter 中为这两个项中的每一个指定了匿名 View.OnClickListener。然后,几天没有处理这段代码后,我正在重构一些东西并在 OnDrawerItemClickListener (以前是空的)中实现了 onClick 功能。这不起作用,正如你上面提到的,我也用谷歌搜索并尝试了很多解决方案,但没有一个有效。然后我查看了代码,发现 DrawerAdapter 中的匿名侦听器,将它们删除,一切正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多