onKeyListener with onScrollListener

134 Views Asked by At

Really not sure if I understand this or maybe going about it wrong. I have a gridlayout (cells are textview) wrapped inside a horizontal scroll which is inside a vertical scroll. I am using the Dpad to navigate across the grid. This works well, as I press the right arrow pad the grid cells move left to right as expected and right to left as left arrow pad is pressed. I have added an onKeylistener attached to each textView of the grid, as I scroll across the grid I am changing the color back ground. The problem is that the onKeyListener apparently takes over the control of the grid. The scroll right works for changing the color but the cells no longer move on the grid. Once I get to last visible column focus continues off screen but the cells stay off screen. Is there a way to implement the scroll inside the onkey event so the cells shift and I have control over the properties of the cell? Or is there a totally different way of doing making this work?

The main components are

textViewD.setOnKeyListener(new View.OnKeyListener() {
                    @Override
                    public boolean onKey(View view, int i, KeyEvent keyEvent) {

                       if(keyEvent.getAction()==KeyEvent.ACTION_DOWN && i == KeyEvent.KEYCODE_DPAD_RIGHT) {

                           gridLayoutE.getChildAt(childIndex[0]).setBackgroundColor(Color.BLACK);
                           gridLayoutE.getChildAt(childIndex[0] + 1).setBackgroundColor(Color.GREEN);
                           childIndex[0] = childIndex[0] + 1;
                           gridLayoutE.requestFocus();


                           return true;
     }
     return false;
     }

The scrolllistener, I have a class that extends the horizontalscroll in order to have my header table scroll along with my grid. This works.

 @Override
public void onScrollChanged (ObservableScrollView scrollView,int x, int y, int oldx, int oldy){
    if (scrollView == hsvHeader) {
        hsvBody.scrollTo(x, y);
    } else if (scrollView == hsvBody) {

        hsvHeader.scrollTo(x, y);
    }
}
1

There are 1 best solutions below

0
On BEST ANSWER

I was able to find a solution to my problem by using dispatchKeyEvent(KeyEvent event). I ended up with something like

 public boolean dispatchKeyEvent(KeyEvent event) {
     mcols= mcols + 1;
     if(event.getAction()==KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT) {
     gridLayoutE.getFocusedChild().setBackgroundColor(Color.BLACK);
     gridLayoutE.findViewById(mcol).setBackgroundColor(Color.GREEN);
     }
        return super.dispatchKeyEvent(event);
    }

This along with my scrolllistener, allowed my to scroll the grid and change each cell as I did.