设置/禁用焦点在GridView中的某个项目

时间:2015-03-16 08:55:52

标签: android gridview focus

我有一个GridView,其中包含我在适配器中定义的项目。这些项目由TextView和ImageView进行堆肥。我想控制对GridView的关注,它看起来像这样:

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

当我在键盘上按“右”时,焦点移动1-> 2-3> 4。我需要禁用焦点2,即1-> 3-> 4。 我试过了:

((GridView) view).getChildAt(mPosition).setNextFocusRightId(mGridView.getChildAt(mPosition+2).getId());
((GridView) view).getChildAt(mPosition + 2).requestFocus();
((GridView) view).setSelection(mPosition + 2);
当在位置0上选择项目时,在dispatchKeyEvent函数上

但是它不起作用。我还试过getChildAt(mPosition + 1).setSelected(false) 在GridView onFocusChangeListener中,它没有任何好处。我被这个问题困住了。请有人给我一张纸条吗?

2 个答案:

答案 0 :(得分:0)

所以对于外接键盘,我有一个想法。

1.Declare one map

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

2.Assign UI控件的值#

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

3。在关键回调上执行此操作

     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); 
        }

答案 1 :(得分:0)

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;
                }