I'm trying to change an ImageView in my RecyclerView. I need to change ImageView every time I click the RecyclerView. So, when I click it once it changes to a tick, then I click it again it changes again to be like the beginning, and when I click it again I want to bring up the tick like when I pressed it the first time.
public class RecyclerViewAdapter1 extends RecyclerView.Adapter<RecyclerViewAdapter1.ViewHolder> {
private LayoutInflater inflater;
private FragmentCommunication mCommunicator;
private static String LOG_TAG = "MyRecyclerViewAdapter";
private ArrayList<String> arrayList;
private ArrayList<Integer> IDList;
private ArrayList<Integer> polikhusus;
private int tekan = 0;
private GlobalClass globalVariable;
private static int lastClickedPosition = -1;// Variable to store the last clicked item position
RecyclerViewAdapter1(ArrayList<String> pintuMasuk, ArrayList<Integer> PoliID, ArrayList<Integer> poli, OnTextClickListener listener) {
this.arrayList = pintuMasuk;
this.IDList = PoliID;
this.polikhusus = poli;
this.mListener = listener;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View V = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_poli_design, parent, false);
ViewHolder VH = new ViewHolder(V);
return VH;
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView PintuMasuk;
private ImageView Meme2;
private RelativeLayout ItemList3;
private int selectedIndex;
private Integer PoliID;
public ViewHolder(View itemView) {
super(itemView);
PintuMasuk = itemView.findViewById(R.id.poli);
Meme2 = itemView.findViewById(R.id.gbpoli);
ItemList3 = itemView.findViewById(R.id.item_list3);
}
}
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
final String PintuMasuk = arrayList.get(position);
final Integer PoliID = IDList.get(position);
final Integer poli = polikhusus.get(position);
holder.PintuMasuk.setText(PintuMasuk);
holder.ItemList3.setTag(position);
holder.Meme2.setImageResource(R.drawable.pintumasuk);
holder.ItemList3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
globalVariable = (GlobalClass) view.getContext().getApplicationContext();
globalVariable.PoliID = PoliID;
if(position != lastClickedPosition){
holder.Meme2.setImageResource(R.drawable.policheck);
Snackbar.make(view, "Anda memilih : "+PoliID+" "+PintuMasuk, Snackbar.LENGTH_SHORT).show();
Log.i(LOG_TAG," Clicked on Item " + position);
}else {
// for cancel
Snackbar.make(view, "Anda membatalkan pilihan Anda", Snackbar.LENGTH_SHORT).show();
globalVariable.PoliID = 0;
}
if (lastClickedPosition != -1)
notifyItemChanged(lastClickedPosition);
lastClickedPosition = position;
}
});
}
#Override
public int getItemCount () {
return this.arrayList.size();
}
}
enter image description here
Related
I have a recycler view to show an activity timeline and three floating buttons that click to add activity. How can I code three buttons to a different view? How can I pass the condition to the Adapter?
Now I can show only one viewType.
thx a lot.
Adapter.java
public class TripAdapter extends RecyclerView.Adapter<TripAdapter.TripHolder> {
private List<Trip> trips = new ArrayList<>();
private OnItemClickListener listener;
#NonNull
#Override
public TripHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.trip_item, parent, false);
return new TripHolder(itemView);
}
#Override
public void onBindViewHolder(#NonNull TripHolder holder, int position) {
Trip currentTrip = trips.get(position);
holder.textViewLodgingTitle.setText(currentTrip.getLodgingTitle());
holder.textTextViewLodgingCheckInDateTime.setText(currentTrip.getLodgingCheckInDateTime());
holder.textTextViewLodgingCheckOutDateTime.setText(currentTrip.getLodgingCheckOutDateTime());
holder.textViewLodgingAddress.setText(currentTrip.getLodgingAddress());
}
#Override
public int getItemCount() {
return trips.size();
}
public void setTrips(List<Trip> trips) {
this.trips = trips;
notifyDataSetChanged();
}
public Trip getTripAt(int position) {
return trips.get(position);
}
class TripHolder extends RecyclerView.ViewHolder {
//lodging
private TextView textViewLodgingTitle;
private TextView textTextViewLodgingCheckInDateTime;
private TextView textTextViewLodgingCheckOutDateTime;
private TextView textViewLodgingAddress;
private TextView textViewLodgingPhone;
private TextView textViewLodgingWebsite;
private TextView textViewLodgingEmail;
public TripHolder(#NonNull View itemView) {
super(itemView);
context = itemView.getContext();
textViewLodgingTitle = itemView.findViewById(R.id.text_view_title);
textTextViewLodgingCheckInDateTime = itemView.findViewById(R.id.text_view_start_date_time);
textTextViewLodgingCheckOutDateTime = itemView.findViewById(R.id.text_view_end_date_time);
textViewLodgingAddress = itemView.findViewById(R.id.text_view_description);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int position = getAdapterPosition();
if (listener != null && position != RecyclerView.NO_POSITION) {
listener.onItemClick(trips.get(position));
}
}
});
}
}
public interface OnItemClickListener {
void onItemClick(Trip trip);
}
public void setOnItemClickListener(OnItemClickListener listener) {
this.listener = listener;
}
}
MainActivity.java
//adapter
RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
final TripAdapter adapter = new TripAdapter();
recyclerView.setAdapter(adapter );
tripViewModel = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(this.getApplication())).get(TripViewModel.class);
tripViewModel.getAllTrips().observe(this, new Observer<List<Trip>>() {
#Override
public void onChanged(#Nullable List<Trip> trips) {
adapter.setTrips(trips);
}
});
//this on onActivityResult()
else if (requestCode == ADD_LODGING && resultCode == Activity.RESULT_OK) {
//get data from lodging activity
String lodgingTitle = data.getStringExtra(LodgingEditActivity.EXTRA_LODGING_TITLE);
String lodgingCheckInDateTime = data.getStringExtra(LodgingEditActivity.EXTRA_LODGING_CHECK_IN_DATE_TIME);
String lodgingCheckOutDateTime = data.getStringExtra(LodgingEditActivity.EXTRA_LODGING_CHECK_OUT_DATE_TIME);
String lodgingDescription = data.getStringExtra(LodgingEditActivity.EXTRA_LODGING_DESCRIPTION);
String lodgingAddress = data.getStringExtra(LodgingEditActivity.EXTRA_LODGING_ADDRESS);
String lodgingPhone = data.getStringExtra(LodgingEditActivity.EXTRA_LODGING_PHONE);
String lodgingWebsite = data.getStringExtra(LodgingEditActivity.EXTRA_LODGING_WEBSITE);
String lodgingEmail = data.getStringExtra(LodgingEditActivity.EXTRA_LODGING_EMAIL);
String lodgingImagePath = "test";
Trip lodging = new Trip(lodgingTitle, lodgingCheckInDateTime, lodgingCheckOutDateTime,lodgingDescription, lodgingAddress, lodgingPhone, lodgingWebsite, lodgingEmail,lodgingImagePath);
tripViewModel.insert(lodging);
Toast.makeText(this, "lodging save", Toast.LENGTH_SHORT).show();
}
My application I adapt from codinginflow channel.
https://codinginflow.com/tutorials/android/room-viewmodel-livedata-recyclerview-mvvm/part-1-introduction
I have a number of buttons inside recycview
I just wanted to add them in array and then i get them to add to them some jobs.
At first i defined array name "buttons"
ToggleButton buttons[] = new ToggleButton[5];
Then i put values in it
buttons[position]=holder.playBtn;
Now I want to press one of them
change all the backgrounds of all buttons
And so i used this function :
closeAll()
,but I didn't succeeding
// My class
public class MyListAdapter extends RecyclerView.Adapter<MyListAdapter.ViewHolder> {
public RecyclerViewClickListener mListener;
private MyListData[] listdata;
private Context context;
private ToggleButton [] listBtnPlyStop = null;
ToggleButton buttons[] = new ToggleButton[5];
public MyListAdapter(Context context ,MyListData[] listdata , RecyclerViewClickListener mListener) {
this.mListener =mListener;
this.listdata = listdata;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View listItem= layoutInflater.inflate(R.layout.row_list_azcar, parent, false);
ViewHolder viewHolder = new ViewHolder(listItem);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
final MyListData myListData = listdata[position];
holder.textView.setText(listdata[position].getDescription());
buttons[position]=holder.playBtn;
holder.playBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
mListener.onClick(compoundButton,position);
setBgBtn(compoundButton,checked,position);
}
});
}
private void setBgBtn(CompoundButton compoundButton , boolean checked,int id) {
if(checked){
compoundButton.setBackground(ContextCompat.getDrawable(context, R.drawable.btnpause));
}else{
compoundButton.setBackground(ContextCompat.getDrawable(context, R.drawable.btnplay));
}
closeAll();
}
private void closeAll(){
for(int j=0; j<buttons.length-1;j++) {
buttons[j].setBackgroundResource(R.drawable.btnpause);
}
}
#Override
public int getItemCount() {
return listdata.length;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
public TextView textView;
public LinearLayout linearLayout;
public ToggleButton toggleButton,playBtn;
public RadioButton radioButton1,radioButton2,radioButton3;
private RecyclerViewClickListener mListener;
public ViewHolder(View itemView){
super(itemView);
//this.itemView = itemView;
this.toggleButton =(ToggleButton) itemView.findViewById(R.id.bt_check2);
this.playBtn =(ToggleButton) itemView.findViewById(R.id.bt_play);
this.radioButton1 = (RadioButton)itemView.findViewById(R.id.radio_btn1);
this.radioButton2= (RadioButton)itemView.findViewById(R.id.radio_btn2);
this.radioButton3= (RadioButton)itemView.findViewById(R.id.radio_btn3);
this.textView = (TextView) itemView.findViewById(R.id._txt_kind_of_azkar);
linearLayout = (LinearLayout)itemView.findViewById(R.id.l_containe_row);
}
}
}
I would suggest creating a dynamic array list of ToggleButtons, like so:
private ArrayList<ToggleButton> buttons;
public MyListAdapter(Context context ,MyListData[] listdata , RecyclerViewClickListener mListener) {
this.mListener =mListener;
this.listdata = listdata;
this.context = context;
this.buttons = new ArrayList<>();
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View listItem= layoutInflater.inflate(R.layout.row_list_azcar, parent, false);
ViewHolder viewHolder = new ViewHolder(listItem);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
final MyListData myListData = listdata[position];
holder.textView.setText(listdata[position].getDescription());
buttons.add(holder.playBtn);
...
private void closeAll(){
for(int j=0; j<buttons.size(); j++) {
buttons.get(j).setBackgroundResource(R.drawable.btnpause);
}
}
[Code below] Item in RecycleView doesn't click [it should open the DetailActivity], I try to find the mistake but I didn't understand where
What I try :
Change size items
Change RecycleView to VERTICAL
Write Log.v
I have the same RecycleViewAdapter but it vertical, and it is working well.
my all project : https://github.com/sanke46/E-Commerce
RecycleViewAdapter :
public class SalesRecyclerViewAdapter extends RecyclerView.Adapter<SalesRecyclerViewAdapter.ViewHolder> {
BasketActivity basketActivity = new BasketActivity();
private List<Item> itemList = basketActivity.getBasketItem();
private ArrayList arr;
private Context mContext;
public SalesRecyclerViewAdapter(Context context, ArrayList<Item> data) {
this.arr = data;
mContext = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_sale, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
final Item item = (Item) arr.get(position);
Picasso.with(mContext).load(item.getImageUrl()).into(holder.imageView);
holder.price.setText(item.getPrice() + " $");
holder.price.setPaintFlags(holder.price.getPaintFlags()| Paint.STRIKE_THRU_TEXT_FLAG);
holder.fixPrice.setText(item.getDiscontPrice() + " $");
holder.name.setText(item.getName());
holder.comment.setText(item.getComment());
holder.gramm.setText(item.converGramms(String.valueOf(item.getGramms())));
holder.kal.setText(item.getKalories() + " kal");
holder.addToCart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
itemList.add((Item) arr.get(position));
basketActivity.setBasketItem(itemList);
}
});
holder.linerSaleClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(mContext, DetailActivity.class);
intent.putExtra("item", item);
mContext.startActivity(intent);
Log.v("SALES", "SALES");
}
});
}
#Override
public int getItemCount() {
return arr.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
private RelativeLayout linerSaleClick;
private ImageView imageView;
private TextView price;
private TextView fixPrice;
private TextView name;
private TextView comment;
private TextView gramm;
private TextView kal;
private Button addToCart;
public ViewHolder(View itemView) {
super(itemView);
linerSaleClick = itemView.findViewById(R.id.linerSaleClick);
imageView = itemView.findViewById(R.id.imageSale);
price = itemView.findViewById(R.id.price);
kal = itemView.findViewById(R.id.kal);
gramm = itemView.findViewById(R.id.gramm);
name = itemView.findViewById(R.id.name);
comment = itemView.findViewById(R.id.comments);
fixPrice = itemView.findViewById(R.id.fixPrice);
addToCart = itemView.findViewById(R.id.buttonTwo);
}
}}
Basically code on your github is different than what you posted. So probably the reason of problem is:
In list_sale.xml
I noticed that in this view com.makeramen.roundedimageview.RoundedImageView you set parametr android:clickable="true" then if you want to rise click event on parent (RelativeLayout) it will not work. Solution: remove this line android:clickable="true".
I made an Adapter for my recyclerView. This adapter works when I use on any xxxActivity.java but when I try to use is on a Fragment Its hows error. Doesn't let me Pass the onClickHandler() that I created in Adapter.
I am Setting Adapter Like this -
events_recyclerview.setAdapter(new FixedPlaceListAdapter(getContext(), placelist, mClickHandler)); //ClickListener doesn't work :'(
Another try was like --
events_recyclerview.setAdapter(new FixedPlaceListAdapter(getContext(), placelist, getmClickHandler()));
Here, I implemented getClickHandler() in the Fragment--
public FixedPlaceListAdapter.FixedPlaceListAdapterOnclickHandler getmClickHandler() {
return mClickHandler;
} //Still doesn't work :'(
and the Adapter Part--
Constructor like this-
public FixedPlaceListAdapter(Context mContext, List<PlaceBean>
placeBeanList, FixedPlaceListAdapterOnclickHandler mClickHandler) {
this.mContext = mContext;
this.placeBeanList = placeBeanList;
this.mClickHandler = mClickHandler;
}
I tried to do this ... but still doesn't work--
events_recyclerview.setAdapter(new FixedPlaceListAdapter(getContext(), placelist, FixedPlaceListAdapter.FixedPlaceListAdapterOnclickHandler.mClickhandler));
here is my full adapter code-
public class FixedPlaceListAdapter extends RecyclerView.Adapter<FixedPlaceListAdapter.FixedPlaceListAdapterViewHolder> {
private final FixedPlaceListAdapterOnclickHandler mClickHandler;
Context mContext;
List<PlaceBean> placeBeanList;
public FixedPlaceListAdapter(Context mContext, List<PlaceBean> placeBeanList, FixedPlaceListAdapterOnclickHandler mClickHandler) {
this.mContext = mContext;
this.placeBeanList = placeBeanList;
this.mClickHandler = mClickHandler;
}
public void setData(List<PlaceBean> placeBeanList) {
this.placeBeanList = placeBeanList;
notifyDataSetChanged();
}
#Override
public FixedPlaceListAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.top_list_single, parent, false);
return new FixedPlaceListAdapterViewHolder(view);
}
#Override
public void onBindViewHolder(FixedPlaceListAdapterViewHolder holder, int position) {
PlaceBean pb = placeBeanList.get(position);
holder.nameTextView.setText(pb.getName());
holder.addressTextView.setText(pb.getVicinity());
holder.rating.setRating(pb.getRating());
if (pb.getPhotoref() != null) {
String imageUrl = UrlsUtil.getSinglePhotoUrlString(mContext, pb.getPhotoref(), "350", "300");
Picasso.with(mContext)
.load(imageUrl)
.into(holder.thumbnailImage);
}
}
#Override
public int getItemCount() {
return placeBeanList.size();
}
public interface FixedPlaceListAdapterOnclickHandler {
void onClick(String id);
}
public class FixedPlaceListAdapterViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView nameTextView;
TextView addressTextView;
RatingBar rating;
ImageView thumbnailImage;
public FixedPlaceListAdapterViewHolder(View itemView) {
super(itemView);
nameTextView = (TextView) itemView.findViewById(R.id.place_name_now_in_list);
addressTextView = (TextView) itemView.findViewById(R.id.address_in_list);
rating = (RatingBar) itemView.findViewById(R.id.rating_single_place_in_list);
thumbnailImage = (ImageView) itemView.findViewById(R.id.place_image_thumb);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
String placeID = placeBeanList.get(getAdapterPosition()).getPlaceref();
mClickHandler.onClick(placeID);
}
}
}
Need Help!
public class CategoryNewsListAdapter extends RecyclerView.Adapter<CategoryNewsListAdapter.ViewHolder> {
private List<CategoryNews> actors = new ArrayList<>();
private Context mContext;
private Queue<List<CategoryNews>> pendingUpdates =
new ArrayDeque<>();
**public interface NewsItemClickListener {
void onNewsItemClick(int pos, CategoryNews categoryNews, ImageView shareImageView);
}**
**NewsItemClickListener newsItemClickListener;**
public CategoryNewsListAdapter(List<CategoryNews> personList, Context mContext, NewsItemClickListener newsItemClickListener) {
this.actors.addAll(personList);
this.mContext = mContext;
this.newsItemClickListener = newsItemClickListener;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
final View view = inflater.inflate(R.layout.particular_cat_news_list_row, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Log.d("BusinessSubCategoryListAdapter", "Bindviewholder without payload");
final CategoryNews newsCategory = actors.get(position);
holder.news_title.setText(newsCategory.getPostTitle());
holder.news_date.setText(newsCategory.getPostDate());
holder.news_category.setText(newsCategory.getCategoryName());
holder.news_description.setText(newsCategory.getPostContent());
// GlideApp
// .with(mContext).load(newsCategory.getPostImage())
// .placeholder(R.mipmap.ic_launcher) // can also be a drawable
// .error(R.drawable.placeholder_image) // will be displayed if the image cannot be loaded
// .crossFade()
// .into(holder.news_image);
Picasso.with(mContext).load(newsCategory.getPostImage()).placeholder(R.drawable.placeholder_image).error(R.drawable.placeholder_image).into(holder.news_image);
**holder.itemView.setOnClickListener(v -> {
newsItemClickListener.onNewsItemClick(position, newsCategory, holder.news_image);
});**
}
#Override
public void onBindViewHolder(ViewHolder holder, int position, List<Object> payloads) {
if (payloads.isEmpty()) {
// if empty, do full binding;
onBindViewHolder(holder, position);
return;
}
Bundle bundle = (Bundle) payloads.get(0);
String newTitle = bundle.getString("NAME_CHANGE");
if (newTitle != null) {
// add some animation if you want
final CategoryNews actor = actors.get(position);
holder.news_title.setText(actor.getPostTitle());
holder.news_date.setText(actor.getPostDate());
holder.news_category.setText(actor.getCategoryName());
holder.news_description.setText(actor.getPostContent());
}
}
public void addItems(List<CategoryNews> newItems) {
// record this value before making any changes to the existing list
int curSize = getItemCount();
// update the existing list
actors.addAll(newItems);
// curSize should represent the first element that got added
// newItems.size() represents the itemCount
notifyItemRangeInserted(curSize, newItems.size());
}
public void updtateItems(List<CategoryNews> newItems) {
// record this value before making any changes to the existing list
int curSize = getItemCount();
// update the existing list
actors.addAll(newItems);
// curSize should represent the first element that got added
// newItems.size() represents the itemCount
notifyItemRangeInserted(0,newItems.size()- getItemCount()-1);
}
#Override
public int getItemCount() {
return actors.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private TextView news_title, news_date, news_category, news_description;
private ImageView news_image;
public ViewHolder(View itemView) {
super(itemView);
news_title = (TextView) itemView.findViewById(R.id.news_title);
news_date = (TextView) itemView.findViewById(R.id.news_date);
news_category = (TextView) itemView.findViewById(R.id.news_category);
news_description = (TextView) itemView.findViewById(R.id.news_description);
news_image = (ImageView) itemView.findViewById(R.id.news_image);
}
}
}
And In your activity
implements NewsItemClickListener
create object NewsItemClickListener newsItemClickListner;
inside oncreate newsItemClickListner=this; (you mush have implemented 1)
pass that object to recyclerview.
In adapter it is received by this
public CategoryNewsListAdapter(List personList, Context mContext, NewsItemClickListener newsItemClickListener) {
this.actors.addAll(personList);
this.mContext = mContext;
this.newsItemClickListener = newsItemClickListener;
}
In your activity
CategoryNewsListAdapter categoryNewsListAdapter= new CategoryNewsListAdapter(list,this,newsItemClickListner)
After doing all this your overriden method will be called when you click in item of recycler view where you have set onclick listner
Okay .. The Problem is Fixed now ...
I was passing the wrong value or maybe the method of passing the ClickHandler was wrong.
The Solution of the Problem :
I Created another Class for ClickHandler-
public class PlaceCardClickHandler implements
FixedPlaceListAdapter.FixedPlaceListAdapterOnclickHandler,
PlaceListAdapter.PlaceListAdapterOnclickHandler {
Context mContext;
public PlaceCardClickHandler(Context mContext) {
this.mContext = mContext;
}
#Override
public void onClick(String id) {
Intent intentToStartDetail = new Intent(mContext, PlaceDetailActivity.class);
intentToStartDetail.putExtra("id", id);
mContext.startActivity(intentToStartDetail);
}
}
and the change in the Fragment was like this- (Just passed a object from the ClickHandler class)
events_recyclerview.setAdapter(new FixedPlaceListAdapter(getContext(), placelist, new PlaceCardClickHandler(getContext())));
I have an activity with three fragments, each one with a separate RecyclerView with a bunch of items. When scrolling between fragments, the onClick() gets mixed up and click on an item in fragment 1 goes to the same position but for the item in fragment 3 therefore taking me to the wrong screen. Am I suppose to differentiate the pages in the custom onClick() method I have setup? All the fragments use the same adapter, so I am not sure if that is part to blame then? Not sure what code would be useful here, but here is my adapter class:
public class ExploreAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static OnEntryClickListener mOnEntryClickListener;
private List<Explore> exploreList;
private List<String> trackedProjects;
private Context context;
private Typeface typeFace, italicTypeface, boldTypeface;
private int typeOfExplore;
private HashMap<String, Boolean> uniqueExploreItems;
private HashMap<String, Long> offSet;
private int firstSet, secondSet;
private boolean apiProcessing;
private String stringValue;
//firebase
private DatabaseReference mDatabase;
public void changeTracked(List<String> tracks) {
trackedProjects = tracks;
notifyDataSetChanged();
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView projectTitle, projectCompany;
public ImageView projectPicture;
private ShineButton projectTrack;
public MyViewHolder(View view) {
super(view);
projectTitle = (TextView) view.findViewById(R.id.exploreProjectTitle);
projectCompany = (TextView) view.findViewById(R.id.exploreProjectCompany);
projectPicture = (ImageView) view.findViewById(R.id.exploreProjectPicture);
projectTrack = (ShineButton) view.findViewById(R.id.exploreProjectTrack);
view.setOnClickListener(this);
projectTrack.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (mOnEntryClickListener != null) {
mOnEntryClickListener.onEntryClick(v, getAdapterPosition());
}
}
}
public ExploreAdapter(Context mContext, List<Explore> explores, List<String> trackedProj, int typeOfE, String strV, HashMap<String, Boolean> uniqueE, HashMap<String, Long> offs, Typeface myTypeface, Typeface myTypefaceItalic, Typeface myTypefaceBold) {
context = mContext;
exploreList = explores;
typeFace = myTypeface;
italicTypeface = myTypefaceItalic;
boldTypeface = myTypefaceBold;
typeOfExplore = typeOfE;
uniqueExploreItems = uniqueE;
offSet = offs;
stringValue = strV;
apiProcessing = true;
mDatabase = FirebaseDatabase.getInstance().getReference();
trackedProjects = trackedProj;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new MyViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.explore_item, parent, false));
}
public void setOnEntryClickListener(OnEntryClickListener onEntryClickListener) {
mOnEntryClickListener = onEntryClickListener;
}
public interface OnEntryClickListener {
void onEntryClick(View view, int position);
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
MyViewHolder myViewHolder = (MyViewHolder) holder;
Explore proj = exploreList.get(position);
myViewHolder.projectTitle.setTypeface(boldTypeface);
myViewHolder.projectCompany.setTypeface(italicTypeface);
myViewHolder.projectTitle.setText(proj.getProjectTitle());
myViewHolder.projectCompany.setText(proj.getSubTitle());
//for updates
if(typeOfExplore == 1) {
String createImagePath = Constants.PHASES_IMAGE + proj.getProjectPicture();
Picasso.with(context).load(createImagePath).placeholder(R.drawable.default_liovinci_bg).resize(320, 240).into(myViewHolder.projectPicture);
} else {
String createImagePath = Constants.PROJECT_IMAGE + proj.getProjectPicture();
Picasso.with(context).load(createImagePath).placeholder(R.drawable.default_liovinci_bg).resize(320, 240).into(myViewHolder.projectPicture);
}
if(typeOfExplore == 1 || typeOfExplore == 2) {
myViewHolder.projectTrack.setVisibility(View.GONE);
} else {
if(trackedProjects != null && trackedProjects.contains(proj.getProjectId())) {
myViewHolder.projectTrack.setColorFilter(ContextCompat.getColor(context, R.color.actionBlue), PorterDuff.Mode.SRC_IN);
myViewHolder.projectTrack.setChecked(true);
} else {
myViewHolder.projectTrack.setColorFilter(ContextCompat.getColor(context, R.color.bottomBarGray), PorterDuff.Mode.SRC_IN);
}
}
If you are using view pager then it initialises all the fragments which you have bound to that view pager. So at last it will initialise last fragment and your adapter class will contains context from your last initialised fragment.
This is the reason. so to overcome your problem you should make different adapter for different fragment.