簡體   English   中英

無法在ListView Android中單擊元素

[英]Can't click on element in ListView Android

我正在嘗試創建自定義列表。 我的列表包含在一個Fragment ,該Fragment正確實現了onScrollListener並使用適配器填充了該列表。 問題是我無法單擊每個項目,也無法找出原因。 這是我的布局片段的代碼

    <?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>

這是我的自定義列表的代碼

 <?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>

這是使用adapter創建我的列表並設置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());

                }

            }
        });

這是我的適配器的代碼

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;

}

從ListView中刪除android:clickable="true" ,並將其從XML布局中移除,並從列表項布局的根中移除。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM