I have a listView and I am trying to make the items on the listview clickable, so I can go into a details fragment, but for some reason it never goes into the onItemClick method.
I dont have any buttons or similar components on my row, only a couple of custom textviews which extend 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() {.....`
my fragment_listview:
`<?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>`
my row
`<?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`
adapter code:
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();
}
}
CodePudding user response:
just apply clicklistner inside "getView".
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();
}
CodePudding user response:
I think instead on setting onclicklistener on the listview you should set onclicklistener on the relativeLayout of the my row.xml by assigning it an id first