Android ListView - 使用整行
Android ListView - Use Whole Row
我有一个自定义列表视图,具有以下布局 (list_single.xml):
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent" >
<TableRow android:layout_width="fill_parent">
<ImageView
android:id="@+id/img"
android:layout_width="64dp"
android:layout_height="64dp"/>
<TextView
android:id="@+id/txt"
android:textSize="24dp" android:textStyle="bold"
android:paddingTop="20dp" android:paddingLeft="10dp"
android:layout_width="fill_parent"
android:layout_height="64dp" />
</TableRow>
</TableLayout>
然后我有一个 CustomList extender class 正在分配此布局:
public class CustomList extends ArrayAdapter<String> {
private final Activity context;
private final String[] web;
private final Integer[] imageId;
public CustomList(Activity context,
String[] web, Integer[] imageId) {
super(context, R.layout.list_single, web);
this.context = context;
this.web = web;
this.imageId = imageId;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_single, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
txtTitle.setText(web[position]);
imageView.setImageResource(imageId[position]);
return rowView;
}
}
然后在 OnCreate 事件处理程序中添加一个侦听器:
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
然而,尽管我希望整行都可以点击,但该行中唯一可点击的区域是图标。我在这里检查了其他一些类似的问题,但找不到 TableLayout 的解决方案。
非常感谢
你应该设置
android:focusableInTouchMode="true"
android:focusable="true"
在您的 table 布局中。一般来说listview的n行布局使用table布局不是一个好主意。对于您的情况,您可以通过任何其他布局轻松地做到这一点,例如线性布局、相对布局等
根据您的建议,我将 Table 替换为相对布局,现在一切正常。十分感谢大家。非常感谢。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/img"
android:layout_width="64dp"
android:layout_height="64dp" android:paddingLeft="20dp" />
<TextView
android:id="@+id/txt"
android:textSize="24dp"
android:textStyle="bold"
android:paddingTop="20dp"
android:paddingLeft="84dp"
android:layout_width="fill_parent"
android:layout_height="64dp" />
</RelativeLayout>
抱歉无法投票(它需要至少 15 分,我没有,因为我是 n00b)非常感谢您的帮助。非常感谢。
我有一个自定义列表视图,具有以下布局 (list_single.xml):
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent" >
<TableRow android:layout_width="fill_parent">
<ImageView
android:id="@+id/img"
android:layout_width="64dp"
android:layout_height="64dp"/>
<TextView
android:id="@+id/txt"
android:textSize="24dp" android:textStyle="bold"
android:paddingTop="20dp" android:paddingLeft="10dp"
android:layout_width="fill_parent"
android:layout_height="64dp" />
</TableRow>
</TableLayout>
然后我有一个 CustomList extender class 正在分配此布局:
public class CustomList extends ArrayAdapter<String> {
private final Activity context;
private final String[] web;
private final Integer[] imageId;
public CustomList(Activity context,
String[] web, Integer[] imageId) {
super(context, R.layout.list_single, web);
this.context = context;
this.web = web;
this.imageId = imageId;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_single, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
txtTitle.setText(web[position]);
imageView.setImageResource(imageId[position]);
return rowView;
}
}
然后在 OnCreate 事件处理程序中添加一个侦听器:
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
然而,尽管我希望整行都可以点击,但该行中唯一可点击的区域是图标。我在这里检查了其他一些类似的问题,但找不到 TableLayout 的解决方案。
非常感谢
你应该设置
android:focusableInTouchMode="true"
android:focusable="true"
在您的 table 布局中。一般来说listview的n行布局使用table布局不是一个好主意。对于您的情况,您可以通过任何其他布局轻松地做到这一点,例如线性布局、相对布局等
根据您的建议,我将 Table 替换为相对布局,现在一切正常。十分感谢大家。非常感谢。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/img"
android:layout_width="64dp"
android:layout_height="64dp" android:paddingLeft="20dp" />
<TextView
android:id="@+id/txt"
android:textSize="24dp"
android:textStyle="bold"
android:paddingTop="20dp"
android:paddingLeft="84dp"
android:layout_width="fill_parent"
android:layout_height="64dp" />
</RelativeLayout>
抱歉无法投票(它需要至少 15 分,我没有,因为我是 n00b)非常感谢您的帮助。非常感谢。