Android開發:SwipeRefreshLayout無法顯示emptyView

來源:互聯網
上載者:User

標籤:android開發   android   

在使用官方的控制項SwipeRefreshLayout,發現原先的emptyView消失了,在網上搜尋了許多資料,沒有匹配我的答案,因為我的emptyView是用一個協助類來實現的,後來百經挫折,終於找到了一個完美的方法了,介紹如下:


首先,更改xml的布局,在外面增加一層Framelayout,改善後的XML布局如下:

<FrameLayout         android:id="@+id/parent"        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1"        >    <android.support.v4.widget.SwipeRefreshLayout        android:id="@+id/swipe_container"        android:layout_width="match_parent"        android:layout_height="match_parent">        <TextView                 android:id="@+id/empty_textview"                android:layout_width="match_parent"                android:layout_height="match_parent"                android:text="empty_textview"                android:textSize="25sp"                />            <ListView                android:id="@+id/listview"                android:layout_width="match_parent"                android:layout_height="match_parent"                android:background="@color/white"                android:scrollbarStyle="outsideOverlay" />                            </android.support.v4.widget.SwipeRefreshLayout>    </FrameLayout>

這樣做的原因,是因為swipelayout是viewgroup,無法直接addView,或者說可以吧,但是addView是沒有效果的,於是在外面嵌套一個FrameLayout,用這個layout來add emptyView,下面是emptyView的協助類


public class EmptyViewHelper {private ListView mListView;private View emptyView;private Context mContext;private String mEmptyText;private TextView mTextView;private FrameLayout parent;public EmptyViewHelper(ListView listView, String text) {mListView = listView;mContext = listView.getContext();mEmptyText = text;initEmptyView();}public EmptyViewHelper(ListView listView, String text, FrameLayout parent) {mListView = listView;mContext = listView.getContext();mEmptyText = text;this.parent = parent;initEmptyView();}private void initEmptyView() {emptyView = View.inflate(mContext, R.layout.empty_view, null);LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);parent.addView(emptyView, lp);mListView.setEmptyView(emptyView);if (!TextUtils.isEmpty(mEmptyText)) {((TextView) emptyView.findViewById(R.id.textview)).setText(mEmptyText);}}}


這樣的話,對於需要增加emptyView的listview,直接使用如下的代碼,既可以實現了


EmptyViewHelper emptyViewHelper = new EmptyViewHelper(mListview, "正在載入", (FrameLayout)v.findViewById(R.id.parent));

這樣,listview就的emptyView效果就出來了


在逐漸的開發過程中,發現代碼的複用越來越重要,對於同樣的功能,能夠使用協助類來實現的,盡量用協助類,降低我們的代碼的工作量,以後同樣的工作,只要很少的代碼就可以實現了,或者同樣的模組,也僅僅需要很少的代碼,這樣的方法,在後期更新的時候,也是只要更新一個模組,就全部都更新了,適合工作中使用。





Android開發:SwipeRefreshLayout無法顯示emptyView

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.