2016-08-08 2 views
2

좋아, 나는 한 다음과 같은 요구 사항 : 일부 안드로이드 버전에서 PopupWindow 내부의 ListView는 팝업에 초점을 맞출 수 없을 때 터치 이벤트를받지 못합니까?

  • 사용자가 활용할 수

    • 글고 제안 팝업 @ 사용자 유형
    • 사용자를 나타나면있는이 제안을 필터링합니다 계속 입력 할 수 있습니다 그것을 여기

  • 을 기각한다
  • 이 사용자는 팝업 외부를 눌러 수있는 팝업 항목을 완료하고 기각하는 제안에 내 코드입니다 :

     PopupWindow popupWindow = new PopupWindow(mContext); 
    
         // Create ListView to show the suggestions 
         ListView listView = new ListView(mContext); 
         listView.setBackgroundColor(Color.WHITE); 
         MentionsAdapter adapter = new MentionsAdapter(mContext, suggestions); 
         listView.setAdapter(adapter); 
         listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() { 
          @Override 
          public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
           String username = mSuggestions.get(position); 
           String mentionCompletion = username.substring(mCurrentQuery.length()).concat(" "); 
           mEditor.getText().insert(mEditor.getSelectionEnd(), mentionCompletion); 
           hideSuggestions(); 
          } 
         }); 
         mSuggestionsListView = listView; 
    
         popupWindow.setContentView(listView); 
         popupWindow.setFocusable(false); 
         popupWindow.setTouchable(true); 
         popupWindow.setOutsideTouchable(true); 
    
         popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { 
    
          @Override 
          public void onDismiss() { 
           clearPopupData(); 
          } 
         }); 
    
         Rect popupRect = calculatePopupPosition(); 
         popupWindow.setWidth(popupRect.width()); 
         popupWindow.setHeight(popupRect.height()); 
         popupWindow.showAtLocation(mEditor, Gravity.START | Gravity.TOP, popupRect.left, popupRect.top); 
    
         mPopupWindow = popupWindow; 
    

    작동하지 않습니다! 문제는 일부 Android 버전/기기에서만 작동한다는 것입니다. 예를 들어 에뮬레이터의 Android 6.0에서 작동하지만 동료 6.0의 LG G4에서는 작동하지 않습니다. Android 4.3.1에서는 작동하지 않지만 4.4.2에서는 작동합니다. PopupWindow에 포커스를 설정할 수없는 경우 ListViewOnItemClickListener이 호출되지 않습니다. PopupWindow에 포커스가있는 경우 ListViewOnItemClickListener은 호출되지만 EditText은 키보드 이벤트를 수신하지 않습니다. PopupWindowListView에서 초점/터치 모드를 변경하는 수많은 조합을 시도했지만 이러한 경우에는 작동하지 않습니다.

    WFT에 대한 제안 사항이 있으며 해결 방법은 무엇입니까?

  • 답변

    1

    내가 원하는 것을 성취 할 수있는 방법을 찾았습니다. 이 일관성없는 동작이 왜 일어나고 있는지에 대한 실제적인 답변보다 해킹이 더 많으므로 해답으로 받아들이지 않을 것입니다. 여전히 동일한 요구 사항을 달성하려는 사람들에게는 가치있을 수 있습니다.

    내가 무슨 짓을 항복했다 터치 이벤트를 수신하는 것이 ListView 임베디드있어 가능하게하기 위해 포커스 PopupWindow을이 효과적으로 만들면서

    popupWindow.setFocusable(true); 
    

    ListView는 키보드 이벤트의 수신기는 나는 OnKeyListener 전달했다 글고 치기로 이벤트 : 여기 통관

     listView.setOnKeyListener(new View.OnKeyListener() { 
          @Override 
          public boolean onKey(View v, int keyCode, KeyEvent event) { 
           mEditor.dispatchKeyEvent(event); 
           return false; 
          } 
         }); 
    

    질문에서 전체 수정 된 코드입니다 :

     PopupWindow popupWindow = new PopupWindow(mContext); 
    
         // Create ListView to show the suggestions 
         ListView listView = new ListView(mContext); 
         listView.setBackgroundColor(Color.WHITE); 
         listView.setOnKeyListener(new View.OnKeyListener() { 
          @Override 
          public boolean onKey(View v, int keyCode, KeyEvent event) { 
           mEditor.dispatchKeyEvent(event); 
           return false; 
          } 
         }); 
         MentionsAdapter adapter = new MentionsAdapter(mContext, suggestions); 
         listView.setAdapter(adapter); 
         listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() { 
          @Override 
          public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
           String username = mSuggestions.get(position); 
           String mentionCompletion = username.substring(mCurrentQuery.length()).concat(" "); 
           mEditor.getText().insert(mEditor.getSelectionEnd(), mentionCompletion); 
           hideSuggestions(); 
          } 
         }); 
         mSuggestionsListView = listView; 
    
         popupWindow.setContentView(listView); 
         popupWindow.setFocusable(true); 
         popupWindow.setTouchable(true); 
         popupWindow.setOutsideTouchable(true); 
    
         popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { 
    
          @Override 
          public void onDismiss() { 
           clearPopupData(); 
          } 
         }); 
    
         Rect popupRect = calculatePopupPosition(); 
         popupWindow.setWidth(popupRect.width()); 
         popupWindow.setHeight(popupRect.height()); 
         popupWindow.showAtLocation(mEditor, Gravity.START | Gravity.TOP, popupRect.left, popupRect.top); 
    

    이것은 모든 Android 버전/기기에서 작동하는 것으로 보입니다. ListView의 항목을 시각적으로 초점을 맞추지 않으려면 ListView의 항목의 배경을 초점을 맞출 때 변경되지 않는 색상이나 드로어 블에 설정할 수 있습니다.

    0

    OnClickListenerconvertview에 설정하여이 문제를 해결했습니다. 이는 어댑터의 getView 메소드 매개 변수입니다. 그런 다음 onItemClick 메서드를 호출하여 listview의 onItemClickListener 메서드를 호출합니다.

    팝업 창 포커스를으로 만들 필요가 없습니다.Infact는, 그냥 ListPopupWindow를 사용하여 우리가 코드

    ListPopupWindow popupWindow = new ListPopupWindow(mContext); 
    
        MentionsAdapter adapter = new MentionsAdapter(mContext, suggestions); 
        popupWindow.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() { 
         @Override 
         public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
          String username = mSuggestions.get(position); 
          String mentionCompletion = username.substring(mCurrentQuery.length()).concat(" "); 
          mEditor.getText().insert(mEditor.getSelectionEnd(), mentionCompletion); 
          hideSuggestions(); 
         } 
        }); 
        popupWindow.setAdapter(adapter); 
    

    MentionsAdaptergetView 방법

    @Override 
        public View getView(int position, View convertView, ViewGroup parent) { 
         if(convertView == null){ 
          convertView = LayoutInflater.from(context).inflate(,R.layout.item_user,parent,false); 
    
         } 
         // this is the key point of workaround 
         convertView.setOnClickListener(new View.OnClickListener() { 
          @Override 
          public void onClick(View v) { 
           popupWindow.getListView().getOnItemClickListener() 
           .onItemClick(listView, v, position, getItemId(position)); 
          } 
         }); 
         return convertView; 
        } 
    
    의 일부 몇 줄을 추가하는 유일한 내부 getView() 어댑터 방법부터 쉽게