gpt4 book ai didi

android - 滑动后如何恢复回收者 View 项目

转载 作者:行者123 更新时间:2023-11-29 22:56:53 29 4
gpt4 key购买 nike

我有一个 recyclerview,我用 itemtouchhelper 类向它添加了滑动,它工作正常,布局中有 2 个 View ,前景 View 在 bacjground 层上滑动时移动,问题是我想前景 View 获取以前的位置滑动后,就像在三星手机中调用电话的滑动效果。我可以通过在滑动后调用 notifyitemchanged 来实现。但我工作了一次,如果我再次在该项目上滑动, View 不会恢复这是我的 itemtouchhelper 类

public class RecyclerItemTouchHelperlog extends ItemTouchHelper.SimpleCallback {
private RecyclerItemTouchHelperListener listener;

public RecyclerItemTouchHelperlog(int dragDirs, int swipeDirs, RecyclerItemTouchHelperListener listener) {
super(dragDirs, swipeDirs);
this.listener = listener;
}

@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
return true;
}

@Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
if (viewHolder != null) {
final View foregroundView = ((recycleAdapterlog.MyViewHolder) viewHolder).logitm;

getDefaultUIUtil().onSelected(foregroundView);
}
}

@Override
public void onChildDrawOver(Canvas c, RecyclerView recyclerView,
RecyclerView.ViewHolder viewHolder, float dX, float dY,
int actionState, boolean isCurrentlyActive) {
final View foregroundView = ((recycleAdapterlog.MyViewHolder) viewHolder).logitm;
getDefaultUIUtil().onDrawOver(c, recyclerView, foregroundView, dX, dY,
actionState, isCurrentlyActive);
}

@Override
public void clearView(@NonNull RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
final View foregroundView = ((recycleAdapterlog.MyViewHolder) viewHolder).logitm;
getDefaultUIUtil().clearView(foregroundView);
}

@Override
public int getSwipeDirs(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {

View hv=((recycleAdapterlog.MyViewHolder) viewHolder).hv;
if(hv==null)return super.getSwipeDirs(recyclerView, viewHolder);
else if(hv.getVisibility()==View.VISIBLE)return 0;
else return super.getSwipeDirs(recyclerView, viewHolder);


}

@Override
public void onChildDraw(Canvas c, @NonNull RecyclerView recyclerView,
@NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY,
int actionState, boolean isCurrentlyActive) {
final View foregroundView = ((recycleAdapterlog.MyViewHolder) viewHolder).logitm;
final View swipel = ((recycleAdapterlog.MyViewHolder) viewHolder).swipelogleft;
final View swiper = ((recycleAdapterlog.MyViewHolder) viewHolder).swipelogright;
if(dX<0){
swipel.setVisibility(View.VISIBLE);
swiper.setVisibility(View.GONE);
}
else {
swipel.setVisibility(View.GONE);
swiper.setVisibility(View.VISIBLE);
}
if(actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
final Float alpha =1 - Math.abs (dX) / ( viewHolder.itemView.getWidth ());
foregroundView.setAlpha (alpha);
foregroundView.setTranslationX (dX);
}

recentconfrag.swipedlog=true;

getDefaultUIUtil().onDraw(c, recyclerView, foregroundView, dX, dY,
actionState, isCurrentlyActive);
}

@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
listener.onSwiped(viewHolder, direction, viewHolder.getAdapterPosition());


}

@Override
public int convertToAbsoluteDirection(int flags, int layoutDirection) {
return super.convertToAbsoluteDirection(flags, layoutDirection);
}

public interface RecyclerItemTouchHelperListener {
void onSwiped(RecyclerView.ViewHolder viewHolder, int direction, int position);

}

最佳答案

我不知道,哪里有问题,我在模拟器和三星 Galaxy S4 上测试了下面的解决方案。

public class RecyclerItemTouchHelper extends ItemTouchHelper.SimpleCallback {
private RecyclerItemTouchHelperListener listener;

public RecyclerItemTouchHelper(int dragDirs, int swipeDirs, RecyclerItemTouchHelperListener listener) {
super(dragDirs, swipeDirs);
this.listener = listener;
}

@Override
public boolean onMove(@NotNull RecyclerView recyclerView,
@NotNull RecyclerView.ViewHolder viewHolder,
@NotNull RecyclerView.ViewHolder target) {
return true;
}

@Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
if (viewHolder instanceof YourAdapter.ItemViewHolder) {
final View foregroundView = ((YourAdapter.ItemViewHolder) viewHolder).getItem();
getDefaultUIUtil().onSelected(foregroundView);
}
}

@Override
public void onChildDrawOver(@NotNull Canvas c, @NotNull RecyclerView recyclerView,
@NotNull RecyclerView.ViewHolder viewHolder, float dX, float dY,
int actionState, boolean isCurrentlyActive) {
if (viewHolder instanceof YourAdapter.ItemViewHolder) {
final View foregroundView = ((YourAdapter.ItemViewHolder) viewHolder).getItem();
getDefaultUIUtil().onDrawOver(c, recyclerView, foregroundView, dX, dY,
actionState, isCurrentlyActive);
}
}

@Override
public void clearView(@NotNull RecyclerView recyclerView, @NotNull RecyclerView.ViewHolder viewHolder) {
if (viewHolder instanceof YourAdapter.ItemViewHolder) {
final View foregroundView = ((YourAdapter.ItemViewHolder) viewHolder).getItem();
getDefaultUIUtil().clearView(foregroundView);
}
}

@Override
public void onChildDraw(@NotNull Canvas c, @NotNull RecyclerView recyclerView,
@NotNull RecyclerView.ViewHolder viewHolder, float dX, float dY,
int actionState, boolean isCurrentlyActive) {
if (viewHolder instanceof YourAdapter.ItemViewHolder) {
final View foregroundView = ((YourAdapter.ItemViewHolder) viewHolder).getItem();
getDefaultUIUtil().onDraw(c, recyclerView, foregroundView, dX, dY,
actionState, isCurrentlyActive);
}
}

@Override
public void onSwiped(@NotNull RecyclerView.ViewHolder viewHolder, int direction) {
listener.onSwiped(viewHolder, direction, viewHolder.getAdapterPosition());
}

public interface RecyclerItemTouchHelperListener {
void onSwiped(RecyclerView.ViewHolder viewHolder, int direction, int position);
}
}

然后在 fragment 中使用它。

class YourFragment : Fragment(), RecyclerItemTouchHelper.RecyclerItemTouchHelperListener {

private var adapter: YourAdapter? = null
private lateinit var itemTouchHelperCallback: RecyclerItemTouchHelper


override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val view = inflater.inflate(LAYOUT, container, false)

adapter = YourAdapter(this)

view.recycler_view.run {
layoutManager = LinearLayoutManager(context)
adapter = this@YourFragment.adapter
setHasFixedSize(true)
// attaching the touch helper to recycler view
itemTouchHelperCallback =
RecyclerItemTouchHelper(0, ItemTouchHelper.LEFT, this@YourFragment)
ItemTouchHelper(itemTouchHelperCallback).attachToRecyclerView(this)
}

return view
}

// callback when recycler view is swiped
// item will be removed on swiped
override fun onSwiped(viewHolder: RecyclerView.ViewHolder?, direction: Int, position: Int) {
// if (viewHolder is YourAdapter.ItemViewHolder) {
// remove the item from recycler view
// This is a custom dialog with "Yes", "No" buttons.
showDialog("Remove the item?",
{ adapter?.removeAt(position) },
{ adapter?.notifyItemChanged(position) })
// }
}

请注意,我在 onSwiped 中使用了 position,但是 viewHolder.getAdapterPosition() 也是正确的。我使用了一个解决方案来恢复 Android RecyclerView ItemTouchHelper revert swipe and restore view holder 中的项目.

关于android - 滑动后如何恢复回收者 View 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57353844/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com