赞
踩
功能效果图类似于APP的桌面的图标拖动,各位阅读者就不截图了,直接贴代码了。
继承ItemTouchHelper重写方法
- private MyAdapter myAdapter ;
- private ItemTouchHelper mItemTouchHelper;
-
-
- public class MyItemTouchHelper extends ItemTouchHelper.Callback{
-
-
- @Override
- public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
- if (recyclerView.getLayoutManager() instanceof GridLayoutManager) {
- final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN |ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
- final int swipeFlags = 0;
- return makeMovementFlags(dragFlags, swipeFlags);
- } else {
- final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
- final int swipeFlags = 0;
- return makeMovementFlags(dragFlags, swipeFlags);
- }
- }
-
-
-
- @Override
- public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
- //得到当拖拽的viewHolder的Position
- int fromPosition = viewHolder.getAdapterPosition();
- //拿到当前拖拽到的item的viewHolder
- int toPosition = target.getAdapterPosition();
- if (fromPosition < toPosition) {
- for (int i = fromPosition; i < toPosition; i++) {
- Collections.swap(list, i, i + 1);
- }
- } else {
- for (int i = fromPosition; i > toPosition; i--) {
- Collections.swap(list, i, i - 1);
- }
- }
- myAdapter.notifyItemMoved(fromPosition, toPosition);
- return true;
- }
-
- @Override
- public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
-
- }
-
-
- /**
- * 长按选中Item的时候开始调用
- *
- * @param viewHolder
- * @param actionState
- */
- @Override
- public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
- if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
- viewHolder.itemView.setBackgroundColor(Color.GRAY);
- }
- super.onSelectedChanged(viewHolder, actionState);
- }
-
- /**
- * 手指松开的时候还原
- * @param recyclerView
- * @param viewHolder
- */
- @RequiresApi(api = Build.VERSION_CODES.M)
- @Override
- public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
- super.clearView(recyclerView, viewHolder);
- viewHolder.itemView.setBackgroundColor(getColor(R.color.black_alpha_01));
- }
-
- /**
- * 重写拖拽不可用
- * @return
- */
- @Override
- public boolean isLongPressDragEnabled() {
- return true;
- }
- }
适配器如下
- public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{
- List<MainTabNewBean.ResultdataBean.ListBean> list ;
- public MyAdapter (List<MainTabNewBean.ResultdataBean.ListBean> list){
- this.list = list ;
- }
- @NonNull
- @Override
- public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
- return new MyViewHolder(LayoutInflater.from(NewMainTableActivity.this).inflate(R.layout.item_new_main,parent,false));
- }
-
-
-
- @Override
- public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
- holder.tvMainText.setText(list.get(position).getSubjectTitle());
- Glide.with(NewMainTableActivity.this).load(list.get(position).getSubjectIcon()).into(holder.ivMainImg);
- holder.tvMainText.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // NewMainTableActivity.this.startActivity(new Intent(MainActivity.this,VideoNomarlActivity.class));
- }
- });
- }
-
- @Override
- public int getItemCount() {
- return list.size();
- }
-
- public class MyViewHolder extends RecyclerView.ViewHolder{
-
- public ImageView ivMainImg ;
- public TextView tvMainText ;
- public LinearLayout llMainItemBac ;
- public MyViewHolder(@NonNull View itemView) {
- super(itemView);
- ivMainImg = itemView.findViewById(R.id.ivMainImg);
- tvMainText = itemView.findViewById(R.id.tvMainText);
- llMainItemBac = itemView.findViewById(R.id.llMainItemBac);
- }
- }
- }
item_new_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@color/black_alpha_01"
- android:baselineAligned="false"
- android:orientation="vertical" >
-
- <LinearLayout
- android:id="@+id/llMainItemBac"
- android:layout_margin="0.5dp"
- android:padding="4dp"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@color/white_color"
- android:gravity="center">
-
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@color/clear_color"
- android:gravity="center">
-
- <ImageView
- android:id="@+id/ivMainImg"
- android:layout_width="40dip"
- android:layout_height="40dip"
- android:layout_centerHorizontal="true"
- android:scaleType="fitXY"
- />
-
- <TextView
- android:id="@+id/tvMainText"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_below="@id/ivMainImg"
- android:layout_marginLeft="10dip"
- android:layout_marginRight="10dip"
- android:layout_marginTop="10dip"
- android:background="@color/clear_color"
- android:ellipsize="end"
- android:gravity="center_vertical|center_horizontal"
- android:textColor="@color/ua_dark_gray_text_color"
- android:textSize="16sp"
- android:text=""/>
- </RelativeLayout>
- </LinearLayout>
-
-
- </LinearLayout>
Activity中的实现
MainTabNewBean是根据自己的数据类型生成的,可以根据自己的需求去改
- rv = findViewById(R.id.rc_people_list);
- GridLayoutManager gridLayoutManager=new GridLayoutManager(this,3) ;
- rv.setLayoutManager(gridLayoutManager);
-
-
- MainTabNewBean mainTabNewBean = new Gson().fromJson(s,MainTabNewBean.class);
- list = mainTabNewBean.getResultdata().getList();
- rv.setAdapter(myAdapter = new MyAdapter(list));
- Log.i("ewqeqw", "getDate: "+list.size());
- mItemTouchHelper = new ItemTouchHelper(new MyItemTouchHelper());
- mItemTouchHelper.attachToRecyclerView(rv);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。