2012-10-12 39 views
3

我在我的适配器上有一个稍微奇怪的错误。 适配器正在创建的视图是左侧的缩略图和有几行的表格。每行有两个文字浏览。convertView失去onitemClick listView滚动后

其中一个textview有android:autoLink="web"属性集,而listview上有一个onItemClickListener。

问题是,每当TextView自动链接它的内容时,下次它的父视图被转换时,它不会再从onItemClickListener接收点击。

让我用一个例子阐明:

  • 厂景,视图2,VIEW3和view4都在屏幕上的列表视图。
  • view2有一个链接,它出现,onClick链接打开。
  • 该项目点击正常工作view1,view3和view4。
  • 滚动列表视图并将视图1转换为位置5,然后将视图2转换为位置6。
  • 位置6上的项目不包含链接,但onItemClick也未触发position6元素。

textview的自动链接功能肯定会改变我的布局,但我不知道是什么。必须有一个属性,我可以重置每个调用getView在我的适配器上,但哪些?

感谢您的任何帮助。

编辑

让我们来看看一些代码,这是相当标准/良好做法。 从我的适配器getView是: @Override 公共视图getView(INT位置,查看convertView,父的ViewGroup){

// 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); 
    } 

之前别人问,这就是expandable_child_view布局:正如我之前所说,只是一个

<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> 

带有图像视图和表格以及几个边距的线性布局。

+0

澄清问题的最佳方法是发布适当的代码。 – Sam

+0

嗨。这是你的标准好习惯实现listview + adapter + loader。我现在编辑帖子。 – Budius

+0

也显示'createNewView()',因为这是创建和设置视图的地方。 – Geobits

回答

6

根据罗曼盖伊here,这是通过设计来支持轨迹球/ dpad导航。 Comment 27有一个解决方法,它通过在每个ListView项设置后裔可聚焦:

setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS); 

android:descendantFocusability="blocksDescendants" 
+0

你是不是等着我来回答它的赏金?呵呵呵。谢谢,它效果很好。我更新了当前的项目,我会回到我做的一个个人项目,该项目在列表视图中拖放n个项目(正在回收的项目)中没有执行某些操作。 (你可以在22小时内奖励你的赏金),如果你已经给出了正确的答案,为什么会阻止? – Budius

+0

那么,你的原始编辑只是将它列入'最近'问题列表的首位。虽然,很高兴为你工作。关于赏金,我认为他们至少使用24小时定时器来防止人们将其授予第一个答案,因为更好的答案可能会在不久之后出现。 – Geobits

+0

谢谢,效果很好。我有一个textview作为我的listview项目布局的一部分,它有android:autoLink =“web”,所以最终用户可以点击链接。在添加完所有内容之后,它现在可以正常工作,但在某些项目不可点击之前。 – j2emanue

0

我想我知道什么可能是怎么回事。当你执行setText()时,有时会消除很多东西。您可能必须使用ClickableSpan将链接操作重新放回到textview中。

+0

抱歉乔,@Geobits有正确的答案。 – Budius