0

I wanna change background color of a ListView, but doesn't work with the listener OnLongClickListener.

The listener OnItemLongClickListener works but why OnLongClickListener no?

Code:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    /*
        etc..
    */
    ListView lv = (ListView)ret.findViewById(R.id.lun_list);
    lv.setAdapter(oa);
    lv.setBackgroundColor(Color.BLACK);

    lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
            showPopup();
            return false;
        }
    });
    lv.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
            showTest();
            return false;
        }
    });

    return ret;
}
private void showTest(){
    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
    getView().findViewById(R.id.lun_list).setBackgroundColor(Color.YELLOW);
}
AlessioDP
  • 88
  • 9

3 Answers3

0

have you added the following attribute to your list view to handle OnLongClickListener please set android:longClickable="true"

Salah Nour ElDin
  • 510
  • 2
  • 13
  • I dont inserted that, with that anyway doesn't works – AlessioDP Jul 30 '15 at 10:01
  • are you use ListActivity or normal Acitity – Salah Nour ElDin Jul 30 '15 at 10:04
  • I'm in a `android.support.v4.app.Fragment`, the MainActivity is a `FragmentActivity` – AlessioDP Jul 30 '15 at 10:05
  • Please use the following code instead of OnLongClickListener use list.setOnItemLongClickListener(new OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView> arg0, View arg1, final int arg2, long arg3) { } }); – Salah Nour ElDin Jul 30 '15 at 10:13
  • I don't need an event for an `ITEM CLICK`, i need to click out of the items of the `ListView`. In Debug I click in the `ListView` but `OUT the items`, it doesn't works. – AlessioDP Jul 30 '15 at 10:15
  • you couldn't click out of the items because listview is group of items in another word if you didn't set items inside listview you didn't have listview so you should deal with listview as a group of items and handle click on items – Salah Nour ElDin Jul 30 '15 at 10:19
  • i set this solution to quick your work instead of searching on it and the following is document for what i say Here is the link for the Android Docs for this method.[link](http://developer.android.com/reference/android/widget/AdapterView.html#setOnItemLongClickListener%28android.widget.AdapterView.OnItemLongClickListener%29) – Salah Nour ElDin Jul 30 '15 at 10:36
  • I avoided the problem, in another method. Anyway solved. – AlessioDP Jul 30 '15 at 10:43
0

Use either listView.setLongClickable(true) in java code or add the attribute android:longClickable:"true" to the ListView in the xml Layout file

finki
  • 2,063
  • 1
  • 20
  • 23
0

Avoided the problem:

I set the ListView in WrapContent Height, so now i can add a OnLongClickListener in the Background of the Fragment and do my events.

AlessioDP
  • 88
  • 9