1

I have problem with ListView and Adapter. ListView setOnItemClickListener and setOnItemLongClickListener does not respond but onScroll works perfectly. Here is the code and you probably have solution for me...

Thanks in advance.

public class AFragment extends Fragment {

View rootView;
ListView l;
public static AArrayAdapter adapter;

public AFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.fragment_list, container, false);
    return rootView;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState){
    l = (ListView) rootView.findViewById(R.id.listView);

    adapter = new AArrayAdapter(getActivity(), R.layout.list_item_adapter, new ArrayList<AString>());
    l.setAdapter(adapter);

    l.setOnItemLongClickListener(new OnItemLongClickListener() {
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            //Toast doesn't work
            return true;
        }
    });

    aList();

    l.setOnScrollListener(new OnScrollListener() {
        private int mLastFirstVisibleItem;

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            //
        }
        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            if (mLastFirstVisibleItem < firstVisibleItem) {
                // working
            }
            if (mLastFirstVisibleItem > firstVisibleItem) {
                // working
            }
            mLastFirstVisibleItem = firstVisibleItem;
        }
    });

   l.setOnItemClickListener(new OnItemClickListener() {
         @Override
         public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
             //Toast doesn't work
         }
     });
}

public void aList(){
    ...
}

/***/

public class AArrayAdapter extends ArrayAdapter<AString> {
    private final Context context;
    private List<AString> items;

    public AArrayAdapter(Context context, int layoutResourceId, List<AString> items) {
        super(context, layoutResourceId, items);
        this.context = context;
        this.items = items;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View rowView = inflater.inflate(R.layout.list_item_adapter, parent, false);

        AHolder holder = null;
        holder = new AHolder();
        holder.acc = items.get(position);
        holder.id = (TextView) rowView.findViewById(R.id.idLine);
        holder.name = (TextView) rowView.findViewById(R.id.firstLine);
        holder.value = (TextView) rowView.findViewById(R.id.secondLine);

        rowView.setTag(holder);

        setupItem(holder);

        final TextView tvId = (TextView) rowView.findViewById(R.id.idLine);
        final ImageButton imageButton = (ImageButton) rowView.findViewById(R.id.icon);

        return rowView;
    }

    private void setupItem(AccHolder holder) {
        holder.name.setText(holder.acc.getName());
        holder.value.setText(holder.acc.getValue());
        holder.id.setText(String.valueOf(holder.acc.getId()));
        holder.removeButton = (ImageButton) rowView.findViewById(R.id.icon);
        holder.removeButton.setTag(holder.account);

        holder.removeButton.setFocusable(false); // <=========== :)
    }

    public class AHolder {
        AString account;
        TextView id;
        TextView name;
        TextView value;
    }
}
}
Arnes
  • 403
  • 1
  • 5
  • 20

2 Answers2

0

This is possibly a duplicate of - ListView items are not clickable. why?

To solve the problem you must not have any focusable items inside the ListView/GridView.

So for Buttons/ImageButtons you must set:

android:focusable="false" 

If you have an ImageButton inside the ListView/GridView you can switch it to an ImageView as the button will not be clickable anyway and this way you do not need to change focusability.

Also as fpr0001 stated in the link provided you could also do:

This answer here worked for me: https://stackoverflow.com/a/16536355/5112161

Mainly he ADDED in the LinearLayout or RelativeLayout the following:

android:descendantFocusability="blocksDescendants"

You also need to REMOVE from all your xml the following:

android:focusable="false"
android:focusable="true"
android:clickable="true"
android:clickable="false"
Community
  • 1
  • 1
Dima Goldin
  • 429
  • 4
  • 9
0

if you have clickable views, Buttons or checkboxes in your row better use Recyclerview.That would be a better choice.You can find basic implementation of Recyclerview here.

The Buttons or checkboxes click listener interfere with the listview click listeners. In the root element of row add one of android:descendantFocusability="beforeDescendants", android:descendantFocusability="blocksDescendants"
Also try adding android:focusable="false" for buttons.

That should solve the issue.

Ravi Theja
  • 3,371
  • 1
  • 22
  • 34