我遇到一个问题,某些设备无法选择列表视图项,而我似乎无法解决代码中出现的问题 - 选择listview项可以在我的设备和模拟器上正常工作。我已经检查了焦点,但这似乎不是问题。有人有什么想法吗?
public class GroupListFragment扩展了ListFragment
@Override
public void onListItemClick(ListView listView, View view, int position, long id) {
super.onListItemClick(listView, view, position, id);
Group group = (Group) listView.getItemAtPosition(position);
mCallbacks.onItemSelected(group);
}
public class GroupListActivity extends FragmentActivity实现了GroupListFragment.Callbacks
@Override
public void onItemSelected(Group group) {
if (twoPaneLayout) {
// seems to work fine
Bundle arguments = new Bundle();
arguments.putLong(GroupDetailFragment.GROUP_ITEM_ID, group.getId());
GroupDetailFragment fragment = new GroupDetailFragment();
fragment.setArguments(arguments);
replaceDetailPane(fragment);
} else {
// does not work on some mobile devices
Intent detailIntent = new Intent(this, GroupDetailActivity.class);
detailIntent.putExtra(GroupDetailFragment.GROUP_ITEM_ID, group.getId());
startActivity(detailIntent);
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_left);
}
}
列表视图:
<?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:animateLayoutChanges="true"
android:background="@drawable/list_item_group_background" >
<ListView
android:id="@id/android:list"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:divider="@null"
android:dividerHeight="0dp"
android:verticalScrollbarPosition="left" />
<TextView
android:id="@id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textIsSelectable="false" />
</LinearLayout>
自定义项目:
<?xml version="1.0" encoding="utf-8"?>
<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:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center_vertical"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_item_group_item_background"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="10dp"
tools:ignore="HardcodedText" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center_vertical"
android:orientation="vertical" >
<TextView
android:id="@+id/list_item_group_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:singleLine="true"
android:text="User 22222222222222222222123456789012345"
android:textColor="@drawable/list_item_group_item_header_text"
android:textSize="16sp"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="@+id/list_item_group_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:singleLine="true"
android:text="Group status..."
android:textColor="@drawable/list_item_group_item_status_text"
android:textSize="12sp"
android:typeface="sans" />
</LinearLayout>
<TextView
android:id="@+id/list_item_group_callout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:background="@drawable/callout_background"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center"
android:singleLine="true"
android:text="new"
android:textColor="@drawable/callout_text"
android:textSize="10sp"
android:textStyle="bold"
android:typeface="sans" />
</LinearLayout>
<LinearLayout
android:id="@+id/list_item_group_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_item_group_toolbar_background"
android:baselineAligned="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:orientation="horizontal"
android:padding="5dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center" >
<Button
android:id="@+id/list_item_group_edit"
android:layout_width="35dp"
android:layout_height="35dp"
android:background="@drawable/list_item_group_edit_background"
android:focusable="false"
android:focusableInTouchMode="false" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center" >
<Button
android:id="@+id/list_item_group_delete"
android:layout_width="35dp"
android:layout_height="35dp"
android:background="@drawable/list_item_group_delete_background"
android:focusable="false"
android:focusableInTouchMode="false" />
</LinearLayout>
</LinearLayout>
答案 0 :(得分:0)
我已将代码更改为以下内容,现在正在运行。
@Override
public void onStart() {
super.onStart();
getListView().setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Group group = (Group) getListView().getItemAtPosition(position);
mCallbacks.onItemSelected(group);
}
});
}
我不确定为什么将click侦听器移动到onStart()方法已经有效,但它有。