asked    Patrick     2018-10-22       android       75 view        1 Answer

[SOLVED] android motion events not triggered with drag listener

Problem

So I have a recycler view populated by card views all with drag listeners attached, the motion events work fine until I set a view to be dragged, so with the line commented out this works, all logs are printed perfectly

holder.handleView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            switch (MotionEventCompat.getActionMasked(event)){
                case MotionEvent.ACTION_DOWN:
                    Log.d("Adptr" , "ACTION_DOWN");
                    holder.cardView.setCardElevation(context.getResources().getDimensionPixelSize(R.dimen.elevation_pressed));
                    //mDragStartListener.onStartDrag(holder);
                    return true;
                case MotionEvent.ACTION_UP:
                    Log.d("Adptr" , "ACTION_UP");
                    holder.cardView.setCardElevation(context.getResources().getDimensionPixelSize(R.dimen.elevation));
                    break;
                case MotionEvent.ACTION_CANCEL:
                    Log.d("Adptr" , "ACTION_CANCEL");
                    holder.cardView.setCardElevation(context.getResources().getDimensionPixelSize(R.dimen.elevation));
                    break;
            }
            return false;
        }
    });

but if I uncomment the line

mDragStartListener.onStartDrag(holder);

then ACTION_UP is no longer called and I'm confused as to why

the listener is an interface

public interface OnStartDragListener {
    void onStartDrag(RecyclerView.ViewHolder viewHolder);
}

I implement it in my activity and pass it to the adapter

@Override
    public void onStartDrag(RecyclerView.ViewHolder viewHolder) {
        mItemTouchHelper.startDrag(viewHolder);
    }

My onMove method is this

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

and in my adapter i have an onItemMove from an interface

public interface ItemTouchHelperAdapter {
  boolean onItemMove(int fromPosition, int toPosition);
  void onItemDismiss(int position);
}


@Override
  public boolean onItemMove(int fromPosition, int toPosition) {
    if (fromPosition < toPosition){
        for (int i = fromPosition; i < toPosition; i++){
            Collections.swap(cardMakerList,i,i+1);
        }
    }else{
        for (int i = fromPosition; i > toPosition; i--){
            Collections.swap(cardMakerList,i,i-1);
        }
    }
    notifyItemMoved(fromPosition,toPosition);
    return true;
}

  1 Answer  

        answered    Edwina     2018-10-22      

Try add below and run:

holder.handleView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            switch (MotionEventCompat.getActionMasked(event)){
                case MotionEvent.ACTION_DOWN:
                    Log.d("Adptr" , "ACTION_DOWN");
                    holder.cardView.setCardElevation(context.getResources().getDimensionPixelSize(R.dimen.elevation_pressed));
                    //mDragStartListener.onStartDrag(holder);
                    return true;
                case MotionEvent.ACTION_MOVE:
                        Log.d("Adptr" , "ACTION_MOVE");
                        return true;
                case MotionEvent.ACTION_UP:
                    Log.d("Adptr" , "ACTION_UP");
                    holder.cardView.setCardElevation(context.getResources().getDimensionPixelSize(R.dimen.elevation));
                    break;
                case MotionEvent.ACTION_CANCEL:
                    Log.d("Adptr" , "ACTION_CANCEL");
                    holder.cardView.setCardElevation(context.getResources().getDimensionPixelSize(R.dimen.elevation));
                    break;
            }
            return false;
        }
    });

As According to your code the flow is not right.If you return false on any Motion action before ACTION_UP then the code of ACTION_UP will not be called.

P.S: I prefer returning true from onTouch until and unless i have to take care of special cases.If there are special case return proper boolean value.

holder.handleView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            switch (MotionEventCompat.getActionMasked(event)){
                case MotionEvent.ACTION_DOWN:
                    Log.d("Adptr" , "ACTION_DOWN");
                    holder.cardView.setCardElevation(context.getResources().getDimensionPixelSize(R.dimen.elevation_pressed));
                    //mDragStartListener.onStartDrag(holder);
                    return true;
                case MotionEvent.ACTION_UP:
                    Log.d("Adptr" , "ACTION_UP");
                    holder.cardView.setCardElevation(context.getResources().getDimensionPixelSize(R.dimen.elevation));
                    break;
                case MotionEvent.ACTION_CANCEL:
                    Log.d("Adptr" , "ACTION_CANCEL");
                    holder.cardView.setCardElevation(context.getResources().getDimensionPixelSize(R.dimen.elevation));
                    break;
            }
            return true;
        }
    });




Your Answer





 2018-10-22         Jerome

Laravel Many to many relationship with custom key relation

Before you mark this question as duplicate, please see the details :) I have a problem related to many to many relationship with custom columns linkingI have following tables Employees -Id -brink_id <----- This is third party id I am saving of employee -nameJobs-id-brink_id <----- This is third party id I am saving of job-descriptionemployee_job-id-brink_id <----- This is third party id I am saving of relationship for third party-brink_employee_id-brink_job_idIn Employee Model I have created relationshippublic function jobs(){ return $this->belongsToMany('App...
 laravel                     1 answers                     53 view
 2018-10-22         Spring

Responses JSON with Relationship - Laravel/Eloquent

I'm working on a Laravel project and I want to create a REST API for a website. On my system, I have two tables:Blogs and Categories. The table blogs have the category_id column, which is a key that references the column ID in the category table.Blogs Migrationclass CreateBlogsTable extends Migration{ public function up() { Schema::create('blogs', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->longtext('body'); $table->string('category_id'); $table->timestamps(); }); } .....}Catego...
 laravel                     3 answers                     55 view
 2018-10-22         Abbott

cant pass the request to policy so i can validate some issues

i authorize store like this public function store( Request $request){ $this->authorizeApi('store',JobPost::class, $request); return $this->jobPostRepository->store($request);}and in policies my store method looks like this public function store(Request $request){ $user=auth()->user(); return ($user->company_id == $request->company_id) &&($request->expiration_date->isAfter($request->publish_date)) &&($request->publish_date->isAfter(now())) ;}when i run this i get"Argument 1 passed to App\...
 api                     1 answers                     55 view