0

I don't understand what's going wrong, when clicking an item on the list nothing appends. my environment is minsdk:21, maxsdk:27, java 8 (openjdk), android-studio 3.1.4, terminal android 6.0

note : using FragmentManager or SupportFragmentManager provides the same result.

enter image description here

the fragment list layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
android:descendantFocusability="blocksDescendants"
    >

    <ListView
        android:id="@+id/list"
        android:dividerHeight="1sp"

        android:divider="@color/blue_serenity_transparent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

List row layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
        <GridLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:useDefaultMargins="true"
            android:background="@color/blue_serenity_transparent"
            android:columnCount="3"
            android:rowCount="2"
            >

            <ImageView
                android:id="@+id/notif_type"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:layout_column="0"
                android:layout_row="0"
                android:layout_gravity="center_vertical"
                 />

            <TextView
                android:id="@+id/title"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:layout_column="1"
                android:layout_row="0"
                android:textSize="18dp"
                android:textColor="@color/black"
                />

            <TextView
                android:id="@+id/date"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_row="1"
                android:layout_column="1"
                />

        </GridLayout>
</LinearLayout>

list setup

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    List<NotificationBean> data = new NotificationBean().list(getActivity().getBaseContext());

    View v = inflater.inflate(R.layout.fragment_notifications, container, false);
    list = (ListView) v.findViewById(R.id.list);

    listAdapter = new Adapter(getActivity().getBaseContext(), data);
    list.setAdapter(listAdapter);

    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.e(TAG, "row clicked");
        }
    });

    return v;

}

List adapter

class NotificationsAdapter extends BaseAdapter {

    private Context context;
    private LayoutInflater inflater = null;
    private List<NotificationBean> notifications;

    public NotificationsAdapter(Context context, List<NotificationBean> notifications){
        this.context = context;
        this.notifications = notifications;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return notifications.size();
    }

    @Override
    public Object getItem(int position) {
        return notifications.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View v = convertView;
        if (v == null)
            v = inflater.inflate(R.layout.fragment_notifications_row, null);

        NotificationBean n = notifications.get(position);

        GridLayout layout = (GridLayout) v.findViewById(R.id.row_layout);

        if (dark) layout.setBackgroundColor(getResources().getColor(R.color.blue_serenity_transparent));
        else layout.setBackgroundColor(getResources().getColor(R.color.white));
        dark = !dark;

        ImageView notifType = (ImageView) v.findViewById(R.id.notif_type);
        notifType.setImageResource(n.getIcon());

        TextView title = (TextView) v.findViewById(R.id.title);
        title.setText(n.getTitle());

        TextView date = (TextView) v.findViewById(R.id.date);
        date.setText(Constants.DTF.format(n.getDate()));

        return v;
    }
}
Emmanuel BRUNET
  • 1,286
  • 3
  • 19
  • 46
  • Possible duplicate of [OnItemCLickListener not working in listview](https://stackoverflow.com/questions/5551042/onitemclicklistener-not-working-in-listview) – Nabin Bhandari Sep 08 '18 at 11:17
  • add this line in List row layout's parent LinearLayout android:clickable="false" – Aniruddh Parihar Sep 08 '18 at 11:22
  • Aniruddh , already tryed clickable + focusable:false both on the linear and grid layouts .. it does not work as of android:descendantFocusability="blocksDescendants" solution found in another thread. but thanks for your answer – Emmanuel BRUNET Sep 08 '18 at 12:06

0 Answers0