Frage

Hello i'm developping an app for Android device.
And i'm trying to implement a Keylistener on a Listview.
But if my listview has a lot of elements, my Keylistener is not called by the apps instead it's use the default keylistner for the Listview. Do you have any clue about this situation?
I'm proceed this way to bind the keylistner to my listview.

public View onCreateView(LayoutInflater inflater, ViewGroup container,
         Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.tv_fragment_channel_list, container);
adapter = new AdapterChannelList(getActivity(), ( (List<Category>) User.getInstance().getTV() ).get(0).getContent());
list = (ListView) v.findViewById(R.id.channelListview);
list.setAdapter(adapter);
list.setOnKeyListener(new OnKeyListener()
{
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event)
    {
    if (event.getAction() == KeyEvent.ACTION_DOWN)
        {
        switch (keyCode)
            {
            case KeyEvent.KEYCODE_DPAD_CENTER:
            case KeyEvent.KEYCODE_ENTER:
            {
                ((MainActivity) getActivity()).loadVideo(mLiveTv.get(
                                         ListId).getUrl(),"TV");
                getZappingBannerFragment().loadChannelInfo(mLiveTv.get(
                                           ListId).getId(), true);
                ((MainActivity) getActivity()).findViewById(R.id.tvpannel).setVisibility(View.INVISIBLE);
                MainActivity.shouldfocus = R.id.videoView1;
                return true;
            }
            case KeyEvent.KEYCODE_DPAD_DOWN:
            {
                if (ListId >= /*6*/mLiveTv.size() -1)
                return true;
                if (ListPos == 10)
                {
                    reduce(ListPos);
                }
                else if (ListId > 10)
                reduce(ListPos);
                else
                reduce(ListId);

                ListId++;
                ListPos++;
                if (ListPos == 10)
                {
                    reduce(ListPos - 1);
                    enlarge(ListPos);
                    list.setSelectionFromTop(ListId, 0);
                    ListPos = 0;
                }
                else if (ListId > 10)
                enlarge(ListPos);
                else
                enlarge(ListId);
                //list.setSelectionFromTop(ListId, 0);
                //list.scrollBy(0, 250);
                return true;
            }
            case KeyEvent.KEYCODE_DPAD_UP:
            {
                // list.scrollBy(0, -250);
                if (ListId > 0)
                {
                    reduce(ListId);
                    ListId--;
                    ListPos--;
                }
                if (ListPos < 0)
                {
                    enlarge(ListId);
                    list.setSelectionFromTop(ListPos, 0);
                    ListPos = 0;
                }
                else
                {
                    enlarge(ListId);
                }
                //list.setSelectionFromTop(ListId, 0);
                //list.setSelection(ListId);

                return true;
            }
            case KeyEvent.KEYCODE_BACK:
            {
                //getZappingBannerFragment().justShow();
                ((MainActivity) getActivity()).findViewById(R.id.tvpannel).setVisibility(View.INVISIBLE);
                ((MainActivity) getActivity()).findViewById(R.id.menu_principal).setVisibility(View.VISIBLE);
                MainActivity.shouldfocus = R.id.menu_principal;
                return true;
            }
            }
        }
    return true;
    }
});
return v;
}
War es hilfreich?

Lösung

You should set your keyListener respectively to your list row items from your list adapter. Hope this helps.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top