【问题标题】:Can't update a ListView inside a TabHost's FrameLayout无法更新 TabHost 的 FrameLayout 内的 ListView
【发布时间】:2011-02-01 02:32:46
【问题描述】:

我在 TabHost 的 FrameLayout 中有一个带有单个 ListView 的 TabActivity。这个 ListView 在我的 3 个选项卡之间共享。列表的项目是使用 Handler 从 Internet 下载的,但是当数据到来时,列表并没有被更新。当我选择另一个选项卡时,列表会自行更新,但我希望能够在下载后看到它正在更新。我已经尝试使 ListView、FrameLayout 和 TabHost 无效,但没有任何反应。 notifyDataSetChanged() 和 notifyDataSetInvalidated() 方法也无济于事。如果我将 ListView 放在 FrameLayout 之外,它会按预期工作。代码如下:

    <?xml version="1.0" encoding="UTF-8"?>

<TabHost android:id="@android:id/tabhost"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:layout_alignParentTop="true">

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

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

     <FrameLayout
         android:id="@android:id/tabcontent"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent">

         <ListView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/comments_list_lv"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
      </FrameLayout>
 </LinearLayout>

    public class CommentsActivity extends TabActivity implements Handler.Callback, OnTabChangeListener {

private static final int take = 20;
private Handler handler;
private CommentsListModel model = null;
private ListView listView;
private CommentsListAdapter adapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.comments_activity);

    handler = new Handler(this);

    setContentView(R.layout.comments_activity);
    listView = (ListView) findViewById(R.id.comments_list_lv);
    listView.setTextFilterEnabled(true);
    getTabHost().setOnTabChangedListener(this);

    buildTabs();
    setListView();
    requestData();
}


@Override
public boolean handleMessage(Message msg) {
   AbstractResponse response = (AbstractResponse) msg.obj;
   ResponseStatus status = response.getStatus();

   if (status.getResponseCode() == ResponseStatus.OK) {
       model = (CommentsListModel) response.getResponseObject();
       adapter = new CommentsListAdapter(this, model.getComments());
       listView.setAdapter(adapter);   
   }
   return true;
}


@Override
public synchronized void onTabChanged(String tabName) {
   if (model != null) {
       Resources res = getResources();
       CommentsFilter filter = null;
       if (tabName.equals(res.getString(R.string.comments_all))) {
           filter = new CommentsFilter(model.getComments(), CommentsFilter.ALL);
       } else if (tabName.equals(res.getString(R.string.comments_liked))) {
           filter = new CommentsFilter(model.getComments(), CommentsFilter.LIKED);
       } else if (tabName.equals(res.getString(R.string.comments_disliked))) {
           filter = new CommentsFilter(model.getComments(), CommentsFilter.DISLIKED);
       }
       adapter = new CommentsListAdapter(this, filter.filter());
       listView.setAdapter(adapter);
  }
}


private void buildTabs() {
  Resources res = getResources();

  TabHost tabHost = getTabHost();
  String tabName = res.getString(R.string.comments_all);

  TabHost.TabSpec spec = tabHost.newTabSpec(tabName);
  spec.setIndicator(tabName, null);
  spec.setContent(R.id.comments_list_lv);
  tabHost.addTab(spec);

  tabName = res.getString(R.string.comments_liked);

  spec = tabHost.newTabSpec(tabName);
  spec.setIndicator(tabName, null);
  spec.setContent(R.id.comments_list_lv);
  tabHost.addTab(spec);

  tabName = res.getString(R.string.comments_disliked);

  spec = tabHost.newTabSpec(tabName);
  spec.setIndicator(tabName, null);
  spec.setContent(R.id.comments_list_lv);
  tabHost.addTab(spec);
  }


private void requestData() {
   AbstractRequest request = new CommentsRequest(barcode, 0, take);
   request.addListener(handler);

   Message msg = new Message();
   msg.what = ApplicationActions.ProtocolActions.REQUEST;
   msg.obj = request;
   msg.setTarget(AsyncRequestController.getInstance().getInboxHandler()); 
   msg.sendToTarget();
 }
                                                                                                         }

【问题讨论】:

    标签: android android-widget android-layout android-listview android-tabhost


    【解决方案1】:

    我解决了这个问题。我只需要在设置适配器后设置 listView.setVisibility(View.VISIBLE) 。虽然视图没有设置为不可见或在 XML 文件中消失,但在这种情况下似乎有必要设置可见性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多