android OnItemClickListener无法与列表视图一起使用

pkmbmrz7  于 2023-01-07  发布在  Android
关注(0)|答案(2)|浏览(108)

我有一个列表视图,我试图使列表视图上的项目可点击,这样我就可以进入细节片段,但由于某种原因,它从未进入onItemClick方法。
我的行上没有任何按钮或类似的组件,只有几个扩展AppCompatTextView的自定义文本视图。

`listView.setAdapter(adapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getContext(), "dwdwdwdw", Toast.LENGTH_SHORT).show();
            selectRow(position);
        }
    });
    listView.setOnScrollListener(new AbsListView.OnScrollListener() {.....`

我的片段列表视图:

`<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout              xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background">

<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="@null" />

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>`

我的排

`<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/listview_background"
android:padding="10dp">

<....MainFontTextView
    android:id="@+id/nameTextView"
    style="@style/black_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:singleLine="true"
    android:text="New Text" />
+ 4 more textviews`

适配器代码:

public class BasePagingAdapter extends ArrayAdapter<Object> {

protected final static int EMPTY_TYPE = 0;
protected final static int LOADING_TYPE = 1;
protected final static int ITEM_TYPE = 2;

protected ArrayList<?> items;
protected boolean showLoadingRow;

public ArrayList<?> getItems() {
    return items;
}

public boolean isShowLoadingRow() {
    return showLoadingRow;
}

public void setShowLoadingRow(boolean showLoadingRow) {
    this.showLoadingRow = showLoadingRow;
}

public BasePagingAdapter(Context context, int resource, ArrayList<?> items) {
    super(context, resource);
    this.items = items;
}

protected static class EmptyViewHolder {
    TextView emptyTextView;
}

protected static class LoadingViewHolder {
    TextView loadingTextView;
}

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

    if (getItemViewType(position) == EMPTY_TYPE) {
        EmptyViewHolder holder;
        if (row == null || !(row.getTag() instanceof EmptyViewHolder)) {
            LayoutInflater inflater = LayoutInflater.from(getContext());
            row = inflater.inflate(R.layout.row_empty, parent, false);

            holder = new EmptyViewHolder();
            holder.emptyTextView = row.findViewById(R.id.emptyTextView);

            row.setTag(holder);
        }

    } else if (getItemViewType(position) == LOADING_TYPE) {
        LoadingViewHolder holder;
        if (row == null || !(row.getTag() instanceof LoadingViewHolder)) {
            LayoutInflater inflater = LayoutInflater.from(getContext());
            row = inflater.inflate(R.layout.row_loading, parent, false);

            holder = new LoadingViewHolder();
            holder.loadingTextView = row.findViewById(R.id.loadingTextView);

            row.setTag(holder);
        }
        holder = (LoadingViewHolder) row.getTag();
        holder.loadingTextView.setText(String.format(getContext().getString(R.string.list_loading_more), Constants.PAGE_SIZE));
    }

    return row;
}

@Override
public int getCount() {
    if (items.size() > 0) {
        if (showLoadingRow) {
            return items.size() + 1;
        } else {
            return items.size();
        }
    } else {
        return 1;
    }
}

@Override
public int getViewTypeCount() {
    return 3;
}

@Override
public int getItemViewType(int position) {
    if (items.size() == 0) {
        return EMPTY_TYPE;
    } else if (items.size() == position) {
        return LOADING_TYPE;
    } else {
        return ITEM_TYPE;
    }
}

@Override
public boolean isEnabled(int position) {
    return items.size() != 0 && position != items.size();
}

public void updateItems(ArrayList<?> items) {
    this.items = items;
    notifyDataSetChanged();
}

}

ubby3x7f

ubby3x7f1#

只需在“getView”中应用clicklistner即可。

public class BasePagingAdapter extends ArrayAdapter<Object> {

protected final static int EMPTY_TYPE = 0;
protected final static int LOADING_TYPE = 1;
protected final static int ITEM_TYPE = 2;

protected ArrayList<?> items;
protected boolean showLoadingRow;

public ArrayList<?> getItems() {
    return items;
}

public boolean isShowLoadingRow() {
    return showLoadingRow;
}

public void setShowLoadingRow(boolean showLoadingRow) {
    this.showLoadingRow = showLoadingRow;
}

public BasePagingAdapter(Context context, int resource, ArrayList<?> items) {
    super(context, resource);
    this.items = items;
}

protected static class EmptyViewHolder {
    TextView emptyTextView;
}

protected static class LoadingViewHolder {
    TextView loadingTextView;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
 EmptyViewHolder holder = new EmptyViewHolder();
holder.yourView.setonClickListner(new onClickListner){
 /// put your Code here. ....
}
    if (getItemViewType(position) == EMPTY_TYPE) {
       
        if (row == null || !(row.getTag() instanceof EmptyViewHolder)) {
            LayoutInflater inflater = LayoutInflater.from(getContext());
            row = inflater.inflate(R.layout.row_empty, parent, false);

            
            holder.emptyTextView = row.findViewById(R.id.emptyTextView);

            row.setTag(holder);
        }

    } else if (getItemViewType(position) == LOADING_TYPE) {
        LoadingViewHolder holder;
        if (row == null || !(row.getTag() instanceof LoadingViewHolder)) {
            LayoutInflater inflater = LayoutInflater.from(getContext());
            row = inflater.inflate(R.layout.row_loading, parent, false);

            holder = new LoadingViewHolder();
            holder.loadingTextView = row.findViewById(R.id.loadingTextView);

            row.setTag(holder);
        }
        holder = (LoadingViewHolder) row.getTag();
        holder.loadingTextView.setText(String.format(getContext().getString(R.string.list_loading_more), Constants.PAGE_SIZE));
    }

    return row;
}

@Override
public int getCount() {
    if (items.size() > 0) {
        if (showLoadingRow) {
            return items.size() + 1;
        } else {
            return items.size();
        }
    } else {
        return 1;
    }
}

@Override
public int getViewTypeCount() {
    return 3;
}

@Override
public int getItemViewType(int position) {
    if (items.size() == 0) {
        return EMPTY_TYPE;
    } else if (items.size() == position) {
        return LOADING_TYPE;
    } else {
        return ITEM_TYPE;
    }
}

@Override
public boolean isEnabled(int position) {
    return items.size() != 0 && position != items.size();
}

public void updateItems(ArrayList<?> items) {
    this.items = items;
    notifyDataSetChanged();
}
46qrfjad

46qrfjad2#

我认为在列表视图上设置onclicklistener时,您应该在myrow.xml的relativeLayout上设置onclicklistener,方法是先给它分配一个id

相关问题