滑动后如何还原回收者视图项目

时间:2019-08-05 07:18:59

标签: android android-recyclerview swipe itemtouchhelper

我有一个recyclerview,并且已经使用itemtouchhelper类向其添加了滑动,并且效果很好,布局中有2个视图,前景视图在bacjground层上滑动,问题是我想让前景视图获得先前的位置滑动后,就像滑动效果一样,可以在三星手机中拨打电话。 我可以通过在滑动后致电notifyitemchanged来做到这一点。但是我工作了一次,如果我再次在该项目上滑动,则无法恢复视图 这是我的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);

}

}

1 个答案:

答案 0 :(得分:1)

我不知道哪里出了问题,我在模拟器和三星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);
    }
}

然后在一个片段中使用它。

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) })
        // }
    }

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