package com.lzx.demo.ui; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Toast; import com.github.jdsjlzx.interfaces.OnRefreshListener; import com.github.jdsjlzx.recyclerview.LRecyclerView; import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter; import com.github.jdsjlzx.recyclerview.ProgressStyle; import com.github.jdsjlzx.util.RecyclerViewStateUtils; import com.github.jdsjlzx.view.LoadingFooter; import com.lzx.demo.R; import com.lzx.demo.adapter.SwipeMenuAdapter; import com.lzx.demo.bean.ItemModel; import com.lzx.demo.util.NetworkUtils; import com.lzx.demo.util.TLog; import com.lzx.demo.view.SampleHeader; import java.lang.ref.WeakReference; import java.util.ArrayList; /** * 滑动删除 */ public class SwipeDeleteActivity extends AppCompatActivity{ private static final String TAG = "lzx"; /**服务器端一共多少条数据*/ private static final int TOTAL_COUNTER = 64; /**每一页展示多少条数据*/ private static final int REQUEST_COUNT = 10; /**已经获取到多少条数据了*/ private static int mCurrentCounter = 0; private LRecyclerView mRecyclerView = null; private SwipeMenuAdapter mDataAdapter = null; private PreviewHandler mHandler = new PreviewHandler(this); private LRecyclerViewAdapter mLRecyclerViewAdapter = null; private boolean isRefresh = false; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sample_ll_activity); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); mRecyclerView = (LRecyclerView) findViewById(R.id.list); ArrayList<ItemModel> dataList = new ArrayList<>(); for (int i = 0; i < 10; i++) { ItemModel itemModel = new ItemModel(); itemModel.title = "item" + i; dataList.add(itemModel); mCurrentCounter += dataList.size(); } mDataAdapter = new SwipeMenuAdapter(this); mDataAdapter.setDataList(dataList); mDataAdapter.setOnDelListener(new SwipeMenuAdapter.onSwipeListener() { @Override public void onDel(int pos) { Toast.makeText(SwipeDeleteActivity.this, "删除:" + pos, Toast.LENGTH_SHORT).show(); //RecyclerView关于notifyItemRemoved的那点小事 参考:http://blog.csdn.net/jdsjlzx/article/details/52131528 mDataAdapter.getDataList().remove(pos); mDataAdapter.notifyItemRemoved(pos);//推荐用这个 if(pos != (mDataAdapter.getDataList().size())){ // 如果移除的是最后一个,忽略 注意:这里的mDataAdapter.getDataList()不需要-1,因为上面已经-1了 mDataAdapter.notifyItemRangeChanged(pos, mDataAdapter.getDataList().size() - pos); } //且如果想让侧滑菜单同时关闭,需要同时调用 ((CstSwipeDelMenu) holder.itemView).quickClose(); } @Override public void onTop(int pos) {//置顶功能有bug,后续解决 TLog.error("onTop pos = " + pos); ItemModel itemModel = mDataAdapter.getDataList().get(pos); mDataAdapter.getDataList().remove(pos); mDataAdapter.notifyItemRemoved(pos); mDataAdapter.getDataList().add(0, itemModel); mDataAdapter.notifyItemInserted(0); if(pos != (mDataAdapter.getDataList().size())){ // 如果移除的是最后一个,忽略 mDataAdapter.notifyItemRangeChanged(0, mDataAdapter.getDataList().size() - 1,"jdsjlzx"); } mRecyclerView.scrollToPosition(0); } }); mLRecyclerViewAdapter = new LRecyclerViewAdapter(mDataAdapter); mRecyclerView.setAdapter(mLRecyclerViewAdapter); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader); mRecyclerView.setArrowImageView(R.drawable.ic_pulltorefresh_arrow); mLRecyclerViewAdapter.addHeaderView(new SampleHeader(this)); mRecyclerView.setOnRefreshListener(new OnRefreshListener() { @Override public void onRefresh() { mDataAdapter.clear(); mLRecyclerViewAdapter.notifyDataSetChanged();//fix bug:crapped or attached views may not be recycled. isScrap:false isAttached:true mCurrentCounter = 0; isRefresh = true; requestData(); } }); mRecyclerView.refresh(); //侧滑删除请不要使用下面接口,SwipeMenuAdapter内部实现item点击事件 /*mLRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(View view, int position) { ItemModel item = mDataAdapter.getDataList().get(position); AppToast.showShortText(SwipeDeleteActivity.this, item.title); } });*/ } private void notifyDataSetChanged() { mLRecyclerViewAdapter.notifyDataSetChanged(); } private void addItems(ArrayList<ItemModel> list) { mDataAdapter.addAll(list); mCurrentCounter += list.size(); } private static class PreviewHandler extends Handler { private WeakReference<SwipeDeleteActivity> ref; PreviewHandler(SwipeDeleteActivity activity) { ref = new WeakReference<>(activity); } @Override public void handleMessage(Message msg) { final SwipeDeleteActivity activity = ref.get(); if (activity == null || activity.isFinishing()) { return; } switch (msg.what) { case -1: if(activity.isRefresh){ activity.mDataAdapter.clear(); mCurrentCounter = 0; } int currentSize = activity.mDataAdapter.getItemCount(); //模拟组装10个数据 ArrayList<ItemModel> newList = new ArrayList<>(); for (int i = 0; i < 10; i++) { if (newList.size() + currentSize >= TOTAL_COUNTER) { break; } ItemModel item = new ItemModel(); item.id = currentSize + i; item.title = "item" + (item.id); newList.add(item); } activity.addItems(newList); activity.mRecyclerView.refreshComplete(REQUEST_COUNT); activity.notifyDataSetChanged(); break; case -2: activity.notifyDataSetChanged(); break; case -3: activity.mRecyclerView.refreshComplete(REQUEST_COUNT); activity.notifyDataSetChanged(); break; default: break; } } } private View.OnClickListener mFooterClick = new View.OnClickListener() { @Override public void onClick(View v) { RecyclerViewStateUtils.setFooterViewState(SwipeDeleteActivity.this, mRecyclerView, REQUEST_COUNT, LoadingFooter.State.Loading, null); requestData(); } }; /** * 模拟请求网络 */ private void requestData() { Log.d(TAG, "requestData"); new Thread() { @Override public void run() { super.run(); try { Thread.sleep(800); } catch (InterruptedException e) { e.printStackTrace(); } //模拟一下网络请求失败的情况 if(NetworkUtils.isNetAvailable(SwipeDeleteActivity.this)) { mHandler.sendEmptyMessage(-1); } else { mHandler.sendEmptyMessage(-3); } } }.start(); } @Override public boolean onPrepareOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main_refresh, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == android.R.id.home) { finish(); } else if (item.getItemId() == R.id.menu_refresh) { mRecyclerView.forceToRefresh(); } return true; } }