我在我的适配器上遇到了一个有点奇怪的错误。
适配器创建的视图是左侧的缩略图和带有几行的表格。每行都有两个文本视图。
其中一个文本视图设置了android:autoLink="web"属性,并且listview上设置了onItemClickListener。
问题在于,每次TextView自动链接其内容后,下一次其父视图被转换时,它不再接收来自onItemClickListener的点击。
让我用一个例子来澄清一下:
- view1、view2、view3和view4在屏幕上的列表视图中。 - view2有一个链接,当点击链接时,它打开。 - 对于view1、view 3和view4,项目单击正常工作。 - 滚动listview并将view1转换为position5,然后将view2转换为position6。 - 位置6处的项不包含链接,但也没有触发onItemClick元素的点击。
文本视图的自动链接功能肯定会改变我的布局,但我不知道是什么。必须有一个我可以对我的适配器上的每个调用重置的属性,但是哪个?
感谢任何帮助。
编辑
让我们看一些代码,这是相当标准/良好的实践。我的适配器中的getView是:
如果完全删除
编辑
以下是布局膨胀代码:
其中一个文本视图设置了android:autoLink="web"属性,并且listview上设置了onItemClickListener。
问题在于,每次TextView自动链接其内容后,下一次其父视图被转换时,它不再接收来自onItemClickListener的点击。
让我用一个例子来澄清一下:
- view1、view2、view3和view4在屏幕上的列表视图中。 - view2有一个链接,当点击链接时,它打开。 - 对于view1、view 3和view4,项目单击正常工作。 - 滚动listview并将view1转换为position5,然后将view2转换为position6。 - 位置6处的项不包含链接,但也没有触发onItemClick元素的点击。
文本视图的自动链接功能肯定会改变我的布局,但我不知道是什么。必须有一个我可以对我的适配器上的每个调用重置的属性,但是哪个?
感谢任何帮助。
编辑
让我们看一些代码,这是相当标准/良好的实践。我的适配器中的getView是:
// Inflate a new layout if needed
if (convertView == null)
convertView = createNewView();
// Gets the item from my array
AGplus item = (AGplus) getItem(position);
// Gets the holder pointing to the views
Holder h = (Holder) convertView.getTag();
// That's a test version, I won't be using date.toString() later on
h.date.setText(new Date(item.getDate()).toString());
// This guys is giving me a headache,
// If it parses the link, I can't never click on this convertView anymore,
// event re-converting them for a text that does not contain links
h.txt.setText(item.getTitle());
// some image download stuff that doesn't matter for this code
return convertView;
}
布局使用的是图片和表格,每个适配器插入表格的行数不同。表格布局是带有ImageView的水平线性布局,表格布局带有一些间距设置,以下是行布局:
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/text" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="web"
android:text=""
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/text" />
</TableRow>
如果完全删除
android:autoLink="web"
,我可以获取所有的点击事件,但正如之前所述,一旦视图被"自动链接",然后我回收该视图,就无法再次获取该视图上的点击事件。编辑
以下是布局膨胀代码:
private View createNewView() {
// Instantiate view
View v = getLayoutInflater().inflate(R.layout.expandable_child_view, null);
TableLayout table = (TableLayout) v.findViewById(R.id.table);
Holder h = new Holder();
v.setTag(h);
// Instantiate rows
h.thumb = (ImageView) v.findViewById(R.id.img);
h.date = (TextView) createNewRow(table, "Date: ");
h.txt = (TextView) createNewRow(table, "Text: ");
return v;
}
private View createNewRow(ViewGroup group, String title) {
View row;
row = getLayoutInflater().inflate(R.layout.item_table_row, null);
((TextView) row.findViewById(R.id.title)).setText(title);
group.addView(row);
return row.findViewById(R.id.text);
}
还有人问之前的问题,那就是可扩展子视图布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical" >
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="5dp"
android:layout_marginTop="20dp"
android:src="@drawable/ic_launcher" />
<TableLayout
android:id="@+id/table"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</TableLayout>
</LinearLayout>
如我之前所说,只需使用线性布局、图像视图和表格以及一些边距即可。
createNewView()
,因为这是视图被创建和设置的地方。 - Geobits