package com.fly.customview.recyclerview;
import android.os.Handler;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import com.fly.customview.recyclerview.base.CommonActivityrecycler;
import com.fly.customview.recyclerview.bean.CourseBean;
import com.fly.customview.recyclerview.presenter.CoursePresenter;
import com.fly.customview.recyclerview.refresh.PullToRefreshLayout;
import com.fly.customview.recyclerview.refresh.PullableRecyclerView;
import com.fly.customview.recyclerview.refresh.RecyclerItemClickListener;
import java.util.ArrayList;
public class MainActivity extends CommonActivityrecycler<CoursePresenter> implements PullToRefreshLayout.OnRefreshListener{
private PullableRecyclerView mRecyclerView;
RecyclerViewAdapter mAdapter;
TextView delete;
@Override
protected CoursePresenter createPresent() {
return new CoursePresenter();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((PullToRefreshLayout) findViewById(R.id.activity_refresh_view)).setOnRefreshListener(this);
mRecyclerView = (PullableRecyclerView) findViewById(R.id.fragment_recycler);
delete = (TextView) findViewById(R.id.delete);
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
delSelections();
}
});
initRecyclerView(mRecyclerView);
setHeadFooterView(mRecyclerView);
mAdapter = new RecyclerViewAdapter(mPresent.items,this, this);
mRecyclerView.setAdapter(mAdapter);
mAdapter.initHeadFooterView(footer);
// 设置头部和脚部的宽度
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
if (mAdapter.isFooter(position)) {
return gridLayoutManager.getSpanCount();
}else{
return 1;
}
}
});
mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(this, mRecyclerView, new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) { /**recyclerView每个item的点击事件*/
if (isState && position < mPresent.items.size()) {
CheckBox checkBox = (CheckBox) view.findViewById(R.id.adapter_interest_course_check_box);
if (checkBox.isChecked()) {
checkBox.setChecked(false);
mPresent.selectItems.set(position, false);
} else {
checkBox.setChecked(true);
mPresent.selectItems.set(position, true);
}
mAdapter.notifyDataSetChanged();
Log.e("onItemClick","点击事件");
}else{
Log.e("onItemClick","点击进入");
}
}
@Override
public void onItemLongClick(View view, int position) { /**recyclerView每个item的长按事件*/
if (!isState) {
mPresent.selectItems.clear();
for (int i = 0; i < mPresent.items.size(); i++) {
mPresent.selectItems.add(false);
}
setState(true);
mAdapter.setIsState(true);
Log.e("onItemLongClick","onItemLongClick");
}else{
Log.e("onItemLongClick","长按事件");
}
}
}));
loadMore(mAdapter);
}
@Override
protected View initView(LayoutInflater inflater, ViewGroup container) {
return null;
}
@Override
protected void initViewsAndEvents() {
}
@Override
protected void initData(Bundle savedInstanceState) {
}
@Override
protected boolean isBindEventBusHere() {
return false;
}
/**
* 设置上拉加载更多
*
* @param adapter recyclerView适配器
*/
public void loadMore(final RecyclerViewAdapter adapter) {
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
private int lastVisibleItem;
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
lastVisibleItem = gridLayoutManager.findLastVisibleItemPosition(); // 滑动到最后一个
if (gridLayoutManager.findFirstCompletelyVisibleItemPosition() == 0) {
Log.e("onScrolled",gridLayoutManager.findFirstCompletelyVisibleItemPosition() + "");
mRecyclerView.nullData(true); // 可以下拉刷新
}else{
mRecyclerView.nullData(false); // 不能下拉刷新
}
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
// 效果在暂停时显示, 否则会导致重绘异常
if (newState == recyclerView.SCROLL_STATE_IDLE) {
if (lastVisibleItem + 1 == adapter.getItemCount()
&& adapter.isShowFooter()) {
if (mPresent.items != null && mPresent.items.size() >= 20) { // 真实开发中要设置mNews.size()大于加载分页显示的个数
loadLayout.setVisibility(View.VISIBLE);
//加载更多
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
CourseBean bean;
for (int i = 0; i < 10; i++) {
bean = new CourseBean();
bean.imageUrl = "http://content.52pk.com/files/100623/2230_102437_1_lit.jpg";
bean.name = "大家好";
mPresent.items.add(bean);
mPresent.selectItems.add(false);
}
mAdapter.notifyDataSetChanged();
}
}, 100);
}
}
}
}
});
}
/**下拉刷新*/
@Override
public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
pullToRefreshLayout.refreshFinish(-1);
}
/**废弃不用*/
@Override
public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
pullToRefreshLayout.loadmoreFinish(-1);
}
//设置当前状态 是否在多选模式
public void setState(boolean b) {
isState = b;
if (b) {
delete.setVisibility(View.VISIBLE);
} else {
delete.setVisibility(View.GONE);
}
}
//删除选中的方法
private void delSelections() {
if (!mPresent.selectItems.contains(true)) {
// CustomToast.showToast(context,"当前未选中项目");
}else{
for (int i = 0; i < mPresent.items.size(); i++) {
if (mPresent.selectItems.get(i)) {
mPresent.items.set(i, null);
}
}
while (mPresent.items.contains(null)) {
mPresent.items.remove(null);
}
mPresent.selectItems = new ArrayList<>();
for (int i = 0; i < mPresent.items.size(); i++) {
mPresent.selectItems.add(false);
}
mAdapter.setData(mPresent.items);
mAdapter.notifyDataSetChanged();
if (mPresent.items.size() == 0) {
mAdapter.setIsState(false);
setState(false);
return;
}
}
}
public ArrayList getSelectItems() {return mPresent.selectItems;}
}
源码下载:
http://download.csdn.net/detail/pengyu1801/9867363