2

So i have searched and searched and tried every method ive seen but nothing seems to solve my problem. My listview generates with custom adapter, i add the footer view, and onTouchMethod, and i know that it is competing for focus with the lsitview and the listview is winning as i have read about this error. But perhaps there is something i cant see and i am missing.

I need the footer view to be selectable at all times, Right now it is only selectable after an item is selected in listview and then everything works as its suppose to. But on first load on the listview, the footer is unresponsive.

Here is my code.

customAdapter = new ListAdapter(this, R.layout.itemlistrow, store_data);
    customAdapter.setNotifyOnChange(true);

    listView.addFooterView(none_of_the_above, null, false);
    listView.setAdapter(customAdapter);

    listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

listView.setOnItemClickListener(new OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> arg0, final View view,
                int position, long id) {



                none_of_the_above.setOnTouchListener(new View.OnTouchListener() {

                    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
                    public boolean onTouch(View v, MotionEvent event) {
                        if (event.getAction() == MotionEvent.ACTION_DOWN) {
                            v.setSelected(true);
                            view.setSelected(false);
                            view.setActivated(false);
                            view.clearFocus();

                            Log.e("NoneOFTheAboveButton:onTouch", "clicked");
                            none_of_the_above.setBackground(getResources().getDrawable(R.drawable.store_setup2_found_selection_boxes_modified_states));
                        }
                        return false;
                    }
                });

listView.clearFocus();
                none_of_the_above.setSelected(false);
                view.setSelected(true);
                view.setActivated(true);

                selectedPosition = position;
                customAdapter.setSelectedPosition(selectedPosition);

}


    });

my footer button xml

<Button xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/none_of_the_above"
    android:layout_width="match_parent"
    android:layout_height="45dp"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:background="@drawable/store_setup2_found_selection_boxes_modified_states"
    android:focusable="true"
    android:clickable="true"
    android:text="NONE OF THE ABOVE" /> 

Listview xml:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/selection_1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="20dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="20dp"
    android:clickable="false"
    android:focusable="false"
    android:descendantFocusability="blocksDescendants" >

I have now taken the onTouchMethod out of the OnItemClick of listview and put it above and it receives click on first try. But now the Listview doesnt lose focus when clicked first and then the footer is clicked.

johntzan
  • 323
  • 2
  • 13

3 Answers3

1

solved by putting onTouch method outside onItemClick as well as one inside onItemClick to interact with listview when listview has been selected.

also added listview.clearChoices()inside the onTouch method outside onIemClick

johntzan
  • 323
  • 2
  • 13
0

Move your

        none_of_the_above.setOnTouchListener(new View.OnTouchListener() {

            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    v.setSelected(true);
                    view.setSelected(false);
                    view.setActivated(false);
                    view.clearFocus();

                    Log.e("NoneOFTheAboveButton:onTouch", "clicked");
                    none_of_the_above.setBackground(getResources().getDrawable(R.drawable.store_setup2_found_selection_boxes_modified_states));
                }
                return false;
            }
        });

outside of listView.setOnItemClickListener(). Put it before this line, not in onItemClick() method.

Then in onItemClick() method put:

none_of_the_above.setTag(view);

And in onTouch() use this:

if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    v.setSelected(true);
                    (v.getTag()).setSelected(false);
                    (v.getTag()).setActivated(false);
                    (v.getTag()).clearFocus();

                    Log.e("NoneOFTheAboveButton:onTouch", "clicked");
                    v.setBackground(getResources().getDrawable(R.drawable.store_setup2_found_selection_boxes_modified_states));
                }
GIGAMOLE
  • 1,274
  • 1
  • 11
  • 17
  • Doing this works and it receives click, but then the listview focus doesn't behave properly(doesn't get unselected when footer is clicked). The footer also doesn't hold onto focus and when i go to continue to next activity, my check to see if an item was selected gives error. – johntzan Feb 06 '15 at 16:15
  • Maybe your Footer layout have `GONE` visibility bu default. – GIGAMOLE Feb 06 '15 at 16:26
  • After some testing, only the listview wasn't losing focus on footer click. so the footer was behaving properly except for the listview not losing focus on its click. This last addition you've added, gives me cannot resolve methods for all the v.getTag() methods. – johntzan Feb 06 '15 at 16:33
0

In your Button xml:

android:clickable="false"

The onItemClickListener will not work, if clickable is true for the element within the list.

android:clickable="true" mean's that it's not clickable?

You will also need to set:

android:focusable="false

As the first click is receiving the focus.

https://stackoverflow.com/a/20473421/3956566

Community
  • 1
  • 1
  • this did not work. Tried both. setting focusable to false gives me indexoutofbounds exception as well as not working. – johntzan Feb 06 '15 at 17:19
  • The listview has a tablelayout with a bunch of textviews. And the button is just the footer. My issue now is not the footer receiving focus and selectable on first click, but instead now when clicking on the listview and then the footer, the listview doesnt lose focus. – johntzan Feb 06 '15 at 17:32
  • any idea why this is happening? – johntzan Feb 06 '15 at 18:17