【发布时间】: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