2011-02-04 98 views
0

我有一个最近出现的奇怪问题。起初我在我的ListView中加载所有项目。它滚动一切都很好。项目是可点击的,并且一切正常。ListView in TabHost滚动/点击问题

然后在我的适配器的getView我补充说:

if(pos == getCount() - 1){ 
    Log.i("Load", "Load database"); 
    load_database(getCount(), 5); 
} 

,这里是load_database()

private void load_database(long offset, long count) { 
    ArrayList<VKContent> objs = BaseApplication.db.get_all(Message.class, MessageHandler.COLUMN_MAILBOX + " = " + mailbox, count, offset); 
    for(VKContent obj: objs){ 
     adp.add((Message)obj); 
    } 
} 

的问题出现后,正确的。一旦加载,它会调用load_database一次,一切都很好。 ListView工作正常。点击处理。但一旦达到“pos == getCount() - 1”。它加载新的数据,但现在无法点击项目。当我滚动并退缩时,屏幕上的触摸不会停止退缩。当我在退缩时尝试滚动时,它在退出停止之前没有任何效果。

如果我切换到不同的Tab,则listview行为被恢复。如果我尝试用我的滚动球来聚焦物品,那么正常的行为也会回来。

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@android:id/tabhost"> 

    <LinearLayout 
    android:orientation="vertical" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent"> 

     <TabWidget android:id="@android:id/tabs" 
      android:layout_height="wrap_content" 
      android:layout_width="fill_parent" /> 

     <FrameLayout android:id="@android:id/tabcontent" 
      android:layout_height="fill_parent" 
      android:layout_width="fill_parent" 
      android:layout_weight="1"> 
      <LinearLayout android:id="@+id/tab2" 
       android:orientation="vertical" 
       android:layout_height="fill_parent" 
       android:layout_width="fill_parent"> 
       <ListView android:id="@+id/lv" 
        android:layout_height="fill_parent" 
        android:layout_width="fill_parent" 
        android:clickable="true" /> 
      </LinearLayout> 
     </FrameLayout> 
    </LinearLayout> 
</TabHost> 

编辑

我曾尝试无效(),forceLayout(),requestLayout(),isFocusable()... ... bringToFront和load_database后等。但没有任何帮助。

编辑2 几乎忘了那段代码。

tabHost.setCurrentTab(1); 
    tabHost.setCurrentTab(0); 

由于某些原因,TabHost存在问题。我的ListView由所有选项卡共享。第一次加载视图时,它不显示任何内容。所以我不得不从第二个标签切换到第一个标签。

回答

0

所以我解决了我的问题,看看它是否没有tabHost工作,我意识到它仍然无法正常工作。所以看着旧代码,我意识到我的懒加载函数实际上是调用一个下载线程,它在后台运行,然后在下载完成后返回到uiThread。

与上面的代码的区别在于,load_database没有运行在不同的线程中。它从getView函数运行,这是我的适配器的功能。换句话说,它显然破坏了列表视图以更新getView中的适配器。

我目前的修复不是最好的,但现在它完成了这项工作。在未来。

if(pos == getCount() - 1){ 
    MessagesActivity.this.load_more = true; 
} 

@Override 
public void onUserInteraction() { 
    super.onUserInteraction(); 

    if(load_more == true || adp.getCount() == 0){ 
     load_more = false; 
     load_database(adp.getCount(), 5); 
    } 
} 

这种方式,load_database不会从getView拼命地跑。和我的适配器更新列表视图没有问题。