set/disable focus on a certain item in GridView

99封情书 提交于 2019-12-12 06:04:24

问题


I have a GridView with items which I defined in an adapter. Those items are composted by TextView and ImageView. I want to control the focus on GridView, It looks like this:

(1) (2) (3) (4)

when I press "right" on keyboard, focus move 1->2-3>4. I need disable the focus on 2, that is 1->3->4. I tried :

((GridView) view).getChildAt(mPosition).setNextFocusRightId(mGridView.getChildAt(mPosition+2).getId());
((GridView) view).getChildAt(mPosition + 2).requestFocus();
((GridView) view).setSelection(mPosition + 2);

on dispatchKeyEvent function when item selected on position 0. But it didn't work. And also I tried getChildAt(mPosition + 1).setSelected(false) in the GridView onFocusChangeListener, and it did no good. I'm stuck by this problem. Please anyone who can give me a note?


回答1:


So for external keyboard i have one idea.

1.Declare one map

Map<View,View> right = new HashMap<View,View>();
 View currentViewFocus;

2.Assign value of android UI control

void  putValueOnDownMap(){
          down.put(androidautocomextview1,textview2);
        down.put(textview2, editetxt3);
        down.put(editetxt3, button4);
            down.put(button4, androidautocomextview1);
        } 

3. On key callback do this

     View currentViewFocus =  androidautocomextview1;
@Override  
public boolean onKeyUp(int keyCode, KeyEvent event) {
     if(productname.hasFocus()){  
                 if(keyCode == KeyEvent.KEYCODE_DPAD_DOWN){
             View temp = currentViewFocus;

             if(temp.getClass().equals(AutoCompleteTextView.class)){
/**
* First it will check that view class object belongs to which child class if it *belongs to autoComplete textview then it will type-cast super class object to child *class and call requestFocus() method to get focus.
*/

            AutoCompleteTextView auto_temp = (AutoCompleteTextView)temp;

                 if(auto_temp.isPopupShowing()){   
/**If popup is showing then on press of down D-pad in popup it should be remain 
*     navigate on popup
*/
                     auto_temp.requestFocus();
                 }
                 else{       

/**Now we made our selection from autocomplete textview popup by “Enter key”
 *Then time to navigate in  textview2 . Pass currentViewFocus as key and get value *from down map again assign new value in   currentViewFocus again check it child *class again type-cast and so on.
*/
                     temp = down.get(currentViewFocus);   
                     currentViewFocus = temp;
                     if (temp == null){
                     }
                     else{
                         if(temp.getClass().equals(TextView.class)){
                             TextView txtv = (TextView)temp;

                                 txtv.requestFocus();
                                 currentViewFocus = temp;


                         }
                         else{    
                             temp.performClick();
                             temp.requestFocus();

                             currentViewFocus = temp;

                         } 
                     }


                 }      
             }    
             else{ 
                 // and so on we first check that class of super class 
                         // type-cast according to child class
                         //  perform stub.
                }
                 }
//=====================================================================
/** So-far we are  navigating through D-pad down key now suppose that our current 
*   focus on  editetxt3 and we want to navigate through D-pad UP key.
*/

 if(keyCode == KeyEvent.KEYCODE_DPAD_UP){

                      return super.onKeyUp(keyCode, event); 
        }



回答2:


if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT
                    && (event.getAction() == KeyEvent.ACTION_DOWN)) {
                if (mType == 0 && currentPos == (disablePos + 1) && disablePos >= 1) {
                    ((GridView) v).getChildAt(disablePos).setSelected(false);
                    ((GridView) v).getChildAt(disablePos - 1).requestFocus();
                    ((GridView) v).setSelection(disablePos - 1);
                    return true;
                } else {
                    return false;
                }if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT
                    && (event.getAction() == KeyEvent.ACTION_DOWN)) {
                if (mType == 0 && currentPos == (disablePos + 1) && disablePos >= 1) {
                    ((GridView) v).getChildAt(disablePos).setSelected(false);
                    ((GridView) v).getChildAt(disablePos - 1).requestFocus();
                    ((GridView) v).setSelection(disablePos - 1);
                    return true;
                } else {
                    return false;
                }


来源:https://stackoverflow.com/questions/29072810/set-disable-focus-on-a-certain-item-in-gridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!