Я создал вид рециркулятора и выполняю действие по нажатию, чтобы получить вид фрагмента. когда я щелкаю по элементу, он должен показывать те же данные во фрагменте, поэтому я создал прослушиватель по щелчку в классе Adapter (onBindViewHolder).
package com.example.
public class Adapter extends
RecyclerView. Adapter<ChannelAdapter.ChannelViewHolder> {
private final Context context;
private List<ChannelMode> ;
public Channel Adapter(Context context, List<ChannelModel> channelList) {
this.context = context;
this.channelList = channelList;
}
@Override
public ChannelViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
View view = layoutInflater.inflate(R.layout.layout_view, parent, false);
return new ChannelViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ChannelViewHolder holder, final int position) {
final ChannelModel channelModel = channelList.get(position);
holder.channel_name.setText(channelModel.getChannel_name());
holder.description.setText(channelModel.getDescription());
Picasso.get().load(channelModel.getLogo()).into(holder.logo);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Clicked", Toast.LENGTH_LONG).show();
Fragment fragment = new FragmentView();
AppCompatActivity activity = new AppCompatActivity();
FragmentManager fragmentManager = activity.getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.framelayout, fragment).addToBackStack(null);
fragmentTransaction.commit();
}
});
}
@Override
public int getItemCount() {
return channelList.size();
}
public class ChannelViewHolder extends RecyclerView.ViewHolder {
ImageView logo;
TextView channel_name;
TextView description;
public ChannelViewHolder(@NonNull final View itemView) {
super(itemView);
logo = itemView.findViewById(R.id.imageView);
channel_name = itemView.findViewById(R.id.textViewName);
description = itemView.findViewById(R.id.textViewDesc);
}
}
}
<FrameLayout
android:id="@+id/framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="@+id/recyclerView" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Фрагмент, когда я нажимаю на элементе recyclerview.