美文网首页自定义Android知识Android开发
打造一个傻瓜式的处理下拉刷新,滚动底部自动加载更多,分页加载,自

打造一个傻瓜式的处理下拉刷新,滚动底部自动加载更多,分页加载,自

作者: 峥嵘岁月Z | 来源:发表于2016-10-18 14:13 被阅读982次

扯谈:

  • 在实际开发工作中,你是否体会过自己无意识中就写了好多重复的代码?然后,感叹自己写的代码不够看啊。在实际开发工作中,你是否体会过自己无意识中就造了好多重复的轮子?然后默默将自己写的轮子换成Github上已经存在并且写得更好的轮子 ,以为自己写的还挺好,现在得亲手将自己写的东西打入冷宫,心情还真有点复杂!!好啦,不扯淡,前顺正传!

正文:

  • 正如标题,我要写的是一个处理下拉刷新,滚动底部自动加载更多,分页加载,自动切换显示网络失败布局,失败重试,暂无数据布局等等等各种UI逻辑的处理。一次封装好,一劳永逸,从此告别复制粘贴,屌丝逆袭,当上CEO,迎娶白富美。
    这样,以后工作中再有异步分页加载列表,下拉刷新,上拉加载更多,无网络时显示网络异常,加载出错时显示失败重试视图等等需求。嘿嘿,小case啦,我两分钟就能搞定~!完成任务还可以偷偷懒,基友群里吹吹水,人生啊,梦想啊。口说无凭,直接上效果图,实际开发中的示例代码。

效果图:

  • Screenshot_2016-10-18-13-35-35-584_com.mk.fortpea.png

代码:

  • Activity
/**
 * 意见反馈列表
 */
public class FeedbackListActivity extends TitleBarActivity implements SwipeRefreshLayout.OnRefreshListener {
    @Bind(R.id.recycler_view) XRecyclerView mRecyclerView;
    @Bind(R.id.content_view) SwipeRefreshLayout mContentView;
    @Bind(R.id.multiple_status_view) MultipleStatusView mMultipleStatusView;
    private FeedbackPresenter mPresenter;
    FeedbackAdapter mAdapter;
    LoadPageListDataView<FeedbackModel> mListDataView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback_list);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        ViewUtils.initVerticalLinearRecyclerView(this, mRecyclerView);
        setSwipeRefreshLayout(mContentView);
        mAdapter = new FeedbackAdapter(mRecyclerView, R.layout.item_feed_back);
        mPresenter = new FeedbackPresenter();
        mListDataView = new LoadPageListDataView<FeedbackModel>(this, mMultipleStatusView, mContentView, mRecyclerView, mAdapter);
        mPresenter.onTakeView(mListDataView);
        mPresenter.onCreate();
        mRecyclerView.setAdapter(mAdapter);
        mContentView.setOnRefreshListener(this);
        mMultipleStatusView.setOnRetryClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mPresenter != null) mPresenter.downPullRefreshList();
            }
        });
    }

    @Override
    public void onRefresh() {
        if (mPresenter != null) mPresenter.downPullRefreshList();
    }

    @Override
    public void onCreateCustomTitleBar(TitleBar titleBar) {
        setTitle(R.string.title_activity_feedback_list);
        setBackBtn();
        titleBar.setActionTextColor(Color.WHITE);
        titleBar.addAction(new TitleBar.TextAction(getString(R.string.add_feed_back)) {
            @Override
            public void performAction(View view) {
                IntentUtils.showActivity(FeedbackListActivity.this, AddFeedbackActivity.class);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mPresenter!=null) mPresenter.destroy();
        ButterKnife.unbind(this);
    }
}

布局:

  • Activity布局:
<?xml version="1.0" encoding="utf-8"?>
<com.mk.common.widget.emptyload.MultipleStatusView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/multiple_status_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/content_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <com.mk.common.widget.xrecyclerview.XRecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </android.support.v4.widget.SwipeRefreshLayout>

</com.mk.common.widget.emptyload.MultipleStatusView>
  • Adapter item布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical"
              android:paddingLeft="@dimen/margin_10"
              android:paddingRight="@dimen/margin_10"
              android:paddingTop="@dimen/margin_10">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/shape_gray_stroke"
        android:minHeight="@dimen/margin_100"
        android:orientation="vertical"
        >
        <TextView
            android:id="@+id/tv_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/margin_5"
            android:textColor="@android:color/black"
            tools:text="时间"
            />
        <View style="@style/HLine"/>
        <TextView
            android:id="@+id/tv_content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="@dimen/margin_5"
            android:text="意见反馈内容"
            />
    </LinearLayout>
</LinearLayout>

总结

  • 看,我就只需要写一个Activity,一个通用的Activity布局和相应Adapter item 布局就OK啦,代码都不超过100行,就完成了所有UI逻辑处理操作,是不是很6,很会偷懒,效率还非常高。由于时间问题,其他相关的类没有帖出来,也还没有将代码开源到Github上供参考,后续会上传我的Github上。本文旨在抛砖引玉的作用,大家相互交流,相互进步。嘿嘿,作为一个懒开发者,要做到先思考后动手,多看开源项目,少走弯路,多写可重用模块代码,少玩LOL!!哈哈...

相关文章

网友评论

本文标题:打造一个傻瓜式的处理下拉刷新,滚动底部自动加载更多,分页加载,自

本文链接:https://www.haomeiwen.com/subject/fxazyttx.html