我在我的应用程序中使用recyclerView,当我单击该项目时,我什么也没得到。我尝试打印文本以查看单击是否有效,但没有任何效果。我已经看过许多教程,但仍然无法正常工作。我能知道问题出在哪里吗? 我需要实现此点击。
这是我的代码:
AdapterClass
public class DosAdapter extends
RecyclerView.Adapter<DosAdapter.DosHolder>{
private List<Dossier> dossiers = new ArrayList<>();
private OnItemClickListener listener;
public interface OnItemClickListener {
void onItemClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener) {
this.listener = listener;
}
@NonNull
@Override
public DosAdapter.DosHolder onCreateViewHolder(@NonNull ViewGroup parent,
int viewType)
{
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.folder_item, parent, false);
return new DosHolder(itemView,this.listener);
}
@Override
public void onBindViewHolder(@NonNull DosAdapter.DosHolder holder, int
position)
{
Dossier currentDos = dossiers.get(position);
holder.titre.setText(currentDos.getTitre());
}
public static class DosHolder extends RecyclerView.ViewHolder
{
private TextView titre;
public DosHolder(View itemView, OnItemClickListener listener)
{
super(itemView);
titre = itemView.findViewById(R.id.text_view_titre);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
listener.onItemClick(position);
}
}
}
});
}
}
}
活动
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
recyclerView.addItemDecoration(new
DividerItemDecoration(getApplicationContext(),
DividerItemDecoration.VERTICAL));
DosAdapter adapter = new DosAdapter();
recyclerView.setAdapter(adapter);
adapter.setOnItemClickListener(new DosAdapter.OnItemClickListener(){
@Override
public void onItemClick(int position) {
System.out.println("clicked");
}
});
答案 0 :(得分:0)
您介意使用此功能吗?我确保命名与您的命名匹配,并且您只需复制整个代码并粘贴即可,没有任何错误可以处理,只需确保您阅读了一些注释即可理解而不仅仅是使用。
为您的适配器
public class DosAdapter extends RecyclerView.Adapter<DosAdapter.ViewHolder> {
private OnItemClickListener listener;
private List<Dossier> dossiers = new ArrayList<>();
public DosAdapter(OnItemClickListener listener, List<Dossier> dossiers) {
this.dossiers = dossiers;
this.listener = listener;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.folder_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
try {
final Dossier currentDos = dossiers.get(position);
holder.titre.setText(currentDos.getTitre());
holder.titre.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onItemClick(position);
}
});
} catch (Exception e) {
Log.e("Exception", e.getMessage()+"");
}
}
@Override
public int getItemCount() {
return dossiers.size();
}
/*--------------------------------------------------------------------------------------
| GET REFERENCES TO VIEWS HERE
*--------------------------------------------------------------------------------------*/
class ViewHolder extends RecyclerView.ViewHolder {
private TextView titre;
ViewHolder(View itemView) {
super(itemView);
titre = itemView.findViewById(R.id.text_view_titre);
}
}
}
如何从需要它的活动中使用适配器。
public class MainActivity extends AppCompatActivity implements OnItemClickListener {
private DosAdapter dosAdapter;
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<Dossier> dossiers = new ArrayList<>();
//I'm assuming you already feed your data, so you're not passing null like me here.
recyclerView = findViewById(R.id.recyclerView);
dosAdapter = new DosAdapter(this, dossiers);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setHasFixedSize(true);
recyclerView.addItemDecoration(new
DividerItemDecoration(getApplicationContext(),
DividerItemDecoration.VERTICAL));
recyclerView.setAdapter(dosAdapter);
}
@Override
public void onItemClick(int position) {
System.out.println("clicked position: "+position);
/*
* This will help your tracing from Logcat much easy to trace,
* simply open Logcat and switch from verbose/Debug to Error,
* like that you will not have much logs and tracking the
* printout becomes easier.
* */
Log.e("CLICK TAG", "Clicked position"+position);
}
}
如果您花时间去研究它,您会更好地理解它。