国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android開發(fā)實現(xiàn)自定義新聞加載頁面功能實例

2019-10-22 18:25:36
字體:
供稿:網(wǎng)友

本文實例講述了Android開發(fā)實現(xiàn)自定義新聞加載頁面功能。分享給大家供大家參考,具體如下:

一、概述:

1、效果演示:

Android,自定義,新聞加載頁面

2、說明:在新聞頁面剛加載的時候,一般會出現(xiàn)五種狀態(tài)

未知狀態(tài)(STATE_UNKNOW)、空狀態(tài)(STATE_EMPTY)、加載中(STATE_LOADING)、錯誤(STATE_ERROT)、成功(STATE_SUCCESS

因為每個Detail頁面都會出現(xiàn),所以我們可以把他們封裝成一個LoadPage的自定義view,可以復(fù)用

二、實現(xiàn):

1、首先的定義三個布局,為什么是三個,因為unkonw與loading的頁面可以使用同一個,而success的頁面是加載數(shù)據(jù)的頁面,這里不用定義

1)loading頁面布局,只有一個進度條

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="match_parent" >  <ProgressBar    style="?android:attr/progressBarStyleLarge"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_centerHorizontal="true"    android:layout_centerVertical="true" /></RelativeLayout>

2)空頁面只有一張圖片,顯示沒有數(shù)據(jù)

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="match_parent" >  <ImageView    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_centerHorizontal="true"    android:layout_centerVertical="true"    android:src="@drawable/ic_empty_page" /></RelativeLayout>

3)錯誤頁面有一張錯誤圖片與按鈕,點擊按鈕重新加載數(shù)據(jù)

<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:orientation="vertical" >  <RelativeLayout    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="center" >    <ImageView      android:id="@+id/page_iv"      android:layout_width="100dp"      android:layout_height="100dp"      android:layout_centerHorizontal="true"      android:scaleType="centerInside"      android:src="@drawable/ic_error_page" />    <Button      android:id="@+id/page_bt"      android:layout_width="wrap_content"      android:layout_height="34dp"      android:layout_below="@id/page_iv"      android:layout_centerHorizontal="true"      android:layout_marginTop="10dp"      android:background="@drawable/btn_bg"      android:ellipsize="end"      android:paddingLeft="10dp"      android:paddingRight="10dp"      android:singleLine="true"      android:text="@string/load_error"      android:textColor="#ff717171"      android:textSize="14dp" />  </RelativeLayout></FrameLayout>

4、初始化控件

/*** 初始化加載三種布局*/private void init() {    mLoadingView = initView(R.layout.loadpage_loading);    mEmptyView = initView(R.layout.loadpage_empty);    mErrorView = initView(R.layout.loadpage_error);    //如果發(fā)生錯誤,點擊重新加載    Button btnError = (Button) mErrorView.findViewById(R.id.page_bt);    btnError.setOnClickListener(new OnClickListener() {      @Override      public void onClick(View v) {        show();      }    });    showPages();}

5、全部代碼:

/** * @描述 加載頁面 * @項目名稱 App_Shop * @包名 com.android.shop.view * @類名 LoadingPage * @author chenlin * @date 2014年3月29日 下午8:49:39 */public abstract class LoadingPage extends FrameLayout {  private final static int STATE_UNKNOW = 0;  private final static int STATE_LOADING = 1;  private final static int STATE_ERROT = 2;  private final static int STATE_EMPTY = 3;  private final static int STATE_SUCCESS = 4;  // 不能使用靜態(tài)的,  private int currentState = STATE_UNKNOW;  private View mLoadingView; // 加載  private View mEmptyView; // 空頁面  private View mErrorView; // 網(wǎng)絡(luò)錯誤  private View mSuccessView; // 加載成功后的頁面  private Context mContext;  /**   * 定義枚舉類型   */  public enum LoadResult {    error(STATE_ERROT), empty(STATE_EMPTY), success(STATE_SUCCESS);    int value;    LoadResult(int value) {      this.value = value;    }    public int getValue() {      return value;    }  }  public LoadingPage(Context context, AttributeSet attrs, int defStyle) {    super(context, attrs, defStyle);    mContext = context;    init();  }  public LoadingPage(Context context, AttributeSet attrs) {    this(context, attrs, 0);  }  public LoadingPage(Context context) {    this(context, null);  }  /**   * 初始化加載三種布局   */  private void init() {    mLoadingView = initView(R.layout.loadpage_loading);    mEmptyView = initView(R.layout.loadpage_empty);    mErrorView = initView(R.layout.loadpage_error);    //如果發(fā)生錯誤,點擊重新加載    Button btnError = (Button) mErrorView.findViewById(R.id.page_bt);    btnError.setOnClickListener(new OnClickListener() {      @Override      public void onClick(View v) {        show();      }    });    showPages();  }  public View initView(int resId) {    View view = View.inflate(mContext, resId, null);    if (view != null) {      this.addView(view, new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));      return view;    }    return null;  }  private void showPages() {    //加載頁面顯示與不顯示    mLoadingView.setVisibility(currentState == STATE_UNKNOW || currentState == STATE_LOADING ? View.VISIBLE        : View.GONE);    //空頁面    mEmptyView.setVisibility(currentState == STATE_EMPTY ? View.VISIBLE : View.GONE);    //錯誤頁面顯示    mErrorView.setVisibility(currentState == STATE_ERROT ? View.VISIBLE : View.GONE);    //如果數(shù)據(jù)加載成功了,    if (currentState == STATE_SUCCESS) {      if (mSuccessView == null) {        //加載成功頁面信息,成功后的頁面就是新聞頁面信息        mSuccessView = createSuccessView();        //添加頁面到framelayout里        addView(mSuccessView, new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));        mSuccessView.setVisibility(View.VISIBLE);      }else {        mSuccessView.setVisibility(View.GONE);      }    }  }  public void show() {    if (currentState == STATE_EMPTY || currentState == STATE_ERROT) {      currentState = STATE_LOADING;    }    // 請求服務(wù)器 獲取服務(wù)器上數(shù)據(jù) 進行判斷    // 請求服務(wù)器 返回一個結(jié)果    ThreadManager.getInstance().createLongPool().execute(new Runnable() {      @Override      public void run() {        //從服務(wù)器加載數(shù)據(jù),得到返回的狀態(tài)信息        final LoadResult result = loadFromServer();        if (result != null) {          Util.runOnUiThread(new Runnable() {            @Override            public void run() {              currentState = result.getValue();              //顯示              showPages();            }          });        }      }    });    showPages();  }  public abstract View createSuccessView();  public abstract LoadResult loadFromServer();}

三、使用:

/** * @描述     fragment * @項目名稱   App_Shop * @包名     com.android.shop.fragment * @類名     BaseFragment * @author   chenlin * @date    2014年3月28日 下午10:33:59 */public abstract class BaseFragment<T> extends Fragment {  private LoadingPage mLoadingPage;  @Override  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {    if (mLoadingPage == null) {      mLoadingPage = new LoadingPage(getActivity()){        @Override        public View createSuccessView() {          return BaseFragment.this.createSuccessView();        }        @Override        public LoadResult loadFromServer() {          return BaseFragment.this.load();        }      };    }else {      ViewUtil.removeParent(mLoadingPage);    }    return mLoadingPage;  }  /***   * 創(chuàng)建成功的界面   * @return   */  public abstract View createSuccessView();  /**   * 從服務(wù)器得到結(jié)果嗎   * @return   */  protected abstract LoadResult load();  /**   * 顯示加載頁面   */  public void show(){    if (mLoadingPage != null) {      mLoadingPage.show();    }  }  /**校驗數(shù)據(jù) */  public LoadResult checkData(List<T> datas){    if (datas == null) {      return LoadResult.error;    }else {      if (datas.size() == 0) {        return LoadResult.empty;      }else {        return LoadResult.success;      }    }  }}

 

希望本文所述對大家Android程序設(shè)計有所幫助。


注:相關(guān)教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 漯河市| 江川县| 赣州市| 肇州县| 新化县| 平阴县| 金坛市| 界首市| 东丽区| 浪卡子县| 沁源县| 盐池县| 富川| 青海省| 莱州市| 商水县| 特克斯县| 辽源市| 淮北市| 六盘水市| 五大连池市| 柞水县| 张家口市| 望江县| 攀枝花市| 蓝田县| 红安县| 泸州市| 沙坪坝区| 漾濞| 册亨县| 琼结县| 翁源县| 镶黄旗| 京山县| 阳新县| 襄汾县| 阜南县| 莱西市| 江都市| 安义县|