在FragmentActivity内部,我尝试在ArrayAdapter中使用NavigationDrawer。尽管onItemClick从来不会被触发(在单击任意位置后,抽屉就会关闭),但这一切似乎都能正常工作。
我已经阅读了几乎无限数量的类似问题的线程,通常通过添加以下内容来解决:
android:focusable="false"我试着把它添加到所有的东西上。我还试着添加
android:descendantFocusability="blocksDescendants"
到我的父级布局。为了确保对DrawerLayout的关注,我调用了:
hashTagDrawerLayout.requestFocus();
在onDrawerOpened内部。你可以猜到..。到目前为止什么都没有起作用,所以我怀疑原因可能不是重点。
在activity中初始化我的抽屉:
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 fragment中的某个东西“偷走”了,尽管它甚至是不可见的?关于如何让它工作,我会给出一些建议。
发布于 2014-06-16 04:42:49
我上过两节课:
OnDrawerItemClickListener implements ListView.OnItemClickListener 和
DrawerAdapter extends BaseAdapter我在抽屉中只有两个列表项,所以我在DrawerAdapter中为这两个项指定了匿名View.OnClickListener。然后,在几天没有处理这段代码之后,我重构了一些东西,并用OnDrawerItemClickListener (以前是空的)实现了onClick功能。这是不起作用的,正如你上面提到的,我也搜索并尝试了很多解决方案,但没有一个是有效的。然后我检查了代码,发现DrawerAdapter中的匿名侦听器删除了它们,一切都运行得很好。
https://stackoverflow.com/questions/21552252
复制相似问题