1. 程式人生 > >Android中BaseFragment封裝多狀態檢視顯示

Android中BaseFragment封裝多狀態檢視顯示

這裡寫圖片描述

根據不同的狀態,去更新不同的UI。

正常狀態: (資料載入完畢,顯示ContentView)
隱藏異常狀態View;
異常狀態: (載入資料中,資料載入失敗)
可以看見非正常狀態下,UI佈局都是一樣的。不一樣的只是圖片和文字。

原理: 佈局當中引入error狀態佈局,在載入資料時,讓errorView顯示,資料載入完成之後則將其隱藏掉。

異常狀態佈局,預設情況下android:visibility=”gone”不佔位隱藏
common_error:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android
="http://schemas.android.com/apk/res/android" android:id="@+id/errorView" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:visibility="gone" android:orientation="vertical">
<ImageView android:id="@+id/iv_error"
android:layout_width="60dp" android:layout_height="60dp" />
<TextView android:id="@+id/tv_error" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="14sp" /> </LinearLayout>

主頁Fragment佈局:複用引入common_error

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

    <include layout="@layout/common_error" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_test"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

開始在BaseFragment中進行封裝:

public class BaseFragment extends Fragment {
    public LayoutInflater inflater;
    public ViewGroup container;
    private View view ,errorView;
    private ImageView error_iv;
    private TextView error_tv;
    private OnReLoadDataListener onReLoadDataListener; //載入資料介面
    private RotateAnimation animator; //載入資料時動畫旋轉圖片
    public Activity mActivity;
    public void setContentView(int layout){
        view = inflater.inflate(layout,container,false);
    }
    public View getContentView(){
        return this.view;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.mActivity = activity;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.inflater  = inflater;
        this.container = container;
        return super.onCreateView(inflater, container, savedInstanceState);
    }
    //初始化errorView
    private void initBaseView(){
        errorView = findViewById(R.id.errorView);
        if(errorView != null){
            errorView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onReLoadDataListener!=null){
                        onReLoadDataListener.request();
                    }
                }
            });
        }
        error_iv = (ImageView)findViewById(R.id.iv_error);
        error_tv = (TextView)findViewById(R.id.tv_error);
    }
    //顯示載入頁
    public void showLoadDataPage(String txt, int resId){
        initBaseView();
        if(errorView == null){
            return;
        }
        if(error_tv == null){
            return;
        }
        if(error_iv == null){
            return;
        }
        errorView.setVisibility(View.VISIBLE);
        error_tv.setText(txt);
        error_iv.setImageResource(resId);
        if(animator == null){
            animator = new RotateAnimation(0f,365f, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
            animator.setDuration(1000);
            animator.setRepeatCount(Integer.MAX_VALUE);
            animator.startNow();
        }
        error_iv.setAnimation(animator);
    }
    //顯示異常頁
    public void showErrorPage(String txt, int resId){
        initBaseView();
        if(errorView == null){
            return;
        }
        if(error_tv == null){
            return;
        }
        if(error_iv == null){
            return;
        }
        errorView.setVisibility(View.VISIBLE);
        error_tv.setText(txt);
        error_iv.setImageResource(resId);
        error_iv.setAnimation(null);
    }
    //顯示內容頁,隱藏異常狀態頁
    public void showContentView(){
        initBaseView();
        if(errorView == null){
            return;
        }
        if(error_tv == null){
            return;
        }
        if(error_iv == null){
            return;
        }
        errorView.setVisibility(View.GONE);
    }
    public View findViewById(int id){
        return view.findViewById(id);
    }
    public void setOnReLoadDataListener(OnReLoadDataListener onReLoadDataListener){
        this.onReLoadDataListener = onReLoadDataListener;
    }
    //載入資料介面
    public interface  OnReLoadDataListener{
        void request();
    }
}

在子Fragment中的使用:

public class TestFragment extends BaseFragment implements BaseFragment.OnReLoadDataListener {
    private RecyclerView mTestRecyclerView;
    private LinearLayoutManager mLayoutManager;
    private String[] mTestData = new String[10];
    private Handler handler = new Handler();
    private boolean isLoad = false;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        setContentView(R.layout.fragment_test);
        initView();
        initData();
        return getContentView();
    }

    private void initView() {
        mLayoutManager = new LinearLayoutManager(mActivity);
        mTestRecyclerView = (RecyclerView) findViewById(R.id.rv_test);
        mTestRecyclerView.setLayoutManager(mLayoutManager);
        mTestRecyclerView.setAdapter(new TestAdapter());
        setOnReLoadDataListener(this);
    }

    private void initData() {
        int length = mTestData.length;
        for (int i = 0; i < length; i++) {
            mTestData[i] = "TEST" + i;
        }
        request();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                showErrorPage("請檢查網路...",R.drawable.ic_error);
                isLoad = true;
            }
        },3000);
    }

    @Override
    public void request() {
        showLoadDataPage("正在載入...", R.drawable.ic_loading);
        if(isLoad){
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mTestRecyclerView.setAdapter(new TestAdapter());
                    showContentView();
                }
            },2000);
        }
    }
    //RecyclerView介面卡
    class TestAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = inflater.inflate(R.layout.item_txt, null);
            return new TestHolder(view);
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            TestHolder testHolder = (TestHolder) holder;
            if(testHolder.item_tv == null){
                Log.i("TAG","testHolder.item_tv==null");
            }
            testHolder.item_tv.setText(mTestData[position]);

        }

        @Override
        public int getItemCount() {
            return mTestData.length;
        }

        class TestHolder extends RecyclerView.ViewHolder {
            private TextView item_tv;
            public TestHolder(View itemView) {
                super(itemView);
                item_tv = (TextView)itemView.findViewById(R.id.tv_test);
            }
        }
    }
}