【发布时间】:2013-08-13 16:27:09
【问题描述】:
我正在尝试使用 TextViews 制作一个 ListView,我希望每个 textview 都能够被点击,并根据它的位置拉出一个 Fragment。但是,itemOnClickListener 似乎甚至没有被调用。
mDrawerList = (ListView) findViewById(R.id.devices_drawer);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, drawerInfo));
mDrawerList.setItemsCanFocus(false);
mDrawerList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
final int pos, long id) {
Log.d(TAG, "CLICKED " + pos);
selectDevice(pos);
}
});
这是列表项的 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/deviceText"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
列表视图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" >
<ListView
android:id="@+id/devices_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.DrawerLayout>
【问题讨论】:
-
发布包含
ListView...的xml文件... -
@Shajeel Afzal 好的,我把它贴在上面了。
-
我认为您应该在
android:layout_width中指定match_parent而不是240dp,请问您为什么要这样做?
标签: java android xml listview drawer