真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

Android中RecyclerView拖拽、側(cè)刪功能的實現(xiàn)代碼

廢話不多說,下面展示一下效果。

創(chuàng)新互聯(lián)公司服務(wù)項目包括五大連池網(wǎng)站建設(shè)、五大連池網(wǎng)站制作、五大連池網(wǎng)頁制作以及五大連池網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,五大連池網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到五大連池省份的部分城市,未來相信會繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

Android中RecyclerView拖拽、側(cè)刪功能的實現(xiàn)代碼

這是GridView主文件實現(xiàn)。

public class GridViewActivity extends AppCompatActivity {
  RecyclerView mRecyclerView;
  List mStringList;
  RecyclerAdapter mRecyAdapter;
  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recyclerview);
    initView();
    initRecy();
  }
  private void initView() {
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    mRecyclerView = (RecyclerView) findViewById(R.id.view_recycler);
  }
  private void initRecy() {
    if (mStringList == null) {
      mStringList = new ArrayList<>();
    }
    mStringList.addAll(DataManager.getData(20 - mStringList.size()));
    mRecyAdapter = new RecyclerAdapter(R.layout.item_gridview, mStringList, true);
    mRecyclerView.setLayoutManager(new GridLayoutManager(this, 4));
    mRecyclerView.addItemDecoration(new DividerGriItemDecoration(this));
    mRecyclerView.setHasFixedSize(true);
    RecyItemTouchHelperCallback itemTouchHelperCallback = new RecyItemTouchHelperCallback(mRecyAdapter, false, true);
    final ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemTouchHelperCallback);
    itemTouchHelper.attachToRecyclerView(mRecyclerView);
    mRecyclerView.addOnItemTouchListener(new OnRecyclerItemClickListener(mRecyclerView) {
      @Override
      public void onItemClick(RecyclerView.ViewHolder viewHolder) {
        RecyclerAdapter.ViewHolder viewHolder1 = (RecyclerAdapter.ViewHolder) viewHolder;
        String tvString = viewHolder1.mTextView.getText().toString();
        Toast.makeText(GridViewActivity.this, "碰了一下 " + tvString, Toast.LENGTH_SHORT).show();
      }
      @Override
      public void onLongClick(RecyclerView.ViewHolder viewHolder) {
        RecyclerAdapter.ViewHolder viewHolder1 = (RecyclerAdapter.ViewHolder) viewHolder;
        String tvString = viewHolder1.mTextView.getText().toString();
        Toast.makeText(GridViewActivity.this, "長按不放可以拖動!", Toast.LENGTH_SHORT).show();
        if (viewHolder.getLayoutPosition() != 0) {
          itemTouchHelper.startDrag(viewHolder);
        }
      }
    });
    mRecyclerView.setAdapter(mRecyAdapter);
  }
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
      finish();
    }
    return super.onOptionsItemSelected(item);
  }
}

代碼不難理解,就是activity里面設(shè)置一大堆初始化的東西。RecyclerView 初始化,setLayoutManager、addItemDecoration、setHasFixedSize、itemTouchHelper、addOnItemTouchListener、setAdapter。拖拽效果關(guān)鍵代碼是itemTouchHelper.startDrag(viewHolder)。其他都圍繞著它來轉(zhuǎn)。

然后具體看看各個設(shè)置。

DataManager

public class DataManager {
  private static List sStringList = Arrays.asList("語文", "數(shù)學(xué)", "英語", "政治",
      "歷史", "化學(xué)", "生物", "地理", "體育", "音樂");
  public static final List getData(int number) {
    List stringList = new ArrayList<>();
    for (int i = 0; i < number; i++) {
      stringList.add(sStringList.get(i % sStringList.size()));
    }
    return stringList;
  }
}

DividerGriItemDecoration 這個繪畫了item的邊線

public class DividerGriItemDecoration extends RecyclerView.ItemDecoration {
  private static final int[] ATTRS = new int[]{android.R.attr.listDivider};
  private Drawable mDivider;
  private int lineWidth = 1;
  public DividerGriItemDecoration(GridViewActivity gridViewActivity) {
    final TypedArray array = gridViewActivity.obtainStyledAttributes(ATTRS);
    mDivider = array.getDrawable(0);
    array.recycle();
  }
  public DividerGriItemDecoration(int color) {
    mDivider = new ColorDrawable(color);
  }
  @Override
  public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
    drawHorizontal(c, parent);
    drawVertical(c, parent);
  }
  private void drawHorizontal(Canvas c, RecyclerView parent) {
    int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++) {
      final View child = parent.getChildAt(i);
      final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
      final int left = child.getLeft() - params.leftMargin;
      final int right = child.getRight() + params.rightMargin + lineWidth;
      final int top = child.getBottom() + params.bottomMargin;
      final int bottom = top + lineWidth;
      mDivider.setBounds(left, top, right, bottom);
      mDivider.draw(c);
    }
  }
  private void drawVertical(Canvas c, RecyclerView parent) {
    final int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++) {
      final View child = parent.getChildAt(i);
      final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
      final int top = child.getTop() - params.topMargin;
      final int bottom = child.getBottom() + params.bottomMargin;
      final int left = child.getRight() + params.rightMargin;
      final int right = left + lineWidth;
      mDivider.setBounds(left, top, right, bottom);
      mDivider.draw(c);
    }
  }
  @Override
  public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    outRect.set(0, 0, lineWidth, lineWidth);
  }
}

RecyclerAdapter 適配器,和ui建立連接

public class RecyclerAdapter extends RecyclerView.Adapter {
  private int item_layout;
  private List mDataList;
  private List mInts;
  private boolean isFirstSpecial;
  public RecyclerAdapter(int item_gridview, List dataList, boolean isFisrtSpecial) {
    this(item_gridview, dataList);
    this.isFirstSpecial = isFisrtSpecial;
  }
  public RecyclerAdapter(int item_gridview, List dataList) {
    this.item_layout = item_gridview;
    this.mDataList = dataList;
    mInts = Arrays.asList(R.mipmap.ic_launcher, R.mipmap.ic_launcher,
        R.mipmap.ic_launcher, R.mipmap.ic_launcher,
        R.mipmap.ic_launcher, R.mipmap.ic_launcher);
  }
  @Override
  public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(item_layout, parent, false);
    return new ViewHolder(view);
  }
  @Override
  public void onBindViewHolder(ViewHolder holder, int position) {
    String str = mDataList.get(position);
    if (isFirstSpecial && position == 0) {
      holder.itemView.setBackgroundColor(Color.LTGRAY);
      holder.mTextView.setText("不許動");
      holder.mImageView.setImageResource(R.mipmap.ic_launcher);
    } else {
      holder.itemView.setBackgroundColor(Color.WHITE);
      holder.mTextView.setText(str);
      holder.mImageView.setImageResource(mInts.get(position % mInts.size()));
    }
  }
  @Override
  public int getItemCount() {
    return mDataList == null ? 0 : mDataList.size();
  }
  public List getDataList() {
    return mDataList;
  }
  class ViewHolder extends RecyclerView.ViewHolder {
    TextView mTextView;
    ImageView mImageView;
    public ViewHolder(View itemView) {
      super(itemView);
      mTextView = itemView.findViewById(R.id.tv_item);
      mImageView = itemView.findViewById(R.id.img_item);
    }
  }
}

OnRecyclerItemClickListener,點擊事件 。我們借用手勢工具類GestureDetectorCompat 來操作

public abstract class OnRecyclerItemClickListener implements RecyclerView.OnItemTouchListener{
  private GestureDetectorCompat mGestureDetectorCompat;
  private RecyclerView mRecyclerView;
  public OnRecyclerItemClickListener(RecyclerView mRecyclerView) {
    this.mRecyclerView = mRecyclerView;
    mGestureDetectorCompat = new GestureDetectorCompat(mRecyclerView.getContext(),
        new ItemTouchHelperGestureListener());
  }
  @Override
  public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
    mGestureDetectorCompat.onTouchEvent(e);
    return false;
  }
  @Override
  public void onTouchEvent(RecyclerView rv, MotionEvent e) {
    mGestureDetectorCompat.onTouchEvent(e);
  }
  @Override
  public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
  }
  //回調(diào)事件
  public abstract void onItemClick(RecyclerView.ViewHolder viewHolder);
  public abstract void onLongClick(RecyclerView.ViewHolder viewHolder);
  private class ItemTouchHelperGestureListener extends GestureDetector.SimpleOnGestureListener {
    @Override
    public boolean onSingleTapUp(MotionEvent e) {
      View childViewUnder = mRecyclerView.findChildViewUnder(e.getX(), e.getY());
      if (childViewUnder != null) {
        RecyclerView.ViewHolder childViewHolder = mRecyclerView.getChildViewHolder(childViewUnder);
        onItemClick(childViewHolder);
      }
      return true;
    }
    @Override
    public void onLongPress(MotionEvent e) {
      View childViewUnder = mRecyclerView.findChildViewUnder(e.getX(), e.getY());
      if (childViewUnder != null) {
        RecyclerView.ViewHolder childViewHolder = mRecyclerView.getChildViewHolder(childViewUnder);
        onLongClick(childViewHolder);
      }
    }
  }
}

RecyItemTouchHelperCallback

public class RecyItemTouchHelperCallback extends ItemTouchHelper.Callback {
  RecyclerView.Adapter mAdapter;
  boolean isSwipeEnable;
  boolean isFirstDragUnable;
  public RecyItemTouchHelperCallback(RecyclerView.Adapter mAdapter) {
    this.mAdapter = mAdapter;
    isSwipeEnable = true;
    isFirstDragUnable = false;
  }
  public RecyItemTouchHelperCallback(RecyclerView.Adapter mAdapter, boolean isSwipeEnable, boolean isFirstDragUnable) {
    this.mAdapter = mAdapter;
    this.isSwipeEnable = isSwipeEnable;
    this.isFirstDragUnable = isFirstDragUnable;
  }
//  獲取Touch的響應(yīng)方向
  @Override
  public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
    if (recyclerView.getLayoutManager() instanceof GridLayoutManager) {//網(wǎng)格布局時候
      int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN |
          ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
      int swipeFlags = 0;
      return makeMovementFlags(dragFlags, swipeFlags);
    } else {//list布局時候
      int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
      int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
      return makeMovementFlags(dragFlags, swipeFlags);
    }
  }
  @Override
  public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
    int fromPosition = viewHolder.getAdapterPosition();
    int toPosition = target.getAdapterPosition();
    if (isFirstDragUnable && toPosition == 0) {
      return false;
    }
    //重新更新排序
    if (fromPosition < toPosition) {
      for (int i = fromPosition; i < toPosition; i++) {
        Collections.swap(((RecyclerAdapter) mAdapter).getDataList(), i, i + 1);
      }
    } else {
      for (int i = fromPosition; i > toPosition; i--) {
        Collections.swap(((RecyclerAdapter) mAdapter).getDataList(), i, i - 1);
      }
    }
    //刷新
    mAdapter.notifyItemMoved(fromPosition, toPosition);
    return true;
  }
  /**
   * 側(cè)滑刪除后會回調(diào)的方法
   */
  @Override
  public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
    int adapterPosition = viewHolder.getAdapterPosition();
    mAdapter.notifyItemRemoved(adapterPosition);
    ((RecyclerAdapter)mAdapter).getDataList().remove(adapterPosition);
  }
  @Override
  public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
    if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
      viewHolder.itemView.setBackgroundColor(Color.LTGRAY);
    }
    super.onSelectedChanged(viewHolder, actionState);
  }
  @Override
  public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
    super.clearView(recyclerView, viewHolder);
    viewHolder.itemView.setBackgroundColor(Color.WHITE);
  }
  @Override
  public boolean isLongPressDragEnabled() {
    return !isFirstDragUnable;
  }
  @Override
  public boolean isItemViewSwipeEnabled() {
    return isSwipeEnable;
  }
}

下面是list的相關(guān)設(shè)置

同樣,listview也是一頓初始化設(shè)置。

public class ListViewActivity extends AppCompatActivity {
  RecyclerView mRecyclerView;
  List mStringList;
  RecyclerAdapter mRecyAdapter;
  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recyclerview);
    initView();
    initRecy();
  }
  private void initRecy() {
    if (mStringList == null) {
      mStringList = new ArrayList<>();
    }
    mStringList.addAll(DataManager.getData(15 - mStringList.size()));
    mRecyAdapter = new RecyclerAdapter(R.layout.item_listview, mStringList);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    mRecyclerView.addItemDecoration(new DividerListItemDecoration(this, LinearLayoutManager.VERTICAL));
    mRecyclerView.setHasFixedSize(true);
    RecyItemTouchHelperCallback itemTouchHelperCallback = new RecyItemTouchHelperCallback(mRecyAdapter);
    final ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemTouchHelperCallback);
    itemTouchHelper.attachToRecyclerView(mRecyclerView);
    mRecyclerView.addOnItemTouchListener(new OnRecyclerItemClickListener(mRecyclerView) {
      @Override
      public void onItemClick(RecyclerView.ViewHolder viewHolder) {
        RecyclerAdapter.ViewHolder viewHolder1 = (RecyclerAdapter.ViewHolder) viewHolder;
        String tvString = viewHolder1.mTextView.getText().toString();
        Toast.makeText(ListViewActivity.this, "碰了一下 " + tvString, Toast.LENGTH_SHORT).show();
      }
      @Override
      public void onLongClick(RecyclerView.ViewHolder viewHolder) {
        Toast.makeText(ListViewActivity.this, "長按不放可以拖動!", Toast.LENGTH_SHORT).show();
      }
    });
    mRecyclerView.setAdapter(mRecyAdapter);
  }
  private void initView() {
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    mRecyclerView = (RecyclerView) findViewById(R.id.view_recycler);
  }
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
      finish();
    }
    return super.onOptionsItemSelected(item);
  }
}

DividerListItemDecoration

public class DividerListItemDecoration extends RecyclerView.ItemDecoration {
  private static final int[] ATTRS = new int[] {android.R.attr.listDivider};
  public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
  public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
  private Drawable mDivider;
  private int mOrientation;
  public DividerListItemDecoration() {
    super();
  }
  public DividerListItemDecoration(Context context, int orientation) {
    final TypedArray array = context.obtainStyledAttributes(ATTRS);
    mDivider = array.getDrawable(0);
    array.recycle();
    this.mOrientation = orientation;
  }
  public DividerListItemDecoration(Context context, int orientation, int drawableId) {
    mDivider = ContextCompat.getDrawable(context, drawableId);
    setOrientation(orientation);
  }
  @Override
  public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
    super.onDraw(c, parent, state);
  }
  @Override
  public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
    super.onDrawOver(c, parent, state);
  }
  @Override
  public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    if (mOrientation == VERTICAL_LIST) {
      outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
    } else {
      outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
    }
    super.getItemOffsets(outRect, view, parent, state);
  }
  public void setOrientation(int orientation) {
    if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
      throw new IllegalArgumentException("invalid orientaion");
    }
    mOrientation = orientation;
  }
}

源碼下載:http://download.csdn.net/download/loongago/9972876

總結(jié)

以上所述是小編給大家介紹的Android中RecyclerView拖拽、側(cè)刪功能的實現(xiàn)代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對創(chuàng)新互聯(lián)網(wǎng)站的支持!


文章名稱:Android中RecyclerView拖拽、側(cè)刪功能的實現(xiàn)代碼
鏈接分享:http://weahome.cn/article/gdjhhd.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部