|
| 1 | +package mcxtzhang.swipedelmenu.viewpager; |
| 2 | + |
| 3 | +import android.graphics.Color; |
| 4 | +import android.os.Bundle; |
| 5 | +import android.support.annotation.Nullable; |
| 6 | +import android.support.v4.app.Fragment; |
| 7 | +import android.support.v7.widget.LinearLayoutManager; |
| 8 | +import android.support.v7.widget.RecyclerView; |
| 9 | +import android.util.Log; |
| 10 | +import android.view.LayoutInflater; |
| 11 | +import android.view.MotionEvent; |
| 12 | +import android.view.View; |
| 13 | +import android.view.ViewGroup; |
| 14 | +import android.widget.Toast; |
| 15 | + |
| 16 | +import com.mcxtzhang.commonadapter.rv.CommonAdapter; |
| 17 | +import com.mcxtzhang.commonadapter.rv.ViewHolder; |
| 18 | +import com.mcxtzhang.swipemenulib.SwipeMenuLayout; |
| 19 | + |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +import mcxtzhang.listswipemenudemo.R; |
| 24 | +import mcxtzhang.swipedelmenu.SwipeBean; |
| 25 | + |
| 26 | +/** |
| 27 | + * Intro: |
| 28 | + * Author: zhangxutong |
| 29 | + |
| 30 | + * Home Page: http://blog.csdn.net/zxt0601 |
| 31 | + * Created: 2017/9/27. |
| 32 | + * History: |
| 33 | + */ |
| 34 | + |
| 35 | +public class FullDemoFragment extends Fragment { |
| 36 | + public static FullDemoFragment newInstance(int position) { |
| 37 | + Bundle args = new Bundle(); |
| 38 | + FullDemoFragment fragment = new FullDemoFragment(); |
| 39 | + args.putInt("index", position); |
| 40 | + fragment.setArguments(args); |
| 41 | + return fragment; |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public void onCreate(@Nullable Bundle savedInstanceState) { |
| 46 | + super.onCreate(savedInstanceState); |
| 47 | + mIndex = getArguments().getInt("index"); |
| 48 | + } |
| 49 | + |
| 50 | + private static final String TAG = "zxt"; |
| 51 | + private RecyclerView mRecyclerView; |
| 52 | + private CommonAdapter<SwipeBean> mAdapter; |
| 53 | + private LinearLayoutManager mLayoutManager; |
| 54 | + private List<SwipeBean> mDatas; |
| 55 | + private int mIndex; |
| 56 | + |
| 57 | + @Nullable |
| 58 | + @Override |
| 59 | + public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { |
| 60 | + View inflate = LayoutInflater.from(getContext()).inflate(R.layout.fragment_full_del_demo, container, false); |
| 61 | + |
| 62 | + mRecyclerView = (RecyclerView) inflate.findViewById(R.id.rv); |
| 63 | + //区分一下不同页面 |
| 64 | + switch (mIndex) { |
| 65 | + case 0: |
| 66 | + mRecyclerView.setBackgroundColor(Color.WHITE); |
| 67 | + break; |
| 68 | + case 1: |
| 69 | + mRecyclerView.setBackgroundColor(Color.YELLOW); |
| 70 | + break; |
| 71 | + default: |
| 72 | + mRecyclerView.setBackgroundColor(Color.GREEN); |
| 73 | + break; |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + initDatas(); |
| 78 | + mAdapter = new CommonAdapter<SwipeBean>(getContext(), mDatas, R.layout.item_cst_swipe) { |
| 79 | + @Override |
| 80 | + public void convert(final ViewHolder holder, SwipeBean swipeBean) { |
| 81 | + ((SwipeMenuLayout) holder.itemView).setLeftSwipe(mIndex == 0 ? false : true);//这句话关掉IOS阻塞式交互效果 并依次打开左滑右滑 |
| 82 | + |
| 83 | + holder.setText(R.id.content, swipeBean.name + (mIndex == 0 ? "我左青龙" : "我右白虎")); |
| 84 | + |
| 85 | + //验证长按 |
| 86 | + holder.setOnLongClickListener(R.id.content, new View.OnLongClickListener() { |
| 87 | + @Override |
| 88 | + public boolean onLongClick(View v) { |
| 89 | + Toast.makeText(mContext, "longclig", Toast.LENGTH_SHORT).show(); |
| 90 | + Log.d("TAG", "onLongClick() called with: v = [" + v + "]"); |
| 91 | + return false; |
| 92 | + } |
| 93 | + }); |
| 94 | + |
| 95 | + holder.setOnClickListener(R.id.btnDelete, new View.OnClickListener() { |
| 96 | + @Override |
| 97 | + public void onClick(View v) { |
| 98 | + int pos = holder.getLayoutPosition(); |
| 99 | + if (pos >= 0 && pos < mDatas.size()) { |
| 100 | + Toast.makeText(getContext(), "删除:" + pos, Toast.LENGTH_SHORT).show(); |
| 101 | + mDatas.remove(pos); |
| 102 | + mAdapter.notifyItemRemoved(pos);//推荐用这个 |
| 103 | + //如果删除时,不使用mAdapter.notifyItemRemoved(pos),则删除没有动画效果, |
| 104 | + //且如果想让侧滑菜单同时关闭,需要同时调用 ((SwipeMenuLayout) holder.itemView).quickClose(); |
| 105 | + //mAdapter.notifyDataSetChanged(); |
| 106 | + } |
| 107 | + } |
| 108 | + }); |
| 109 | + //注意事项,设置item点击,不能对整个holder.itemView设置咯,只能对第一个子View,即原来的content设置,这算是局限性吧。 |
| 110 | + (holder).setOnClickListener(R.id.content, new View.OnClickListener() { |
| 111 | + @Override |
| 112 | + public void onClick(View v) { |
| 113 | + Toast.makeText(mContext, "onClick:" + mDatas.get(holder.getAdapterPosition()).name, Toast.LENGTH_SHORT).show(); |
| 114 | + Log.d("TAG", "onClick() called with: v = [" + v + "]"); |
| 115 | + } |
| 116 | + }); |
| 117 | + //置顶: |
| 118 | + holder.setOnClickListener(R.id.btnTop, new View.OnClickListener() { |
| 119 | + @Override |
| 120 | + public void onClick(View v) { |
| 121 | + int pos = holder.getLayoutPosition(); |
| 122 | + |
| 123 | + if (pos > 0 && pos < mDatas.size()) { |
| 124 | + SwipeBean swipeBean = mDatas.get(pos); |
| 125 | + mDatas.remove(swipeBean); |
| 126 | + mAdapter.notifyItemInserted(0); |
| 127 | + mDatas.add(0, swipeBean); |
| 128 | + mAdapter.notifyItemRemoved(pos + 1); |
| 129 | + if (mLayoutManager.findFirstVisibleItemPosition() == 0) { |
| 130 | + mRecyclerView.scrollToPosition(0); |
| 131 | + } |
| 132 | + //notifyItemRangeChanged(0,holder.getAdapterPosition()+1); |
| 133 | + } |
| 134 | + |
| 135 | + } |
| 136 | + }); |
| 137 | + |
| 138 | + |
| 139 | + } |
| 140 | + }; |
| 141 | + |
| 142 | + mRecyclerView.setAdapter(mAdapter); |
| 143 | + //mRecyclerView.setLayoutManager(mLayoutManager = new GridLayoutManager(getContext(), 2)); |
| 144 | + mRecyclerView.setLayoutManager(mLayoutManager = new LinearLayoutManager(getContext())); |
| 145 | + |
| 146 | + //6 2016 10 21 add , 增加viewChache 的 get()方法, |
| 147 | + // 可以用在:当点击外部空白处时,关闭正在展开的侧滑菜单。我个人觉得意义不大, |
| 148 | + mRecyclerView.setOnTouchListener(new View.OnTouchListener() { |
| 149 | + @Override |
| 150 | + public boolean onTouch(View v, MotionEvent event) { |
| 151 | + if (event.getAction() == MotionEvent.ACTION_UP) { |
| 152 | + SwipeMenuLayout viewCache = SwipeMenuLayout.getViewCache(); |
| 153 | + if (null != viewCache) { |
| 154 | + viewCache.smoothClose(); |
| 155 | + } |
| 156 | + } |
| 157 | + return false; |
| 158 | + } |
| 159 | + }); |
| 160 | + |
| 161 | + return inflate; |
| 162 | + } |
| 163 | + |
| 164 | + |
| 165 | + private void initDatas() { |
| 166 | + mDatas = new ArrayList<>(); |
| 167 | + for (int i = 0; i < 20; i++) { |
| 168 | + mDatas.add(new SwipeBean("" + i)); |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments