2014-06-09 46 views
1

当我长时间点击列表视图中的一个项目(触发上下文动作条显示)时,列表视图自动滚动到最后。如果用户在滚动后选择了一个项目,这种行为是非常令人不愉快的。listview在显示上下文动作条后滚动结束

使用断点我证实,这发生onItemCheckedStateChanged()MultiChoiceModeListener实施已完成。但是我不确定什么代码在这之后会导致行为被执行。

从列表视图布局中删除transcriptMode属性可以解决问题。但是我不想删除它,因为当光标中的数据发生变化时它需要自动滚动。任何想法是什么导致问题?

@Override 
     public void onActivityCreated(Bundle savedInstanceState) { 

      super.onActivityCreated(savedInstanceState); 

      // Get the chat messages list view from the layout 
      lv_chatMessages = (ListView) getActivity().findViewById(
        R.id.listview_chat); 

      // Instantiate the adapter for the chat messages 
      adpt_chat = new ChatMessagesAdapter(getActivity()); 

      // connect the adapter to the list view 
      lv_chatMessages.setAdapter(adpt_chat); 

      //Implement multi choice mode listener for the list view 
      //only if Android API 11 or higher 

      if (Utils.hasHoneycomb()) { 

       //Enable selection of multiple chat messages 
       lv_chatMessages.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL); 

       //Handle Action mode events 
       lv_chatMessages 
         .setMultiChoiceModeListener(new MultiChoiceModeListener() { 

          @Override 
          public boolean onActionItemClicked(ActionMode mode, 
            MenuItem item) { 
           switch(item.getItemId()){ 
           case R.id.delete_menu : 
            long[] selected_IDs = lv_chatMessages.getCheckedItemIds(); 
            int deletedRows = deleteMessages(selected_IDs); 
            Toast.makeText(getActivity(), deletedRows + " message(s) deleted", 
              Toast.LENGTH_SHORT).show(); 
            return true; // true indicates the menu selection is handled. no further 
                // system handling required 
           default : 
            return false; 
           } 

          } 

          @Override 
          public boolean onCreateActionMode(ActionMode mode, 
            Menu menu) { 

           MenuInflater inflater = mode.getMenuInflater(); 
           inflater.inflate(R.menu.chatsession_contextmenu, menu); 
           return true; 
          } 

          @Override 
          public void onDestroyActionMode(ActionMode arg0) { 
           // TODO Auto-generated method stub 

          } 

          @Override 
          public boolean onPrepareActionMode(ActionMode arg0, 
            Menu arg1) { 
           // TODO Auto-generated method stub 
           return false; 
          } 

          @Override 
          public void onItemCheckedStateChanged(ActionMode mode, 
            int position, long id, boolean checked) { 

           mode.setTitle(lv_chatMessages.getCheckedItemCount() + " selected"); 


          } 

         }); 
      } 
... 
.. 

} 

列表视图布局:

<ListView 
     android:id="@+id/listview_chat" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:layout_alignParentTop="true" 
     android:transcriptMode="alwaysScroll" 
     android:stackFromBottom="true" 
     android:layout_above="@id/layout_input" 
     android:divider="#00000000" 
     /> 
+0

Quesiton downvoted?为什么? – faizal

回答

1

卸下transcriptMode属性似乎是唯一的解决办法。我使用下面的代码,只要我需要自动滚动到底:

new Handler().postDelayed(new Runnable(){ 
      public void run(){ 
       lv_chatMessages.setSelection(lv_chatMessages.getCount()); 
      } 
     },100);