我有一个项目,我需要以两种方式在一行上滑动以启动 *** 作.
向左滑动应从特定列表中删除有问题的项目(不将其从原始列表中删除,因此将其保留在recyclervIEw数据集中),向右滑动应将相关项目添加到另一个列表(同样不是原始列表) .
我实现了一个itemtouchhelperAdapter和一个itemtouchhelperCallback,我可以检测到左/右滑动,但视图会从屏幕上移开,我留下一个空白矩形.有任何想法吗?@H_502_3@
public interface itemtouchhelperAdapter {/** * Called when an item has been dismissed by a swipe.<br/> * <br/> * Implementations should call {@link RecyclerVIEw.Adapter#notifyItemRemoved(int)} after * adjusting the underlying data to reflect this removal. * * @param position The position of the item dismissed. * @see RecyclerVIEw#getAdapterpositionFor(RecyclerVIEw.VIEwHolder) * @see RecyclerVIEw.VIEwHolder#getAdapterposition() */voID onItemleftSwipe(int position);voID onItemRightSwipe(int position);
}
公共类itemtouchhelperCallback扩展itemtouchhelper.Callback {
private final itemtouchhelperAdapter mAdapter;public itemtouchhelperCallback(itemtouchhelperAdapter adapter) { mAdapter = adapter;}@OverrIDepublic int getMovementFlags(RecyclerVIEw recyclerVIEw, RecyclerVIEw.VIEwHolder vIEwHolder) { // int dragFlags = itemtouchhelper.UP | itemtouchhelper.DOWN; int swipeFlags = itemtouchhelper.START | itemtouchhelper.END; return makeMovementFlags(0, swipeFlags);}@OverrIDepublic boolean onMove(RecyclerVIEw recyclerVIEw, RecyclerVIEw.VIEwHolder vIEwHolder, RecyclerVIEw.VIEwHolder target) { return false;}@OverrIDepublic voID onSwiped(RecyclerVIEw.VIEwHolder vIEwHolder, int direction) { if (direction == itemtouchhelper.START) mAdapter.onItemleftSwipe(vIEwHolder.getAdapterposition()); else if (direction == itemtouchhelper.END) mAdapter.onItemRightSwipe(vIEwHolder.getAdapterposition()); else System.out.println("direction: " + direction);}@OverrIDepublic boolean isLongPressDragEnabled() { return false;}@OverrIDepublic boolean isItemVIEwSwipeEnabled() { return true;}
}
这是在我的适配器类中:
@OverrIDepublic voID onItemleftSwipe(int position) { System.out.println("swiped left on " + mDataset.get(position).getname());}@OverrIDepublic voID onItemRightSwipe(int position) { System.out.println("swiped right on " + mDataset.get(position).getname());}
解决方法:
public class MainActivity extends AppCompatActivity { RecyclerVIEw mRecyclerVIEw; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); Toolbar toolbar = (Toolbar) findVIEwByID(R.ID.toolbar); setSupportActionbar(toolbar); mRecyclerVIEw = (RecyclerVIEw) findVIEwByID(R.ID.recycler_vIEw); setUpRecyclerVIEw(); } private voID setUpRecyclerVIEw() { mRecyclerVIEw.setLayoutManager(new linearlayoutmanager(this)); mRecyclerVIEw.setAdapter(new TestAdapter()); mRecyclerVIEw.setHasFixedSize(true); setUpitemtouchhelper(); setUpAnimationDecoratorHelper(); } private voID setUpitemtouchhelper() { itemtouchhelper.SimpleCallback simpleItemtouchCallback = new itemtouchhelper.SimpleCallback(0, itemtouchhelper.left | itemtouchhelper.RIGHT) { Drawable background; Drawable xMark; int xMarkmargin; boolean initiated; private voID init() { background = new colorDrawable(color.WHITE); xMark = ContextCompat.getDrawable(MainActivity.this, R.drawable.ic_clear_24dp); xMark.setcolorFilter(color.WHITE, PorterDuff.Mode.SRC_Atop); xMarkmargin = (int) MainActivity.this.getResources().getDimension(R.dimen.ic_clear_margin); initiated = true; } @OverrIDe public boolean onMove(RecyclerVIEw recyclerVIEw, RecyclerVIEw.VIEwHolder vIEwHolder, RecyclerVIEw.VIEwHolder target) { return false; } @OverrIDe public int getSwipeDirs(RecyclerVIEw recyclerVIEw, RecyclerVIEw.VIEwHolder vIEwHolder) { int position = vIEwHolder.getAdapterposition(); TestAdapter testAdapter = (TestAdapter)recyclerVIEw.getAdapter(); if (testAdapter.isUndoOn() && testAdapter.isPendingRemoval(position)) { return 0; } return super.getSwipeDirs(recyclerVIEw, vIEwHolder); } @OverrIDe public voID onSwiped(RecyclerVIEw.VIEwHolder vIEwHolder, int swipeDir) { int swipedposition = vIEwHolder.getAdapterposition(); TestAdapter adapter = (TestAdapter)mRecyclerVIEw.getAdapter(); boolean undoOn = adapter.isUndoOn(); if (undoOn) { adapter.pendingRemoval(swipedposition); } else { adapter.remove(swipedposition); } } @OverrIDe public voID onChildDraw(Canvas c, RecyclerVIEw recyclerVIEw, RecyclerVIEw.VIEwHolder vIEwHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { VIEw itemVIEw = vIEwHolder.itemVIEw; if (vIEwHolder.getAdapterposition() == -1) { return; } if (!initiated) { init(); } background.setBounds(itemVIEw.getRight() + (int) dX, itemVIEw.gettop(), itemVIEw.getRight(), itemVIEw.getBottom()); background.draw(c); int itemHeight = itemVIEw.getBottom() - itemVIEw.gettop(); int intrinsicWIDth = xMark.getIntrinsicWIDth(); int intrinsicHeight = xMark.getIntrinsicWIDth(); int xMarkleft = itemVIEw.getRight() - xMarkmargin - intrinsicWIDth; int xMarkRight = itemVIEw.getRight() - xMarkmargin; int xMarktop = itemVIEw.gettop() + (itemHeight - intrinsicHeight)/2; int xMarkBottom = xMarktop + intrinsicHeight; xMark.setBounds(xMarkleft, xMarktop, xMarkRight, xMarkBottom); xMark.draw(c); super.onChildDraw(c, recyclerVIEw, vIEwHolder, dX, dY, actionState, isCurrentlyActive); } }; itemtouchhelper mitemtouchhelper = new itemtouchhelper(simpleItemtouchCallback); mitemtouchhelper.attachToRecyclerVIEw(mRecyclerVIEw); } private voID setUpAnimationDecoratorHelper() { mRecyclerVIEw.addItemdecoration(new RecyclerVIEw.Itemdecoration() { Drawable background; boolean initiated; private voID init() { background = new colorDrawable(color.RED); initiated = true; } @OverrIDe public voID onDraw(Canvas c, RecyclerVIEw parent, RecyclerVIEw.State state) { if (!initiated) { init(); } if (parent.getItemAnimator().isRunning()) { VIEw lastVIEwComingDown = null; VIEw firstVIEwComingUp = null; int left = parent.getHeight(); int right = parent.getWIDth(); int top = 0; int bottom = 0; int childCount = parent.getLayoutManager().getChildCount(); for (int i = 0; i < childCount; i++) { VIEw child = parent.getLayoutManager().getChildAt(i); if (child.getTranslationY() < 0) { // vIEw is coming down lastVIEwComingDown = child; } else if (child.getTranslationY() > 0) { // vIEw is coming up if (firstVIEwComingUp == null) { firstVIEwComingUp = child; } } } if (lastVIEwComingDown != null && firstVIEwComingUp != null) { // vIEws are coming down AND going up to fill the voID top = lastVIEwComingDown.getBottom() + (int) lastVIEwComingDown.getTranslationY(); bottom = firstVIEwComingUp.gettop() + (int) firstVIEwComingUp.getTranslationY(); } else if (lastVIEwComingDown != null) { // vIEws are going down to fill the voID top = lastVIEwComingDown.getBottom() + (int) lastVIEwComingDown.getTranslationY(); bottom = lastVIEwComingDown.getBottom(); } else if (firstVIEwComingUp != null) { // vIEws are coming up to fill the voID top = firstVIEwComingUp.gettop(); bottom = firstVIEwComingUp.gettop() + (int) firstVIEwComingUp.getTranslationY(); } background.setBounds(left, top, right, bottom); background.draw(c); } super.onDraw(c, parent, state); } }); } class TestAdapter extends RecyclerVIEw.Adapter { private static final int PENDING_REMoval_TIMEOUT = 3000; // 3sec List<String> items; List<String> itemsPendingRemoval; int lastInsertedindex; // so we can add some more items for testing purposes boolean undoOn; // is undo on, you can turn it on from the toolbar menu private Handler handler = new Handler(); // hanlder for running delayed runnables HashMap<String, Runnable> pendingRunnables = new HashMap<>(); // map of items to pending runnables, so we can cancel a removal if need be public TestAdapter() { items = new ArrayList<>(); itemsPendingRemoval = new ArrayList<>(); // let's generate some items lastInsertedindex = 15; // this should give us a couple of screens worth for (int i=1; i<= lastInsertedindex; i++) { items.add("Item " + i); } } @OverrIDe public RecyclerVIEw.VIEwHolder onCreateVIEwHolder(VIEwGroup parent, int vIEwType) { return new TestVIEwHolder(parent); } @OverrIDe public voID onBindVIEwHolder(RecyclerVIEw.VIEwHolder holder, int position) { TestVIEwHolder vIEwHolder = (TestVIEwHolder)holder; final String item = items.get(position); if (itemsPendingRemoval.contains(item)) { // we need to show the "undo" state of the row //vIEwHolder.itemVIEw.setBackgroundcolor(color.RED); vIEwHolder.itemVIEw.setBackgroundcolor(color.WHITE); vIEwHolder.TitleTextVIEw.setVisibility(VIEw.GONE); //vIEwHolder.undobutton.setVisibility(VIEw.VISIBLE); vIEwHolder.undobutton.setVisibility(VIEw.GONE); vIEwHolder.undobutton.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { // user wants to undo the removal, let's cancel the pending task Runnable pendingRemovalRunnable = pendingRunnables.get(item); pendingRunnables.remove(item); if (pendingRemovalRunnable != null) handler.removeCallbacks(pendingRemovalRunnable); itemsPendingRemoval.remove(item); // this will rebind the row in "normal" state notifyItemChanged(items.indexOf(item)); } }); } else { // we need to show the "normal" state vIEwHolder.itemVIEw.setBackgroundcolor(color.WHITE); vIEwHolder.TitleTextVIEw.setVisibility(VIEw.VISIBLE); vIEwHolder.TitleTextVIEw.setText(item); vIEwHolder.undobutton.setVisibility(VIEw.GONE); vIEwHolder.undobutton.setonClickListener(null); } } @OverrIDe public int getItemCount() { return items.size(); } public voID addItems(int howMany){ if (howMany > 0) { for (int i = lastInsertedindex + 1; i <= lastInsertedindex + howMany; i++) { items.add("Item " + i); notifyItemInserted(items.size() - 1); } lastInsertedindex = lastInsertedindex + howMany; } } public voID setUndoOn(boolean undoOn) { this.undoOn = undoOn; } public boolean isUndoOn() { return undoOn; } public voID pendingRemoval(int position) { final String item = items.get(position); if (!itemsPendingRemoval.contains(item)) { itemsPendingRemoval.add(item); // this will redraw row in "undo" state notifyItemChanged(position); // let's create, store and post a runnable to remove the item Runnable pendingRemovalRunnable = new Runnable() { @OverrIDe public voID run() { remove(items.indexOf(item)); } }; handler.postDelayed(pendingRemovalRunnable, PENDING_REMoval_TIMEOUT); pendingRunnables.put(item, pendingRemovalRunnable); } } public voID remove(int position) { String item = items.get(position); if (itemsPendingRemoval.contains(item)) { itemsPendingRemoval.remove(item); } if (items.contains(item)) { items.remove(position); notifyItemRemoved(position); } } public boolean isPendingRemoval(int position) { String item = items.get(position); return itemsPendingRemoval.contains(item); } } static class TestVIEwHolder extends RecyclerVIEw.VIEwHolder { TextVIEw TitleTextVIEw; button undobutton; public TestVIEwHolder(VIEwGroup parent) { super(LayoutInflater.from(parent.getContext()).inflate(R.layout.row_vIEw, parent, false)); TitleTextVIEw = (TextVIEw) itemVIEw.findVIEwByID(R.ID.Title_text_vIEw); undobutton = (button) itemVIEw.findVIEwByID(R.ID.undo_button); } }}
总结 以上是内存溢出为你收集整理的如何在没有解雇的情况下在android中的recyclerview上滑动全部内容,希望文章能够帮你解决如何在没有解雇的情况下在android中的recyclerview上滑动所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)