2016-03-02 57 views
0

我使用最新更新的支持庫創建了底部工作表。基本上,只要長時間點擊回收站視圖中的項目,就會顯示底部工作表(包含包含列表視圖的線性佈局)。但是每當我點擊listview中的項目時都不會發生任何事情。我添加了吐司,當我點擊表單項目時,它永遠不會被觸發。有任何想法嗎?提前致謝!ListView OnItemClickListener不在底部工作表中觸發

這裏是ListView控件初始化方法:

public void initSheet() { 
     bottomSheet = findViewById(R.id.list_sheet); 
     ListAdapter adapter = new ListAdapter(this, R.layout.custom_sheet_row, getSheetInfo(), "Sheet"); 
     list = (ListView) findViewById(R.id.list_sheet_list); 
     list.setAdapter(adapter); 
     behavior = BottomSheetBehavior.from(bottomSheet); 
     behavior.setHideable(true); 
    } 

這裏是recyclerview長按:

@Override 
      public void onLongClick(View view, int position) { 
       final String itemText = ((TextView) view.findViewById(R.id.textRow)).getText().toString(); 
       list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
        @Override 
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
         Toast.makeText(ListActivity.this, "sdjakfjs", Toast.LENGTH_SHORT).show(); 
         switch (position) { 
          case 0: 
           Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); 
           sharingIntent.setType("text/plain"); 
           sharingIntent.putExtra(Intent.EXTRA_TEXT, itemText); 
           startActivity(Intent.createChooser(sharingIntent, "Share the item")); 
           break; 
          case 1: 
           final ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 
           clipboard.setPrimaryClip(ClipData.newPlainText("New", itemText)); 
           break; 
          case 2: 
           editText(itemText, position); 
           break; 
          case 3: 
           listAdapter.deleteItem(position); 
           break; 
         } 
         behavior.setState(BottomSheetBehavior.STATE_HIDDEN); 
        } 
       }); 
       behavior.setState(BottomSheetBehavior.STATE_EXPANDED); 
      } 
+0

請確定您使用的是什麼,列表視圖或回收站視圖。你的代碼是列表視圖,在某些地方你使用回收視圖 – theLazyFinder

+0

就像我在op中說過的,我有一個recyclerview作爲主應用,但在底部列表中有一個列表視圖 –

回答

0

嘗試。
實現自定義clicklistener它將完美地工作。

// creating interface to handle clicks for Recycler view items 
public interface ClickListener 
{ 
    void onClick(View view,int position); 
    void onLongClick(View view,int position); 
} 
public static class CustomRecyclerTouchListener implements RecyclerView.OnItemTouchListener 
{ 
    private GestureDetector gestureDetector; 
    private MainActivity.ClickListener clickListener; 
    public CustomRecyclerTouchListener(Context context,final RecyclerView recyclerView,final MainActivity.ClickListener clickListener) 
    { 
     this.clickListener=clickListener; 
     gestureDetector=new GestureDetector(context,new GestureDetector.SimpleOnGestureListener() 
     { 
      @Override 
      public boolean onSingleTapUp(MotionEvent e) { 
       return true; 
      } 
      @Override 
      public void onLongPress(MotionEvent e) { 
       View child=recyclerView.findChildViewUnder(e.getX(),e.getY()); 
       if(child!=null && clickListener !=null) 
       { 
        clickListener.onLongClick(child,recyclerView.getChildPosition(child)); 
       } 
      } 
     }); 
    } 
    @Override 
    public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) { 
     View child=recyclerView.findChildViewUnder(motionEvent.getX(),motionEvent.getY()); 
     if(child!=null && clickListener!=null && gestureDetector.onTouchEvent(motionEvent)) 
     { 
      clickListener.onClick(child,recyclerView.getChildPosition(child)); 
     } 
     return false; 
    } 
    @Override 
    public void onTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) { 
    } 
    @Override 
    public void onRequestDisallowInterceptTouchEvent(boolean b) { 
    } 
} 
} 

然後像這樣執行。

recyclerView.addOnItemTouchListener(new CustomRecyclerTouchListener(getApplicationContext(), recyclerView, new ClickListener() { 
     @Override 
     public void onClick(View view, int position) { 
      User user=users.get(position); 
      Toast.makeText(MainActivity.this,user.getName()+" is clicked",Toast.LENGTH_SHORT).show(); 
     } 
     @Override 
     public void onLongClick(View view, int position) { 
      User user=users.get(position); 
      Toast.makeText(MainActivity.this,user.getName()+" is long clicked",Toast.LENGTH_SHORT).show(); 
     } 
    })); 

更多詳細信息請點擊這裏: - http://coderzpassion.com/android-working-with-recycler-view/

+0

我已經在使用這個用於recyclerview。我遇到的問題是與listview。對不起,如果我不清楚。基本上,我在底部表單裏面有一個listview,不管出於什麼原因都沒有收到點擊命令。 –

0

好吧,我落得這樣做創建4個textviews線性佈局的內部,而不是使用列表視圖。然後,我只是添加了一個onclick的所有文字瀏覽器來完成他們各自的命令