我需要在MotionEvent.ACTION_DOWN中获取元素的位置,并在移动元素后获取他的最后位置(MotionEvent.ACTION_UP)。 但是,当我在onInterceptTouchEvent中写入return false时,不会调用MotionEvent.ACTION_UP。 当我在onInterceptTouchEvent中写入true时,它会调用ACTION_UP,但会阻止回收器视图事件。
@Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
if (e.getAction() == MotionEvent.ACTION_UP) {
Log.d("Action", "ACTION_UP");
Log.d("view", ((TextView)childView.findViewById(R.id.quest_row_title)).getText().toString());
}
if (e.getAction() == MotionEvent.ACTION_DOWN) {
Log.d("Action", "ACTION_DOWN");
Log.d("view", ((TextView)childView.findViewById(R.id.quest_row_title)).getText().toString());
Integer count = view.getChildAdapterPosition(childView);
}
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
}
return false;
}
public class PageRecyclerView extends RecyclerView.Adapter<PageRecyclerView.PageViewHolder> implements ItemTouchHelperAdapter {
private ArrayList<Page> pages;
public static HashMap<Integer, Editable> editable;
public PageRecyclerView (ArrayList<Page> pages)
{
this.pages = pages;
}
@Override
public PageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View addView = LayoutInflater.from(parent.getContext()).inflate(R.layout.page_row, parent, false);
return new PageViewHolder(addView);
}
@Override
public void onBindViewHolder(PageViewHolder holder, int position) {
holder.title.setText(pages.get(position).getTitle());
holder.idPage.setText(String.valueOf(pages.get(position).getIdPage()));
holder.countSteps.setText(String.valueOf(pages.get(position).getCountSteps()));
}
@Override
public int getItemCount() {
return pages.size();
}
@Override
public void onItemDismiss(int position) {
pages.remove(position);
notifyItemRemoved(position);
}
@Override
public void onItemMoved(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, int fromPos, RecyclerView.ViewHolder target, int toPos, int x, int y) {
Log.d("Positions moved", "Fromposition"+fromPos+"toPosition"+toPos);
Log.d("Positions movedkj", "With id"+((PageViewHolder)viewHolder).idPage.getText().toString()+"toPosition WIth id"+((PageViewHolder)target).idPage.getText().toString());
Integer idFrom = Integer.valueOf(((PageViewHolder)viewHolder).idPage.getText().toString());
Integer idTo= Integer.valueOf(((PageViewHolder)target).idPage.getText().toString());
if( (PageViewActivity.editableHashMap.get(idFrom))==null){
PageViewActivity.editableHashMap.put(idTo, new Editable(new Page(idFrom,fromPos), new Page(idTo,toPos)));
}
}
@Override
public boolean onItemMove(int fromPosition, int toPosition) {
Log.d("Positions", "Fromposition"+fromPosition+"toPosition"+toPosition+"Listsize"+pages.size());
if (fromPosition < toPosition) {
for (int i = fromPosition; i < toPosition; i++) {
Collections.swap(pages, i, i + 1);
}
} else {
for (int i = fromPosition; i > toPosition; i--) {
Collections.swap(pages, i, i - 1);
}
}
notifyItemMoved(fromPosition, toPosition);
return true;
}
public static class PageViewHolder extends RecyclerView.ViewHolder {
TextView title;
TextView idPage;
TextView countSteps;
public PageViewHolder(View itemView) {
super(itemView);
title = (TextView)itemView.findViewById(R.id.page_row_title);
idPage = (TextView)itemView.findViewById(R.id.page_row_id_page);
countSteps = (TextView)itemView.findViewById(R.id.page_row_count_steps);
}
}
}
答案 0 :(得分:0)
而不是
return false;
尝试返回此内容并查看
return view.onInterceptTouchEvent(e);
这不会拦截默认触摸事件
答案 1 :(得分:0)
感谢@pskink的回答,我们需要覆盖两个方法onSelectedChanged(如ACTION_DOWN)和ClearView(如ACTION_UP)。
@Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
// We only want the active item to change
if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
if (viewHolder instanceof ItemTouchHelperViewHolder) {
// Let the view holder know that this item is being moved or dragged
ItemTouchHelperViewHolder itemViewHolder = (ItemTouchHelperViewHolder) viewHolder;
itemViewHolder.onItemSelected();
Log.d("onSelectedChanged",((PageRecyclerView.PageViewHolder)itemViewHolder).idPage.getText().toString());
Log.d("onSelectedChanged", String.valueOf(viewHolder.getAdapterPosition()));
}
}
super.onSelectedChanged(viewHolder, actionState);
}
@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder);
viewHolder.itemView.setAlpha(ALPHA_FULL);
if (viewHolder instanceof ItemTouchHelperViewHolder) {
// Tell the view holder it's time to restore the idle state
ItemTouchHelperViewHolder itemViewHolder = (ItemTouchHelperViewHolder) viewHolder;
Log.d("onClearView",((PageRecyclerView.PageViewHolder)itemViewHolder).idPage.getText().toString());
itemViewHolder.onItemClear();
Log.d("onClearView","test");
Log.d("onClearView", String.valueOf(viewHolder.getAdapterPosition()));
}