diff --git a/demo/src/main/java/com/freelib/multiitem/demo/EmptyAndErrorActivity.java b/demo/src/main/java/com/freelib/multiitem/demo/EmptyAndErrorActivity.java index 47887b9..f3464ef 100644 --- a/demo/src/main/java/com/freelib/multiitem/demo/EmptyAndErrorActivity.java +++ b/demo/src/main/java/com/freelib/multiitem/demo/EmptyAndErrorActivity.java @@ -4,14 +4,89 @@ import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import com.freelib.multiitem.adapter.BaseItemAdapter; +import com.freelib.multiitem.adapter.holder.BaseViewHolder; +import com.freelib.multiitem.demo.bean.ImageBean; +import com.freelib.multiitem.demo.bean.ImageTextBean; +import com.freelib.multiitem.demo.bean.MainBean; +import com.freelib.multiitem.demo.bean.TextBean; +import com.freelib.multiitem.demo.state.ItemEmptyAndError; +import com.freelib.multiitem.demo.viewholder.ImageAndTextManager; +import com.freelib.multiitem.demo.viewholder.ImageViewManager; +import com.freelib.multiitem.demo.viewholder.TextViewManager; +import com.freelib.multiitem.helper.StateViewHelper; +import com.freelib.multiitem.item.BaseItemState; +import com.freelib.multiitem.listener.OnItemClickListener; +import com.freelib.multiitem.listener.OnStateClickListener; + +import org.androidannotations.annotations.AfterViews; import org.androidannotations.annotations.EActivity; +import org.androidannotations.annotations.ViewById; + +import java.util.ArrayList; +import java.util.List; + +import static android.R.id.empty; +import static com.freelib.multiitem.demo.R.id.recyclerView; @EActivity(R.layout.layout_recycler) public class EmptyAndErrorActivity extends AppCompatActivity { + @ViewById(R.id.recyclerView) + protected RecyclerView recyclerView; + private StateViewHelper emptyViewHelper; + private StateViewHelper errorViewHelper; public static void startActivity(Context context) { -// EmptyAndErrorActivity_. + EmptyAndErrorActivity_.intent(context).start(); + } + + @AfterViews + protected void initViews() { + setTitle(R.string.empty_error_title); + recyclerView.setLayoutManager(new LinearLayoutManager(this)); + //初始化adapter + BaseItemAdapter adapter = new BaseItemAdapter(); + //为XXBean数据源注册XXManager管理类 + adapter.register(TextBean.class, new TextViewManager()); + recyclerView.setAdapter(adapter); + adapter.addDataItem(new TextBean("展示空白页")); + adapter.addDataItem(new TextBean("展示错误页")); + + setOnItemClickListener(adapter); + + initEmptyHelper(); + initErrorHelper(); + } + + private void setOnItemClickListener(BaseItemAdapter adapter) { + adapter.setOnItemClickListener(new OnItemClickListener() { + @Override + public void onItemClick(BaseViewHolder viewHolder) { + switch (viewHolder.getItemPosition()) { + case 0: + emptyViewHelper.show(); + break; + case 1: + errorViewHelper.show(); + break; + } + } + }); + } + + private void initEmptyHelper() { + BaseItemState emptyItem = new ItemEmptyAndError("列表数据为空"); + emptyViewHelper = new StateViewHelper(recyclerView, emptyItem); + emptyItem.setOnStateClickListener(() -> emptyViewHelper.hide()); + } + + private void initErrorHelper() { + BaseItemState emptyItem = new ItemEmptyAndError("数据加载错误"); + errorViewHelper = new StateViewHelper(recyclerView, emptyItem); + emptyItem.setOnStateClickListener(() -> emptyViewHelper.hide()); } } diff --git a/demo/src/main/java/com/freelib/multiitem/demo/MainActivity.java b/demo/src/main/java/com/freelib/multiitem/demo/MainActivity.java index 259cf77..7e4426f 100644 --- a/demo/src/main/java/com/freelib/multiitem/demo/MainActivity.java +++ b/demo/src/main/java/com/freelib/multiitem/demo/MainActivity.java @@ -51,6 +51,8 @@ protected void initViews() { () -> InputActivity.startActivity(this))); adapter.addDataItem(new MainBean(getString(R.string.user_info_title), () -> UserInfoActivity.startActivity(this))); + adapter.addDataItem(new MainBean(getString(R.string.empty_error_title), + () -> EmptyAndErrorActivity.startActivity(this))); adapter.setOnItemClickListener(new OnItemClickListener() { @Override diff --git a/demo/src/main/java/com/freelib/multiitem/demo/UserInfoActivity.java b/demo/src/main/java/com/freelib/multiitem/demo/UserInfoActivity.java index 451d841..52ebaa3 100644 --- a/demo/src/main/java/com/freelib/multiitem/demo/UserInfoActivity.java +++ b/demo/src/main/java/com/freelib/multiitem/demo/UserInfoActivity.java @@ -45,7 +45,7 @@ protected void initViews() { BaseItemAdapter adapter = new BaseItemAdapter(); //为UserBean数据源注册数据绑定View Holder Manager管理类 adapter.register(ItemInfo.class, new DataBindViewHolderManager<>( - R.layout.item_image_text_data_bind, BR.itemData)); + R.layout.item_info, BR.itemData)); recyclerView.setAdapter(adapter); List list = new ArrayList<>(5); diff --git a/demo/src/main/java/com/freelib/multiitem/demo/state/ItemEmptyAndError.java b/demo/src/main/java/com/freelib/multiitem/demo/state/ItemEmptyAndError.java new file mode 100644 index 0000000..d18afcf --- /dev/null +++ b/demo/src/main/java/com/freelib/multiitem/demo/state/ItemEmptyAndError.java @@ -0,0 +1,45 @@ +package com.freelib.multiitem.demo.state; + +import android.databinding.ViewDataBinding; + +import com.freelib.multiitem.demo.BR; +import com.freelib.multiitem.demo.R; +import com.freelib.multiitem.item.BaseItemState; + +/** + * Created by free46000 on 2017/4/23. + */ +public class ItemEmptyAndError extends BaseItemState { + private String message; + private String btnText = "点击重试"; + + public ItemEmptyAndError(String message) { + this.message = message; + } + + @Override + protected void onBindViewHolder(ViewDataBinding dataBinding, ItemEmptyAndError data) { + dataBinding.setVariable(BR.itemData, data); + } + + @Override + protected int getItemLayoutId() { + return R.layout.layout_empty_error; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public String getBtnText() { + return btnText; + } + + public void setBtnText(String btnText) { + this.btnText = btnText; + } +} diff --git a/demo/src/main/res/layout/item_info.xml b/demo/src/main/res/layout/item_info.xml index cc4c32b..f6ef1f4 100644 --- a/demo/src/main/res/layout/item_info.xml +++ b/demo/src/main/res/layout/item_info.xml @@ -1,7 +1,7 @@ + xmlns:app="http://schemas.android.com/apk/res-auto" + xmlns:tools="http://schemas.android.com/tools"> @@ -20,28 +20,39 @@ + + \ No newline at end of file diff --git a/demo/src/main/res/layout/layout_empty_error.xml b/demo/src/main/res/layout/layout_empty_error.xml index eb9394f..68c6341 100644 --- a/demo/src/main/res/layout/layout_empty_error.xml +++ b/demo/src/main/res/layout/layout_empty_error.xml @@ -1,34 +1,46 @@ - + - + -