2017-01-01 213 views
0

我在下面的代碼中設置了一個點擊偵聽器,但滑動和磁帶事件處理程序沒有被調用。任何想法爲什麼?未觸發觸摸和滑動事件

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    recyclerView = (RecyclerView) findViewById(R.id.view); 

    getOverflowMenu(); 

    android.support.v4.app.LoaderManager loaderManager = 
     getSupportLoaderManager(); 

    android.support.v4.content.Loader<Cursor> loader = 
     loaderManager.getLoader(loader_id); 

    if (loader != null) 
     loaderManager.initLoader(loader_id, null, this); 
    else 
     loaderManager.restartLoader(loader_id,null,this); 

    recyclerView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Toast.makeText(getApplicationContext(),"touched",Toast.LENGTH_SHORT).show();; 
     } 
    }); 

    new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) { 
     @Override 
     public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { 
      Toast.makeText(getApplicationContext(),"moved",Toast.LENGTH_SHORT).show(); 
      return true; 
     } 

     @Override 
     public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) { 
      long id = (long) viewHolder.itemView.getTag(); 
      remove(id); 
     } 
    }); 
} 
+0

'完成(11)' - 這是家庭作業嗎? –

+1

你需要更加努力地思考你要求人們爲你做些什麼。如果你在這裏需要幫助,請很好地格式化你的代碼。刪除不必要的評論,使*易*閱讀。不要包含比展示問題所需的代碼更多的代碼,並要求提供非常清晰易懂的問題,並對其進行智能地陳述。 – clearlight

回答

0

試試這個與循環意見活動(像主也許)

private RecyclerView recyclerView; 

ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) { 

     @Override 
     public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { 
      return false; 
     } 
     @Override 
     public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { 
      int position = viewHolder.getAdapterPosition(); 
       if (direction == ItemTouchHelper.RIGHT){ 
       Toast.makeText(MainActivity.this, "swiped left", Toast.LENGTH_SHORT).show(); 
      //do something awsome 
      } else { 
       Toast.makeText(MainActivity.this, "swiped right", Toast.LENGTH_SHORT).show(); 
      //do something awesomer 
      } 
     } 

    }; 
    ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback); 
    itemTouchHelper.attachToRecyclerView(recyclerView); 


} 
+0

我試過了,但沒有奏效 –