简体   繁体   中英

How to move down the selector in list view when click of volume down button in android

How to move focus of list item to down item when we clicked on volume down button of android mobile.Tried this code but unable to down the focus.any help on this or any other alternative approaches to down the selector. Thanks in advance.

    super.onCreate(savedInstanceState);
    // 2. create array adapter
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, // standard row layout
                                                    // provided by android
            listItemArray);
    // 3. Call setListAdapter
    setListAdapter(adapter);
    listView = getListView();
    listView.setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // TODO Auto-generated method stub

            switch (keyCode) {
            case KeyEvent.KEYCODE_VOLUME_DOWN:

                ListView list =(ListView)v;
                for (int j=0; j < list.getChildCount(); j++){
                    if(list.getChildAt(j).getBackground().equals(R.color.gray)){
                    list.getChildAt(j).setBackgroundResource(R.color.TRANSPARENT);
                    list.getChildAt(j++).setBackgroundResource(R.color.gray);
                    }else
                        list.getChildAt(0).setBackgroundResource(R.color.gray);
                }
                                    break;
            }           

            return false;
        }
    });

I can hardly imagine how would onKeyListener on a ListView detect volume key press. Try to override dispatchKeyEvent() in your Activity instead:

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    switch (event.getKeyCode()) {
        case KeyEvent.KEYCODE_VOLUME_UP:
            Toast.makeText(this, "volume up", Toast.LENGTH_SHORT).show();
            return true;
        case KeyEvent.KEYCODE_VOLUME_DOWN:
            Toast.makeText(this, "volume down", Toast.LENGTH_SHORT).show();
            return true;
        default:
            return super.dispatchKeyEvent(event);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM