Load a fragment on clicking CardView from RecyclerView - android

I've a RecyclerView and CardView inside it. Now what i want to do is load a simple Fragment on clicking the CardView. I'm not able to do that with FragmentManager or SupportFragmentManager. Please give me some directions.
My RecyclerViewAdapder and ViewHolder are ..
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private String[] eventName;
private String[] eventBrief;
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView textViewName, textViewBrief;
public CardView cardView;
public ViewHolder(View v) {
super(v);
textViewName = (TextView)v.findViewById(R.id.textViewName);
textViewBrief = (TextView)v.findViewById(R.id.textViewBrief);
final Context context = v.getContext();
cardView = (CardView)v.findViewById(R.id.card_view);
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
EventFragment eventFragment = EventFragment.newInstance();
// Load and view eventFragment here??
}
});
}
}
public RecyclerViewAdapter(String[] eventName, String[] eventBrief) {
this.eventName = eventName;
this.eventBrief = eventBrief;
}
#Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.my_card_view, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.textViewName.setText(eventName[position]);
holder.textViewBrief.setText(eventBrief[position]);
}
#Override
public int getItemCount() {
return eventName.length;
}
}

A simple implementation is to add a onClickListener() to the ViewHolder 's itemView in the constructor of the Adapter.
public MyViewHolder(View view) {
super(view);
itemView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
AppCompatActivity activity = (AppCompatActivity) view.getContext();
Fragment myFragment = new MyFragment();
activity.getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, myFragment).addToBackStack(null).commit();
}
});
}

I'm not able to do that with FragmentManager or
SupportFragmentManager
That's because RecyclerViewAdapter doesn't hold a reference to the FragmentManager. You should communicate to the Activity/Fragment hosting the RecyclerView, that the event occurred and from there you start the fragment. To do so check my answer here

There are many ways to go at this, really. I feel that the best implementation is a callback to the activity notifying that an item has been clicked. I'll give you a hand with some code, see below:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private String[] eventName;
private String[] eventBrief;
private OnItemClickListener listener;
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView textViewName, textViewBrief;
public CardView cardView;
private OnItemClickListener listener;
public ViewHolder(View v, OnItemClickListener listener) {
super(v);
textViewName = (TextView)v.findViewById(R.id.textViewName);
textViewBrief = (TextView)v.findViewById(R.id.textViewBrief);
final Context context = v.getContext();
cardView = (CardView)v.findViewById(R.id.card_view);
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(listener!=null)
listener.onItemClick(textViewName.getText().toString(),
textViewBrief.getText().toString();
}
});
}
}
public RecyclerViewAdapter(String[] eventName, String[] eventBrief) {
this.eventName = eventName;
this.eventBrief = eventBrief;
}
#Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.my_card_view, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.textViewName.setText(eventName[position]);
holder.textViewBrief.setText(eventBrief[position]);
holder.setOnItemClickListener(listener);
}
#Override
public int getItemCount() {
return eventName.length;
}
public void setOnItemClickListener(OnItemClickListener listener){
this.listener = listener
}
public interface OnItemClickListener {
public void onItemClick(String textName, String textViewBrief);
}
}
Then in your activity just instantiate it like this:
RecyclerViewAdapter adapter = new RecyclerViewAdapter(eventName, eventBrief);
adapter.setOnItemClickListener(new RecyclerViewAdapter.OnItemClickListener(){
public void onItemClick(String textName, String textViewBrief){
EventFragment eventFragment = EventFragment.newInstance();
//replace content frame with your own view.
FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.replace(R.id.content_frame, eventFragment).commit()
}
});

Related

How can i fix my problem with RecyclerView and OnClickAdapter?

Sorry, i have a problem, i have created ad adapter for a recyclerView to click on one of my item but it doesn't work, it is very frustrating, do you have any ideas?
I have used it in a old application and it works properly but now i don't know why it doesn't work...
Here is my adapter:
public class TecniciAdapter extends RecyclerView.Adapter<TecniciAdapter.MyViewHolder> {
private Context mContext;
private List<Utenti> tecniciList;
private OnTecniciListner onTecniciListner;
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView nome;
public TextView cognome;
public TextView stato;
public OnTecniciListner onTecniciListner;
public MyViewHolder(View view, OnTecniciListner onTecniciListner) {
super(view);
nome = view.findViewById(R.id.textNome);
cognome = view.findViewById(R.id.textCognome);
stato = view.findViewById(R.id.textCurrentState);
this.onTecniciListner = onTecniciListner;
view.setOnClickListener(this);
}
#Override
public void onClick(View v) {
onTecniciListner.onTecniciClick(getAdapterPosition());
}
}
public TecniciAdapter(Context context, ArrayList<Utenti> list, OnTecniciListner onTecniciListner) {
this.mContext = context;
this.tecniciList = list;
this.onTecniciListner = onTecniciListner;
}
#NonNull
#Override
public TecniciAdapter.MyViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(mContext)
.inflate(R.layout.technician_item, viewGroup, false);
return new TecniciAdapter.MyViewHolder(view, onTecniciListner);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder myViewHolder, int i) {
myViewHolder.nome.setText(tecniciList.get(i).getNome());
myViewHolder.cognome.setText(tecniciList.get(i).getCognome());
myViewHolder.stato.setText(tecniciList.get(i).getStato());
}
#Override
public int getItemCount() {
return tecniciList.size();
}
public interface OnTecniciListner{
void onTecniciClick(int position);
}
}
and this is the part in the fragment:
TecniciAdapter adapter = new TecniciAdapter(getActivity(), tecniciList, TecniciFragment.this);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setAdapter(adapter);
I have also this in my fragment:
#Override
public void onTecniciClick(int position) {
Utenti tecnici = tecniciList.get(position);
Intent intent = new Intent(getActivity(), LoginActivity.class);
startActivity(intent);
}
You do not need to create an instance of OnTecniciListner in your ViewHolder class, the one in the TecniciAdapter should work just fine.
So your MyViewHolder class should look like this
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView nome;
public TextView cognome;
public TextView stato;
public MyViewHolder(View view) {
super(view);
nome = view.findViewById(R.id.textNome);
cognome = view.findViewById(R.id.textCognome);
stato = view.findViewById(R.id.textCurrentState);
view.setOnClickListener(this);
}
#Override
public void onClick(View v) {
onTecniciListner.onTecniciClick(getAdapterPosition());
}
}
in onBindViewHoldertry this :
myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});

Recyclerview onItemClick not working (no log)

I wanted to implement an OnItemClick on the elements of my RecyclerView and i built an interface on my adapter and I am calling it on my activity, however the click isn't working and there is no errors in the log, so I'm not sure what I'm messing up. It is a Grid Recyclerview, if that affects anything.. The toast doesn't show up either.
my adapter
public class ImagesAdapter extends RecyclerView.Adapter<ImagesAdapter.ImageViewHolder> {
private Context mContext;
private List<Images> mUploads;
private OnItemClickListener mListener;
public ImagesAdapter(Context context, List<Images> uploads) {
mContext = context;
mUploads = uploads;
}
#Override
public ImageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(mContext).inflate(R.layout.layout_view_post, parent, false);
return new ImageViewHolder(v);
}
#Override
public void onBindViewHolder(ImageViewHolder holder, int position) {
Picasso.get().load(mUploads.get(position).getImage_url2()).fit().centerCrop().into(holder.imageView);
}
#Override
public int getItemCount() {
return mUploads.size();
}
public class ImageViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public ImageView imageView;
LinearLayout view_container;
public ImageViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.thumbnail2);
view_container = itemView.findViewById(R.id.container2);
}
#Override
public void onClick(View v) {
if (mListener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
mListener.onItemClick(position);
}
}
}
}
public interface OnItemClickListener {
void onItemClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener) {
mListener = listener;
}
}
My activity
public class ProfileActivity2 extends AppCompatActivity implements OnMapReadyCallback, ImagesAdapter.OnItemClickListener {
mRecyclerview = findViewById(R.id.recyclerview_insideprofile);
mRecyclerview.setLayoutManager(new GridLayoutManager(this, 2));
mRecyclerview.setHasFixedSize(true);
mAdapter = new ImagesAdapter(ProfileActivity2.this, mImages);
mRecyclerview.setAdapter(mAdapter);
mStorage = FirebaseStorage.getInstance();
mDatabaseRef = FirebaseDatabase.getInstance().getReference("Building_Images");
mAdapter.setOnItemClickListener(ProfileActivity2.this);
#Override
public void onItemClick(int position) {
Toast.makeText(ProfileActivity2.this, "TestMessage", Toast.LENGTH_SHORT).show();
Images selectedItem = mImages.get(position);
final String image_url = selectedItem.getImage_url2();
Intent mainIntent = new Intent(ProfileActivity2.this, FullScreenImageActivity.class);
mainIntent.putExtra("b_image_url", image_url);
startActivity(mainIntent);
}
You forgot to add itemView.setOnClickListener in your viewholder, you have to add:
public ImageViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.layout);
itemView.setOnClickListener(this);

How to to set onClick in the RecyclerView?

I have created a RecyclerView Adapter but now i'm struggling to do the onClick to open new activity.
This is my RecyclerView Adapter class:
public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.ViewHolder> {
private ArrayList<HashMap<String, String>> mDataset;
Context ctx;
public RecycleAdapter(ArrayList<HashMap<String, String>> mDataset) {
this.mDataset = mDataset;
this.ctx = ctx;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_procedure, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.getId().setText(mDataset.get(position).get("id"));
holder.getItemname().setText(mDataset.get(position).get("name"));
//holder.getIvImage().setBackground(mDataset.get(position).get("icon"));
}
#Override
public int getItemCount() {
return mDataset.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private CardView cardView;
private TextView itemId;
private TextView tvName;
private ImageView ivImage;
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
itemId = itemView.findViewById(R.id.tvProfileItemName);
tvName = itemView.findViewById(R.id.tvProfileItemAge);
ivImage = itemView.findViewById(R.id.imageView3);
}
public TextView getId() {
return itemId;
}
public TextView getItemname() {
return tvName;
}
public ImageView getIvImage() {
return ivImage;
}
#Override
public void onClick(View v) {
}
}
}
My Main Activity just display the data inside of a recycler view
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
ListAdapter adapter = new SimpleAdapter(MainActivity.this, contactList,
R.layout.list_item, new String[]{"id", "name", "icon"},
new int[]{R.id.id, R.id.name, R.id.icon});
mAdapter = new RecycleAdapter(contactList);
mRecyclerView.setAdapter(mAdapter);
}
use interface
in recycler view adapter class
public interface recClick{
void onClickRec(int p);
}
private recClick rc;
public void setRecClick(recClick rc){
this.rc = rc
}
in recycler view holder class
#Override
public void onClick(View v) {
rc.onClickRec(getAdapterPosition());
}
in MainActivity use Interface
class MainActivity implements AdapterClass.recClick{
mAdapter.setRecClick(this);
#Override
public onClickRec(int p)
{
// do someting
}
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
final int pos =position;
holder.getId().setText(mDataset.get(position).get("id"));
holder.getItemname().setText(mDataset.get(position).get("name"));
//holder.getIvImage().setBackground(mDataset.get(position).get("icon"));
holder.cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(context,NewActivity.class);
intent.putExtra("yourValue", String.valueOf(mDataset.get(position).get(pos)));
ctx.startActivity(intent);
}
});
}
Remember to give reference to your cardview
cardView = (CardView)itemView.findViewById(R.id.cardview);

Android RecyclerView onClick in different Activity

Everything works well and my onClick in my recyclerView is working in getting the positions of my items, but what my design calls for is to be able to click an item of the recyclerView and open up a new activity (as a popover or pop up). I can achieve this but my problems comes with the information I need to display on the popover. The information comes like this inside the activity (inside a Firebase value call)
attributeList.removeAll(attributeList);
for (DataSnapshot child : dataSnapshot.child("Attribute").getChildren()){
Attribute attribute = child.getValue(Attribute.class);
attribute_list newAttributeList = new attribute_list( attribute.Name + ": " + attribute.Value);
attributeList.add(newAttributeList);
}
attributeAdapter = new attribute_list_adapter(attributeList, getContext());
recyclerAttribute.setAdapter(attributeAdapter);
This works perfectly for displaying the information, but there's more then just a "value" and a "name" associated with the click.
Basically when I select an item, I need to get the position of the item clicked (which I have) and compare it to the position inside attributeList so I can call a Firebase call (or pass the data somehow) to the popover to display values from the "Attribute" class (such as Name, Value, Description, and another list (recyclerView).
My recyclerView:
public class attribute_list_adapter extends RecyclerView.Adapter<attribute_list_adapter.ViewHolder> {
private List<attribute_list> listItems;
private Context context;
public attribute_list_adapter(List<attribute_list> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.attribute_list, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
attribute_list listItem = listItems.get(position);
holder.txtTitle.setText(listItem.getTxtTitle());
}
#Override
public int getItemCount() {
return listItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView txtTitle;
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
txtTitle = (TextView) itemView.findViewById(R.id.txtTitle);
}
#Override
public void onClick(View v) {
}
}
}
This is example:
public class attribute_list_adapter extends RecyclerView.Adapter<attribute_list_adapter.ViewHolder> {
private List<attribute_list> listItems;
private Context context;
public attribute_list_adapter(List<attribute_list> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.attribute_list, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.itemView.setOnClickListener(new View.onClickListener() {
#Override
public void onClick(View v) {
onItemClickListener.onItemClick(position);
}
});
}
#Override
public int getItemCount() {
return listItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView txtberita;
ImageView imgberita;
TextView txtnama;
public ViewHolder(View itemView) {
super(itemView);
txtnama = (TextView) itemView.findViewById(R.id.txtnama);
txtberita = (TextView) itemView.findViewById(R.id.txtberita);
imgberita = (ImageView) itemView.findViewById(R.id.imgberita);
}
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener){
this.onItemClickListener = onItemClickListener;
}
OnItemClickListener onItemClickListener;
public interface OnItemClickListener{
void onItemClick(int position);
}
}
your Activity. in Oncreate()
public class TestActivity extends AppCompatActivity implements attribute_list_adapter.OnItemClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
attribute_list_adapter adapter = new attribute_list_adapter(listItems, this);
adapter.setOnItemClickListener(this);
}
#Override
public void onItemClick(int position) {
// code here
}
}
Create an interface something like
public interface OnSingleItemClickListener{
void onSingleItemClick(int position);
}
Then implement it on your ViewHolder like this
public class ViewHolder extends RecyclerView.ViewHolder implements OnSingleItemClickListener {
public ViewHolder(View itemView) {
super(itemView);
}
#Override
void onSingleItemClick(int position){
if(listItems.get(position) == listItems.get(getAdapterPosition)){
// TODO do something here
}
}
now on your OnBindViewHolder inside your adapter you must do this.
holder.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
holder.onSingleItemClick(position);
}
}):

Recyclerview old item view doesn't remove when click another item?

I have a recyclerview . I click on a item. It is highlighted ( sets background - color) . And then, I click another item , It doesn't remove old highlighted view. My class is very leak . Also could you give me an advice to make more effective ?
public class DrawerMenuShelfListAdapter extends RecyclerView.Adapter<DrawerMenuShelfListAdapter.ViewHolder> implements View.OnClickListener {
private int selectedRow = 0;
public Fragment fragment;
public ViewHolder holder;
public RecyclerView recyclerView;
private ArrayList<ShelfModel> shelfList;
public DrawerMenuShelfListAdapter(Fragment fragment, ArrayList<ShelfModel> shelfList, RecyclerView recyclerView) {
this.shelfList = shelfList;
this.fragment = fragment;
this.recyclerView = recyclerView;
}
public void setItemSelected(int position){
recyclerView.getChildAt(selectedRow).setBackgroundColor(fragment.getActivity().getResources().getColor(android.R.color.transparent));
notifyItemChanged(selectedRow);
selectedRow = position;
recyclerView.getChildAt(selectedRow).setBackgroundColor(fragment.getActivity().getResources().getColor(android.R.color.black));
notifyItemChanged(selectedRow);
}
public void updateList(ArrayList<ShelfModel> list){
shelfList.clear();
shelfList.addAll(list);
notifyDataSetChanged();
notifyItemChanged(selectedRow);
}
#Override
public DrawerMenuShelfListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v= LayoutInflater.from(parent.getContext())
.inflate(R.layout.navigation_drawer_list_row, parent, false);
holder = new ViewHolder(v);
holder.text = (TextView) v.findViewById(R.id.definitionText);
holder.total = (TextView) v.findViewById(R.id.countText);
holder.editButton = (ImageButton)v.findViewById(R.id.editButton);
holder.text.setTypeface(App.MUSEO_100);
holder.total.setTypeface(App.MUSEO_300);
return holder;
}
#Override
public void onBindViewHolder(ViewHolder holder, final int position) {
final ShelfModel shelfModel = shelfList.get(position);
holder.text.setText(shelfModel.getName());
holder.total.setText(String.valueOf(shelfModel.getTotal()));
holder.shelfId = shelfModel.getShelfId();
holder.itemView.setOnClickListener(this);
}
#Override
public int getItemCount() {
return shelfList.size();
}
#Override
public void onClick(View v) {
int position = recyclerView.getChildLayoutPosition(v);
((DrawerMenuShelfListListener)fragment).onShelfItemClick(shelfList.get(position));
setItemSelected(position);
}
public interface DrawerMenuShelfListListener extends BaseRecyclerViewListener {
void onShelfItemClick(ShelfModel shelfModel);
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView text;
public TextView total;
public ImageView editButton;
public int shelfId;
public ViewHolder(View view) {
super(view);
}
}
}
Ok. I found the solution.If I don't call notifyItemChanged , it works

Categories

Resources