onClickListener 在 Recycler Adapter 类中不起作用
Posted
技术标签:
【中文标题】onClickListener 在 Recycler Adapter 类中不起作用【英文标题】:onClickListener is not working in Recycler Adapter class 【发布时间】:2021-08-03 17:19:25 【问题描述】:我正在使用 Recycler 视图显示列表,但 setOnclicklistener 在 Recylerview Adapter 类中没有工作。下面是我的适配器类代码和 xml 代码。 我只是简单地实现 setOnClickListener 到适配器相同的 setOnClickListener 在一天前工作并且
几天前我遇到了同样的问题,但我做了另一个适配器,它成功了,但今天我又遇到了同样的问题
虽然我用谷歌搜索但确实得到了答案,我在这里发布了一个问题,我经常遇到我犯错的原因,并记住这一点以获取更多代码。
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.chat.Activities.Statedata;
import com.example.chat.Models.StateCases;
import com.example.chat.R;
import com.example.chat.databinding.StateDataItemBinding;
import java.util.ArrayList;
public class StatelistAdapter extends RecyclerView.Adapter<StatelistAdapter.StatelistHolder>
Context context;
ArrayList<StateCases> stateCases;
public StatelistAdapter(Context context, ArrayList<StateCases> stateCases)
this.context = context;
this.stateCases = stateCases;
@NonNull
@Override
public StatelistHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
View view = LayoutInflater.from(context).inflate(R.layout.state_data_item, parent, false);
return new StatelistHolder(view);
@Override
public void onBindViewHolder(@NonNull StatelistHolder holder, int position)
StateCases scase = stateCases.get(position);
holder.binding.states.setText(scase.getStatename());
holder.binding.sconfirmed.setText(scase.getTodaycases());
holder.binding.stotay.setText(scase.getTotalcases());
holder.binding.todayreovered.setText(scase.getTodayrecovered());
holder.binding.srecovered.setText(scase.getRecovered());
holder.binding.sdeaths.setText(scase.getDeaths());
holder.binding.todaydeath.setText(scase.getTodaydeaths());
holder.itemView.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Intent intent = new Intent(context, Statedata.class);
intent.putExtra("totalcase", scase.getTotalcases());
intent.putExtra("todaycase", scase.getTodaycases());
intent.putExtra("statename", scase.getStatename());
intent.putExtra("todayrecovered", scase.getTodayrecovered());
intent.putExtra("totalrecovered", scase.getRecovered());
intent.putExtra("todaydeaths", scase.getTodaydeaths());
intent.putExtra("totaldeaths", scase.getDeaths());
context.startActivity(intent);
);
@Override
public int getItemCount()
return stateCases.size();
public class StatelistHolder extends RecyclerView.ViewHolder
StateDataItemBinding binding;
public StatelistHolder(@NonNull View itemView)
super(itemView);
binding = StateDataItemBinding.bind(itemView);
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:stretchColumns="1,2,3,4,5"
tools:context=".Activities.Statewiselist">
<com.romainpiel.shimmer.ShimmerTextView
android:id="@+id/lastime"
android:layout_
android:layout_
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:text="Loading..." />
<HorizontalScrollView
android:layout_
android:layout_
android:layout_below="@id/lastime"
android:layout_marginTop="20dp">
<LinearLayout
android:layout_
android:layout_
android:orientation="vertical">
<TableLayout
android:layout_
android:layout_
android:background="#318797"
android:stretchColumns="1,2,3,4,5">
<TableRow
android:layout_
android:layout_
android:layout_marginTop="10dp">
<TextView
android:id="@+id/textView25"
android:layout_
android:layout_
android:layout_marginLeft="10dp"
android:text="States"
android:textColor="#FFFFFF"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView8"
android:layout_
android:layout_
android:text="Total"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView26"
android:layout_
android:layout_
android:layout_marginLeft="10dp"
android:text="Today Cases"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView12"
android:layout_
android:layout_
android:text="Latest Reco."
android:textColor="#FFFFFF"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView28"
android:layout_
android:layout_
android:layout_marginLeft="10dp"
android:text="Recovered"
android:textColor="#FFFFFF"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView13"
android:layout_
android:layout_
android:text="Latest deat."
android:textColor="#FFFFFF"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView29"
android:layout_
android:layout_
android:layout_marginLeft="10dp"
android:layout_marginEnd="5dp"
android:text="Deaths"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="18sp"
android:textStyle="bold" />
</TableRow>
</TableLayout>
<com.cooltechworks.views.shimmer.ShimmerRecyclerView
android:id="@+id/strecyclerview"
android:layout_
android:layout_
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:shimmer_demo_angle="20"
app:shimmer_demo_child_count="10"
app:shimmer_demo_grid_child_count="2"
app:shimmer_demo_layout="@layout/statelistitem"
app:shimmer_demo_layout_manager_type="linear_vertical"
tools:listitem="@layout/state_data_item">
</com.cooltechworks.views.shimmer.ShimmerRecyclerView>
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
提前致谢。
【问题讨论】:
【参考方案1】:在这种情况下,原因可能在StateDataItemBinding
。
state_data_item.xml
中的root view
是否可点击?
【讨论】:
或者子视图可以点击? 测试child view
.setOnClickListener() 是否有效。以上是关于onClickListener 在 Recycler Adapter 类中不起作用的主要内容,如果未能解决你的问题,请参考以下文章
Bitmap、Bitmap.recycle()、WeakReferences 和垃圾回收