FF91
FF91

Reputation: 29

Can't click on element in ListView Android

I am trying to create a custom list. My list is contained in a Fragment that correctly implements onScrollListener and populates the list using an adapter. The problem is that I cannot click on each item and I cannot figure out why. Here there is the code of my layout fragment

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


    <ListView

        android:id="@+id/listNotification"
        android:scrollbars="none"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:footerDividersEnabled="false"
        android:headerDividersEnabled="false"
        android:paddingStart="15dp"
        android:paddingEnd="15dp"
        android:clickable="true"

        />

</LinearLayout>

and here there is the code of my custom list

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


    <LinearLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >

        <ImageView
            android:id="@+id/imageNotification"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:padding="5dp"
            android:focusable="false"/>

        <LinearLayout android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textNotification"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Medium Text"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="5dp"
                android:padding="2dp"
                android:textColor="#33CC33"
                android:focusable="false"/>

            <TextView
                android:id="@+id/idQuestion"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:visibility="gone"
                android:focusable="false"
                />

            <TextView
                android:id="@+id/typeNotification"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:visibility="gone"
                android:focusable="false"/>

           </LinearLayout>

        </LinearLayout>

    </LinearLayout>

here there is the code that creates my list using the adapter and setting the onclicklistener

adapter = new NotificationListAdapter(getActivity(), this.rows);
        list = (ListView) firstAccessView.findViewById(R.id.listNotification);
        list.setAdapter(adapter);

        list.setOnScrollListener(this);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

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

                if(adapter.getItem(position).getTypeNotification()==0) {
                    mNotificationInteface.readQuestion(adapter.getItem(position).getQuestionId());
                }
                if(adapter.getItem(position).getTypeNotification()==1){
                    mNotificationInteface.readAnswer(adapter.getItem(position).getQuestionId());

                }

            }
        });

and here there is the code of my adapter

public class NotificationListAdapter extends ArrayAdapter<NotificationItem> {

private View view;

private final Activity context;
private List<NotificationItem> rows;
private int count = 1;

public NotificationListAdapter(Activity context, List<NotificationItem> firstRows ) {

    super(context, R.layout.list_notifications, firstRows);
    this.context = context;
    this.rows = firstRows;


}

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

    ViewHolder holder;
    if (convertView == null) {
        LayoutInflater inflater = context.getLayoutInflater();
        view = inflater.inflate(R.layout.list_notifications, null);

        view.setPadding(0,10,0,10);

        holder = new ViewHolder();
        holder.textNotification = (TextView) view.findViewById(R.id.textNotification);
        holder.idQuestion = (TextView) view.findViewById(R.id.idQuestion);
        holder.typeNotification = (TextView) view.findViewById(R.id.typeNotification);
        holder.imageNotification = (ImageView) view.findViewById(R.id.imageNotification);

        view.setTag(holder);

    } else {

        view=convertView;
        holder = (ViewHolder) convertView.getTag();
    }


    int typeNotification = this.rows.get(position).getTypeNotification();

    holder.textNotification.setTextColor(Color.BLACK);

    holder.idQuestion.setText(String.valueOf(this.rows.get(position).getQuestionId()));
    holder.typeNotification.setText(String.valueOf(this.rows.get(position).getTypeNotification()));
    if(typeNotification==0){
        holder.textNotification.setText(R.string.askQuestion);
        holder.imageNotification.setImageResource(R.mipmap.iconuseranonymous);
   }
    if(typeNotification==1){
        //nome da recuperare da con id notifica, quindi id utente quindi dome
        holder.textNotification.setText(R.string.answerQuestion);
        Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").transform(new CircleTransform()).fit().centerCrop().into(holder.imageNotification);
    }
    if(typeNotification==2){
        //nome e immagine da recuperare
        holder.textNotification.setText(R.string.newFriend);
        Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").transform(new CircleTransform()).fit().centerCrop().into(holder.imageNotification);


    }

    return view;
}

@Override
public NotificationItem getItem(int position){
    return this.rows.get(position);

}

@Override
public int getCount() {
    return count;
}

public void setCount(int count) {
    this.count = count;
}


static class ViewHolder {

    ImageView imageNotification;
    TextView textNotification;
    TextView idQuestion;
    TextView typeNotification;
    int position;

}

Upvotes: 0

Views: 1897

Answers (1)

Karakuri
Karakuri

Reputation: 38595

Remove android:clickable="true" from the ListView and it's parent in your XML layout, and also from the root of your list item layout.

Upvotes: 1

Related Questions