Search in sources :

Example 1 with AutoLoadAdapter

use of xyz.zpayh.myadapter.adapter.AutoLoadAdapter in project SherlockAdapter by EvilBT.

the class AutoLoadMoreActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.act_auto_load_more);
    final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    mAdapter = new AutoLoadAdapter(this);
    recyclerView.setAdapter(mAdapter);
    final SwipeRefreshLayout refreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh);
    mAdapter.setAlwaysShowHead(true);
    mAdapter.setAlwaysShowFoot(true);
    mAdapter.addHeadLayout(R.layout.item_head);
    mAdapter.addFootLayout(R.layout.item_foot2);
    // 模拟数据
    data = new ArrayList<>();
    for (int i = 0; i < mTitles.length; i++) {
        ImageCard card = new ImageCard(mImageResId[i], mTitles[i]);
        data.add(card);
    }
    refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

        @Override
        public void onRefresh() {
            // 模拟刷新
            recyclerView.postDelayed(new Runnable() {

                @Override
                public void run() {
                    refreshLayout.setRefreshing(false);
                    mAdapter.setData(data);
                }
            }, 500);
        }
    });
    // 必须设置事件监听与开启auto
    mAdapter.openAutoLoadMore(true);
    mAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {

        @Override
        public void onLoadMore() {
            Log.d("Sherlock", "加载更多");
            // 模拟加载更多
            recyclerView.postDelayed(new Runnable() {

                @Override
                public void run() {
                    if (mState == LOAD_ADD) {
                        mAdapter.addData(data);
                    } else if (mState == LOAD_COMPLETED) {
                        mAdapter.loadCompleted();
                    } else if (mState == LOAD_FAILED) {
                        mAdapter.loadFailed();
                    }
                }
            }, 800);
        }
    });
    findViewById(R.id.action_add).setOnClickListener(this);
    findViewById(R.id.action_failed).setOnClickListener(this);
    findViewById(R.id.action_completed).setOnClickListener(this);
    findViewById(R.id.action_empty).setOnClickListener(this);
    findViewById(R.id.action_close).setOnClickListener(this);
    mShowHead = (CheckBox) findViewById(R.id.cb_show_head);
    mShowFoot = (CheckBox) findViewById(R.id.cb_show_foot);
    mShowHead.setChecked(true);
    mShowFoot.setChecked(true);
    mShowHead.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            mAdapter.setAlwaysShowHead(isChecked);
        }
    });
    mShowFoot.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            mAdapter.setAlwaysShowFoot(isChecked);
        }
    });
}
Also used : OnLoadMoreListener(xyz.zpayh.adapter.OnLoadMoreListener) AutoLoadAdapter(xyz.zpayh.myadapter.adapter.AutoLoadAdapter) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) SwipeRefreshLayout(android.support.v4.widget.SwipeRefreshLayout) ImageCard(xyz.zpayh.myadapter.data.ImageCard) RecyclerView(android.support.v7.widget.RecyclerView) CompoundButton(android.widget.CompoundButton)

Aggregations

SwipeRefreshLayout (android.support.v4.widget.SwipeRefreshLayout)1 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 RecyclerView (android.support.v7.widget.RecyclerView)1 CompoundButton (android.widget.CompoundButton)1 OnLoadMoreListener (xyz.zpayh.adapter.OnLoadMoreListener)1 AutoLoadAdapter (xyz.zpayh.myadapter.adapter.AutoLoadAdapter)1 ImageCard (xyz.zpayh.myadapter.data.ImageCard)1