按钮在设置对 View.GONE 的可见性后仍会显示在布局中一段时间



我的布局包括 1 个显示项目列表的ListView和 1 个下方的">加载更多"按钮

ListView加载更多项目。单击此按钮时,我将其可见性设置为View.GONE

改为显示进度视图。但实际上,">加载更多"按钮仍然显示,直到新项目出现

添加到ListView .看起来很不舒服!

我试图打电话给adapter.onNotifyDataSetChanged但没有效果。还有其他方法可以解决这个问题吗?

谢谢

我的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_centerInParent="true"
    android:orientation="vertical"
    android:padding="4dip" >
    <ProgressBar
        android:id="@+id/loading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:visibility="invisible"
        android:indeterminate="true" />
    <ListView
        android:id="@+id/videoList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/load_more"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />
</RelativeLayout>

我的代码:

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.load_more:
            if (startIndex <= mActivity.mVideoCount[selected_pos]) {
                new loadMoreVideosTask().execute();
            }
            break;
        default:
            break;
        }
    }
   private class loadMoreVideosTask extends AsyncTask<Void, Void, Void> {
                  onPreExecute{
                     // Hide load more button
                     // Show loading Progress  
                 }
              protected Void doInBackground(Void... params) {
                // Loading data
              }
              onPosExecute {
                  // Update ListView
               }
   }

我认为

ListView.invalidateViews()

可能会为你做这个伎俩。它强制重绘所有子视图,包括包含按钮的子视图。(将其放在呼叫之后,以将其可见性设置为 GONE

据我了解,adapter.onNotifyDataSetChanged仅在更改ListView底层数据时才合适。

编辑:请注意,仅当您的按钮是 ListView 的子按钮时,这才有效,而它可能不是。

也许,您将"loadMore"按钮的可见性设置为View.GONE利用列表视图更新其数据,请检查它。

我尝试了很多方法,但没有任何改变。所以我只是简单地将加载更多按钮的阿尔法设置为 0。但这不是完美的方式,因为我仍然可以看到按钮占用的空白区域。

最新更新