0

Sorry for the confusing title, I basically have an XML file used for the RecycleView item. And now I have 2 fragments, I want the appearance of the item to change based on the fragment I included.

3
  • 1
    you can pass boolean in adapter constructor based on from where it is being called. and use that value to check which fragment called your adapter.
    – karan
    Mar 2, 2019 at 4:34
  • cut this to answer so i can upvote for you.
    – nguyen sam
    Mar 2, 2019 at 4:49
  • it fine, Try implementing this in your code and post it as self answer with code that resolves it. Hope it works for you.
    – karan
    Mar 2, 2019 at 4:52

1 Answer 1

0

This is how I do it

I add 1 more field name

private String result

to help this adapter recognized what fragments include, the code I compare the fragment is in the constructor of "TicketHistoryViewHolder".

public class TicketHistoryAdapter extends RecyclerView.Adapter<TicketHistoryAdapter.TicketHistoryViewHolder> {
private Context context;
private String result;
private TicketHistoryAdapter.OnItemClickListener listener;
private final String TAG = "SAM";

public interface OnItemClickListener {
    public void onItemClick(int position);
}

public TicketHistoryAdapter(Context context, OnItemClickListener listener, String result) {
    this.context = context;
    this.listener = listener;
    this.result = result;
}

public TicketHistoryAdapter() {}
@NonNull
@Override
public TicketHistoryViewHolder onCreateViewHolder(@NonNull 
ViewGroup viewGroup, int i) {
LayoutInflater layoutInflater = 
LayoutInflater.from(viewGroup.getContext());

//default view is view name "after confirm"
View finalView=R.layout.item_ticket_after_confirm;

//compare view name by using field "result"
if(result=="beforeConfirm"){
View finalView=R.layout.item_ticket_before_confirm;
  }
    View view = layoutInflater.inflate(finalView, viewGroup, false);
    return new TicketHistoryAdapter.TicketHistoryViewHolder(view);
  }
     }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.