Using multiple RecyclerView with one Adapter class











up vote
2
down vote

favorite












I have 3 RecyclerView to display most visited market, close by markets and favorite markets.



I have created 3 difference instance of MarketAdapter class for the three RecyclerView



Everything works fine, but my Activity implements one OnClickListener and I cant figure out which adapter was clicked. Is it possible to programmatically determine the Adapter that was clicked from the OnClickListener?



Here is my MarketAdapter Class



public class MarketAdapter extends RecyclerView.Adapter<MarketAdapter.ViewHolder> {

ArrayList<Markets> mMarket = new ArrayList<>();
Context mContext;
private final MarketsItemsClickListener mItemsClickListener;
private final MarketLongClickListener mLongClickListener;
private final MarketClickListener mClickListener;

public MarketAdapter(Context context, MarketsItemsClickListener itemsClickListener, MarketClickListener clickListener, MarketLongClickListener longClickListener){
mContext = context;
mItemsClickListener = itemsClickListener;
mLongClickListener = longClickListener;
mClickListener = clickListener;
}

public interface MarketLongClickListener{
void onLongClick(int position);
}

public interface MarketClickListener{
void onClick(int position);
}

public interface MarketsItemsClickListener{
void imageViewOnClickListener(View view, int position);
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
Context context = viewGroup.getContext();
int layoutForListItem = R.layout.list_market;
LayoutInflater inflater = LayoutInflater.from(context);
boolean shouldAttachToParentImmediately = false;

View view = inflater.inflate(layoutForListItem, viewGroup, shouldAttachToParentImmediately);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
viewHolder.bindView(i);
}

@Override
public int getItemCount() {
if(mMarket != null) return mMarket.size();
return 0;
}

public void setData(ArrayList<Markets> markets){
mMarket = markets;
notifyDataSetChanged();
}

public void addData(Markets market, int position){
mMarket.add(0, market);
notifyDataSetChanged();
}

public Markets getItem(int position){return mMarket.get(position);}

class ViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener, View.OnClickListener{

ImageView mImageView;
TextView mNameTextView, mCityTextView;

public ViewHolder(@NonNull View itemView) {
super(itemView);

mImageView = itemView.findViewById(R.id.image_view);
mNameTextView = itemView.findViewById(R.id.name_text_view);
mCityTextView = itemView.findViewById(R.id.city_text_view);

itemView.setOnLongClickListener(this);
itemView.setOnClickListener(this);
}

void bindView(int position){
Markets market = getItem(position);

mCityTextView.setText(market.getCity());
mNameTextView.setText(market.getName());
}

@Override
public void onClick(View v) {
//Get position of Adapter
int position = getAdapterPosition();
//Handle the click
mClickListener.onClick(position);
}

@Override
public boolean onLongClick(View v) {
return false;
}
}


}



and OnClickListener from Activity



@Override
public void onClick(int position) {}









share|improve this question






















  • does your most visited, close by and fav. market views has same UI structure. or little bit different.
    – Ashish Kudale
    Nov 21 at 16:17










  • You don't need 3 adapters you need 3 different view-holders.
    – Murtaza Khursheed Hussain
    Nov 21 at 16:37















up vote
2
down vote

favorite












I have 3 RecyclerView to display most visited market, close by markets and favorite markets.



I have created 3 difference instance of MarketAdapter class for the three RecyclerView



Everything works fine, but my Activity implements one OnClickListener and I cant figure out which adapter was clicked. Is it possible to programmatically determine the Adapter that was clicked from the OnClickListener?



Here is my MarketAdapter Class



public class MarketAdapter extends RecyclerView.Adapter<MarketAdapter.ViewHolder> {

ArrayList<Markets> mMarket = new ArrayList<>();
Context mContext;
private final MarketsItemsClickListener mItemsClickListener;
private final MarketLongClickListener mLongClickListener;
private final MarketClickListener mClickListener;

public MarketAdapter(Context context, MarketsItemsClickListener itemsClickListener, MarketClickListener clickListener, MarketLongClickListener longClickListener){
mContext = context;
mItemsClickListener = itemsClickListener;
mLongClickListener = longClickListener;
mClickListener = clickListener;
}

public interface MarketLongClickListener{
void onLongClick(int position);
}

public interface MarketClickListener{
void onClick(int position);
}

public interface MarketsItemsClickListener{
void imageViewOnClickListener(View view, int position);
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
Context context = viewGroup.getContext();
int layoutForListItem = R.layout.list_market;
LayoutInflater inflater = LayoutInflater.from(context);
boolean shouldAttachToParentImmediately = false;

View view = inflater.inflate(layoutForListItem, viewGroup, shouldAttachToParentImmediately);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
viewHolder.bindView(i);
}

@Override
public int getItemCount() {
if(mMarket != null) return mMarket.size();
return 0;
}

public void setData(ArrayList<Markets> markets){
mMarket = markets;
notifyDataSetChanged();
}

public void addData(Markets market, int position){
mMarket.add(0, market);
notifyDataSetChanged();
}

public Markets getItem(int position){return mMarket.get(position);}

class ViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener, View.OnClickListener{

ImageView mImageView;
TextView mNameTextView, mCityTextView;

public ViewHolder(@NonNull View itemView) {
super(itemView);

mImageView = itemView.findViewById(R.id.image_view);
mNameTextView = itemView.findViewById(R.id.name_text_view);
mCityTextView = itemView.findViewById(R.id.city_text_view);

itemView.setOnLongClickListener(this);
itemView.setOnClickListener(this);
}

void bindView(int position){
Markets market = getItem(position);

mCityTextView.setText(market.getCity());
mNameTextView.setText(market.getName());
}

@Override
public void onClick(View v) {
//Get position of Adapter
int position = getAdapterPosition();
//Handle the click
mClickListener.onClick(position);
}

@Override
public boolean onLongClick(View v) {
return false;
}
}


}



and OnClickListener from Activity



@Override
public void onClick(int position) {}









share|improve this question






















  • does your most visited, close by and fav. market views has same UI structure. or little bit different.
    – Ashish Kudale
    Nov 21 at 16:17










  • You don't need 3 adapters you need 3 different view-holders.
    – Murtaza Khursheed Hussain
    Nov 21 at 16:37













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I have 3 RecyclerView to display most visited market, close by markets and favorite markets.



I have created 3 difference instance of MarketAdapter class for the three RecyclerView



Everything works fine, but my Activity implements one OnClickListener and I cant figure out which adapter was clicked. Is it possible to programmatically determine the Adapter that was clicked from the OnClickListener?



Here is my MarketAdapter Class



public class MarketAdapter extends RecyclerView.Adapter<MarketAdapter.ViewHolder> {

ArrayList<Markets> mMarket = new ArrayList<>();
Context mContext;
private final MarketsItemsClickListener mItemsClickListener;
private final MarketLongClickListener mLongClickListener;
private final MarketClickListener mClickListener;

public MarketAdapter(Context context, MarketsItemsClickListener itemsClickListener, MarketClickListener clickListener, MarketLongClickListener longClickListener){
mContext = context;
mItemsClickListener = itemsClickListener;
mLongClickListener = longClickListener;
mClickListener = clickListener;
}

public interface MarketLongClickListener{
void onLongClick(int position);
}

public interface MarketClickListener{
void onClick(int position);
}

public interface MarketsItemsClickListener{
void imageViewOnClickListener(View view, int position);
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
Context context = viewGroup.getContext();
int layoutForListItem = R.layout.list_market;
LayoutInflater inflater = LayoutInflater.from(context);
boolean shouldAttachToParentImmediately = false;

View view = inflater.inflate(layoutForListItem, viewGroup, shouldAttachToParentImmediately);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
viewHolder.bindView(i);
}

@Override
public int getItemCount() {
if(mMarket != null) return mMarket.size();
return 0;
}

public void setData(ArrayList<Markets> markets){
mMarket = markets;
notifyDataSetChanged();
}

public void addData(Markets market, int position){
mMarket.add(0, market);
notifyDataSetChanged();
}

public Markets getItem(int position){return mMarket.get(position);}

class ViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener, View.OnClickListener{

ImageView mImageView;
TextView mNameTextView, mCityTextView;

public ViewHolder(@NonNull View itemView) {
super(itemView);

mImageView = itemView.findViewById(R.id.image_view);
mNameTextView = itemView.findViewById(R.id.name_text_view);
mCityTextView = itemView.findViewById(R.id.city_text_view);

itemView.setOnLongClickListener(this);
itemView.setOnClickListener(this);
}

void bindView(int position){
Markets market = getItem(position);

mCityTextView.setText(market.getCity());
mNameTextView.setText(market.getName());
}

@Override
public void onClick(View v) {
//Get position of Adapter
int position = getAdapterPosition();
//Handle the click
mClickListener.onClick(position);
}

@Override
public boolean onLongClick(View v) {
return false;
}
}


}



and OnClickListener from Activity



@Override
public void onClick(int position) {}









share|improve this question













I have 3 RecyclerView to display most visited market, close by markets and favorite markets.



I have created 3 difference instance of MarketAdapter class for the three RecyclerView



Everything works fine, but my Activity implements one OnClickListener and I cant figure out which adapter was clicked. Is it possible to programmatically determine the Adapter that was clicked from the OnClickListener?



Here is my MarketAdapter Class



public class MarketAdapter extends RecyclerView.Adapter<MarketAdapter.ViewHolder> {

ArrayList<Markets> mMarket = new ArrayList<>();
Context mContext;
private final MarketsItemsClickListener mItemsClickListener;
private final MarketLongClickListener mLongClickListener;
private final MarketClickListener mClickListener;

public MarketAdapter(Context context, MarketsItemsClickListener itemsClickListener, MarketClickListener clickListener, MarketLongClickListener longClickListener){
mContext = context;
mItemsClickListener = itemsClickListener;
mLongClickListener = longClickListener;
mClickListener = clickListener;
}

public interface MarketLongClickListener{
void onLongClick(int position);
}

public interface MarketClickListener{
void onClick(int position);
}

public interface MarketsItemsClickListener{
void imageViewOnClickListener(View view, int position);
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
Context context = viewGroup.getContext();
int layoutForListItem = R.layout.list_market;
LayoutInflater inflater = LayoutInflater.from(context);
boolean shouldAttachToParentImmediately = false;

View view = inflater.inflate(layoutForListItem, viewGroup, shouldAttachToParentImmediately);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
viewHolder.bindView(i);
}

@Override
public int getItemCount() {
if(mMarket != null) return mMarket.size();
return 0;
}

public void setData(ArrayList<Markets> markets){
mMarket = markets;
notifyDataSetChanged();
}

public void addData(Markets market, int position){
mMarket.add(0, market);
notifyDataSetChanged();
}

public Markets getItem(int position){return mMarket.get(position);}

class ViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener, View.OnClickListener{

ImageView mImageView;
TextView mNameTextView, mCityTextView;

public ViewHolder(@NonNull View itemView) {
super(itemView);

mImageView = itemView.findViewById(R.id.image_view);
mNameTextView = itemView.findViewById(R.id.name_text_view);
mCityTextView = itemView.findViewById(R.id.city_text_view);

itemView.setOnLongClickListener(this);
itemView.setOnClickListener(this);
}

void bindView(int position){
Markets market = getItem(position);

mCityTextView.setText(market.getCity());
mNameTextView.setText(market.getName());
}

@Override
public void onClick(View v) {
//Get position of Adapter
int position = getAdapterPosition();
//Handle the click
mClickListener.onClick(position);
}

@Override
public boolean onLongClick(View v) {
return false;
}
}


}



and OnClickListener from Activity



@Override
public void onClick(int position) {}






java android android-recyclerview






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 at 16:11









Anga Koko

131110




131110












  • does your most visited, close by and fav. market views has same UI structure. or little bit different.
    – Ashish Kudale
    Nov 21 at 16:17










  • You don't need 3 adapters you need 3 different view-holders.
    – Murtaza Khursheed Hussain
    Nov 21 at 16:37


















  • does your most visited, close by and fav. market views has same UI structure. or little bit different.
    – Ashish Kudale
    Nov 21 at 16:17










  • You don't need 3 adapters you need 3 different view-holders.
    – Murtaza Khursheed Hussain
    Nov 21 at 16:37
















does your most visited, close by and fav. market views has same UI structure. or little bit different.
– Ashish Kudale
Nov 21 at 16:17




does your most visited, close by and fav. market views has same UI structure. or little bit different.
– Ashish Kudale
Nov 21 at 16:17












You don't need 3 adapters you need 3 different view-holders.
– Murtaza Khursheed Hussain
Nov 21 at 16:37




You don't need 3 adapters you need 3 different view-holders.
– Murtaza Khursheed Hussain
Nov 21 at 16:37












3 Answers
3






active

oldest

votes

















up vote
3
down vote



accepted










You can pass a tag in the constructor and get that tag back via a click listener to identify the click as



private final MarketsItemsClickListener mItemsClickListener;
private final MarketLongClickListener mLongClickListener;
private final MarketClickListener mClickListener;
private final String mTag;

public MarketAdapter(Context context, MarketsItemsClickListener itemsClickListener, MarketClickListener clickListener, MarketLongClickListener longClickListener, String tag){
mTag = tag
mContext = context;
mItemsClickListener = itemsClickListener;
mLongClickListener = longClickListener;
mClickListener = clickListener;
}


Modify the listener as



public interface MarketClickListener{
void onClick(int position, String tag);
}


and the listener code in activity as



@Override
public void onClick(int position, String tag) {
switch(tag){
case "adapter1":
break;
case "adapter2":
break;
case "adapter3":
break;
}
}


and create adapter object as



 MarketAdapter adapter = new MarketAdapter("adapter1"....); 
MarketAdapter adapter1 = new MarketAdapter("adapter2"....);
MarketAdapter adapter2 = new MarketAdapter("adapter3"....);


and use



mClickListener.onClick(position, mTag);


Note: You can use enums as well






share|improve this answer






























    up vote
    1
    down vote













    If you want to do it this way, I would add a new variable to the adapters constructor, and then use a case/if statement to determine what you want to do in your onbindviewholder.






    share|improve this answer




























      up vote
      1
      down vote













      You can add a attribute inside MarketAdapter so that you can tell what instance is that adapter.



      Change your custom click listener to receive the adapter type:



      public interface MarketClickListener {
      //You can change this to receive any data you want from the adapter
      void onClick(int position, int adapterType);
      }


      Add the constants, the identifier attribute and change the listener in your adatper:



      public class MarketAdapter extends RecyclerView.Adapter<MarketAdapter.ViewHolder> {
      //The constants types
      public static int MOST_VISITED_MARKETS = 1;
      public static int CLOSE_BY_MARKETS = 2;
      public static int FAVORITE_MARKETS = 3;
      //New attribute
      private int adapterType;

      ...
      //Keep the listener
      private final MarketClickListener mClickListener;

      public MarketAdapter(Context context, MarketsItemsClickListener itemsClickListener, MarketClickListener clickListener, MarketLongClickListener longClickListener, int adapterType){
      ...
      //Set the type
      adapterType = adapterType;
      }

      ...

      }


      In your activity:



      mostVisitedRecyclerView.setAdapter(new MarketAdapter(this,itemsClickListener,clickListener,longClickListener,MarketAdapter.MOST_VISITED_MARKETS ));
      closeByRecyclerView.setAdapter(new MarketAdapter(this,itemsClickListener,clickListener,longClickListener,MarketAdapter.CLOSE_BY_MARKETS));
      favoritesRecyclerView.setAdapter(new MarketAdapter(this,itemsClickListener,clickListener,longClickListener,MarketAdapter.FAVORITE_MARKETS));


      In your view holder, change the onClick:



      @Override
      public void onClick(View v) {
      //Get position of Adapter
      int position = getAdapterPosition();
      //Handle the click
      mClickListener.onClick(position,adapterType);
      }


      I didn't test this but I think it will do the trick. Try it out.






      share|improve this answer





















        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416182%2fusing-multiple-recyclerview-with-one-adapter-class%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        3
        down vote



        accepted










        You can pass a tag in the constructor and get that tag back via a click listener to identify the click as



        private final MarketsItemsClickListener mItemsClickListener;
        private final MarketLongClickListener mLongClickListener;
        private final MarketClickListener mClickListener;
        private final String mTag;

        public MarketAdapter(Context context, MarketsItemsClickListener itemsClickListener, MarketClickListener clickListener, MarketLongClickListener longClickListener, String tag){
        mTag = tag
        mContext = context;
        mItemsClickListener = itemsClickListener;
        mLongClickListener = longClickListener;
        mClickListener = clickListener;
        }


        Modify the listener as



        public interface MarketClickListener{
        void onClick(int position, String tag);
        }


        and the listener code in activity as



        @Override
        public void onClick(int position, String tag) {
        switch(tag){
        case "adapter1":
        break;
        case "adapter2":
        break;
        case "adapter3":
        break;
        }
        }


        and create adapter object as



         MarketAdapter adapter = new MarketAdapter("adapter1"....); 
        MarketAdapter adapter1 = new MarketAdapter("adapter2"....);
        MarketAdapter adapter2 = new MarketAdapter("adapter3"....);


        and use



        mClickListener.onClick(position, mTag);


        Note: You can use enums as well






        share|improve this answer



























          up vote
          3
          down vote



          accepted










          You can pass a tag in the constructor and get that tag back via a click listener to identify the click as



          private final MarketsItemsClickListener mItemsClickListener;
          private final MarketLongClickListener mLongClickListener;
          private final MarketClickListener mClickListener;
          private final String mTag;

          public MarketAdapter(Context context, MarketsItemsClickListener itemsClickListener, MarketClickListener clickListener, MarketLongClickListener longClickListener, String tag){
          mTag = tag
          mContext = context;
          mItemsClickListener = itemsClickListener;
          mLongClickListener = longClickListener;
          mClickListener = clickListener;
          }


          Modify the listener as



          public interface MarketClickListener{
          void onClick(int position, String tag);
          }


          and the listener code in activity as



          @Override
          public void onClick(int position, String tag) {
          switch(tag){
          case "adapter1":
          break;
          case "adapter2":
          break;
          case "adapter3":
          break;
          }
          }


          and create adapter object as



           MarketAdapter adapter = new MarketAdapter("adapter1"....); 
          MarketAdapter adapter1 = new MarketAdapter("adapter2"....);
          MarketAdapter adapter2 = new MarketAdapter("adapter3"....);


          and use



          mClickListener.onClick(position, mTag);


          Note: You can use enums as well






          share|improve this answer

























            up vote
            3
            down vote



            accepted







            up vote
            3
            down vote



            accepted






            You can pass a tag in the constructor and get that tag back via a click listener to identify the click as



            private final MarketsItemsClickListener mItemsClickListener;
            private final MarketLongClickListener mLongClickListener;
            private final MarketClickListener mClickListener;
            private final String mTag;

            public MarketAdapter(Context context, MarketsItemsClickListener itemsClickListener, MarketClickListener clickListener, MarketLongClickListener longClickListener, String tag){
            mTag = tag
            mContext = context;
            mItemsClickListener = itemsClickListener;
            mLongClickListener = longClickListener;
            mClickListener = clickListener;
            }


            Modify the listener as



            public interface MarketClickListener{
            void onClick(int position, String tag);
            }


            and the listener code in activity as



            @Override
            public void onClick(int position, String tag) {
            switch(tag){
            case "adapter1":
            break;
            case "adapter2":
            break;
            case "adapter3":
            break;
            }
            }


            and create adapter object as



             MarketAdapter adapter = new MarketAdapter("adapter1"....); 
            MarketAdapter adapter1 = new MarketAdapter("adapter2"....);
            MarketAdapter adapter2 = new MarketAdapter("adapter3"....);


            and use



            mClickListener.onClick(position, mTag);


            Note: You can use enums as well






            share|improve this answer














            You can pass a tag in the constructor and get that tag back via a click listener to identify the click as



            private final MarketsItemsClickListener mItemsClickListener;
            private final MarketLongClickListener mLongClickListener;
            private final MarketClickListener mClickListener;
            private final String mTag;

            public MarketAdapter(Context context, MarketsItemsClickListener itemsClickListener, MarketClickListener clickListener, MarketLongClickListener longClickListener, String tag){
            mTag = tag
            mContext = context;
            mItemsClickListener = itemsClickListener;
            mLongClickListener = longClickListener;
            mClickListener = clickListener;
            }


            Modify the listener as



            public interface MarketClickListener{
            void onClick(int position, String tag);
            }


            and the listener code in activity as



            @Override
            public void onClick(int position, String tag) {
            switch(tag){
            case "adapter1":
            break;
            case "adapter2":
            break;
            case "adapter3":
            break;
            }
            }


            and create adapter object as



             MarketAdapter adapter = new MarketAdapter("adapter1"....); 
            MarketAdapter adapter1 = new MarketAdapter("adapter2"....);
            MarketAdapter adapter2 = new MarketAdapter("adapter3"....);


            and use



            mClickListener.onClick(position, mTag);


            Note: You can use enums as well







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 21 at 16:37

























            answered Nov 21 at 16:19









            Pavneet_Singh

            26.2k42242




            26.2k42242
























                up vote
                1
                down vote













                If you want to do it this way, I would add a new variable to the adapters constructor, and then use a case/if statement to determine what you want to do in your onbindviewholder.






                share|improve this answer

























                  up vote
                  1
                  down vote













                  If you want to do it this way, I would add a new variable to the adapters constructor, and then use a case/if statement to determine what you want to do in your onbindviewholder.






                  share|improve this answer























                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    If you want to do it this way, I would add a new variable to the adapters constructor, and then use a case/if statement to determine what you want to do in your onbindviewholder.






                    share|improve this answer












                    If you want to do it this way, I would add a new variable to the adapters constructor, and then use a case/if statement to determine what you want to do in your onbindviewholder.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 21 at 16:19









                    Joshua Best

                    9210




                    9210






















                        up vote
                        1
                        down vote













                        You can add a attribute inside MarketAdapter so that you can tell what instance is that adapter.



                        Change your custom click listener to receive the adapter type:



                        public interface MarketClickListener {
                        //You can change this to receive any data you want from the adapter
                        void onClick(int position, int adapterType);
                        }


                        Add the constants, the identifier attribute and change the listener in your adatper:



                        public class MarketAdapter extends RecyclerView.Adapter<MarketAdapter.ViewHolder> {
                        //The constants types
                        public static int MOST_VISITED_MARKETS = 1;
                        public static int CLOSE_BY_MARKETS = 2;
                        public static int FAVORITE_MARKETS = 3;
                        //New attribute
                        private int adapterType;

                        ...
                        //Keep the listener
                        private final MarketClickListener mClickListener;

                        public MarketAdapter(Context context, MarketsItemsClickListener itemsClickListener, MarketClickListener clickListener, MarketLongClickListener longClickListener, int adapterType){
                        ...
                        //Set the type
                        adapterType = adapterType;
                        }

                        ...

                        }


                        In your activity:



                        mostVisitedRecyclerView.setAdapter(new MarketAdapter(this,itemsClickListener,clickListener,longClickListener,MarketAdapter.MOST_VISITED_MARKETS ));
                        closeByRecyclerView.setAdapter(new MarketAdapter(this,itemsClickListener,clickListener,longClickListener,MarketAdapter.CLOSE_BY_MARKETS));
                        favoritesRecyclerView.setAdapter(new MarketAdapter(this,itemsClickListener,clickListener,longClickListener,MarketAdapter.FAVORITE_MARKETS));


                        In your view holder, change the onClick:



                        @Override
                        public void onClick(View v) {
                        //Get position of Adapter
                        int position = getAdapterPosition();
                        //Handle the click
                        mClickListener.onClick(position,adapterType);
                        }


                        I didn't test this but I think it will do the trick. Try it out.






                        share|improve this answer

























                          up vote
                          1
                          down vote













                          You can add a attribute inside MarketAdapter so that you can tell what instance is that adapter.



                          Change your custom click listener to receive the adapter type:



                          public interface MarketClickListener {
                          //You can change this to receive any data you want from the adapter
                          void onClick(int position, int adapterType);
                          }


                          Add the constants, the identifier attribute and change the listener in your adatper:



                          public class MarketAdapter extends RecyclerView.Adapter<MarketAdapter.ViewHolder> {
                          //The constants types
                          public static int MOST_VISITED_MARKETS = 1;
                          public static int CLOSE_BY_MARKETS = 2;
                          public static int FAVORITE_MARKETS = 3;
                          //New attribute
                          private int adapterType;

                          ...
                          //Keep the listener
                          private final MarketClickListener mClickListener;

                          public MarketAdapter(Context context, MarketsItemsClickListener itemsClickListener, MarketClickListener clickListener, MarketLongClickListener longClickListener, int adapterType){
                          ...
                          //Set the type
                          adapterType = adapterType;
                          }

                          ...

                          }


                          In your activity:



                          mostVisitedRecyclerView.setAdapter(new MarketAdapter(this,itemsClickListener,clickListener,longClickListener,MarketAdapter.MOST_VISITED_MARKETS ));
                          closeByRecyclerView.setAdapter(new MarketAdapter(this,itemsClickListener,clickListener,longClickListener,MarketAdapter.CLOSE_BY_MARKETS));
                          favoritesRecyclerView.setAdapter(new MarketAdapter(this,itemsClickListener,clickListener,longClickListener,MarketAdapter.FAVORITE_MARKETS));


                          In your view holder, change the onClick:



                          @Override
                          public void onClick(View v) {
                          //Get position of Adapter
                          int position = getAdapterPosition();
                          //Handle the click
                          mClickListener.onClick(position,adapterType);
                          }


                          I didn't test this but I think it will do the trick. Try it out.






                          share|improve this answer























                            up vote
                            1
                            down vote










                            up vote
                            1
                            down vote









                            You can add a attribute inside MarketAdapter so that you can tell what instance is that adapter.



                            Change your custom click listener to receive the adapter type:



                            public interface MarketClickListener {
                            //You can change this to receive any data you want from the adapter
                            void onClick(int position, int adapterType);
                            }


                            Add the constants, the identifier attribute and change the listener in your adatper:



                            public class MarketAdapter extends RecyclerView.Adapter<MarketAdapter.ViewHolder> {
                            //The constants types
                            public static int MOST_VISITED_MARKETS = 1;
                            public static int CLOSE_BY_MARKETS = 2;
                            public static int FAVORITE_MARKETS = 3;
                            //New attribute
                            private int adapterType;

                            ...
                            //Keep the listener
                            private final MarketClickListener mClickListener;

                            public MarketAdapter(Context context, MarketsItemsClickListener itemsClickListener, MarketClickListener clickListener, MarketLongClickListener longClickListener, int adapterType){
                            ...
                            //Set the type
                            adapterType = adapterType;
                            }

                            ...

                            }


                            In your activity:



                            mostVisitedRecyclerView.setAdapter(new MarketAdapter(this,itemsClickListener,clickListener,longClickListener,MarketAdapter.MOST_VISITED_MARKETS ));
                            closeByRecyclerView.setAdapter(new MarketAdapter(this,itemsClickListener,clickListener,longClickListener,MarketAdapter.CLOSE_BY_MARKETS));
                            favoritesRecyclerView.setAdapter(new MarketAdapter(this,itemsClickListener,clickListener,longClickListener,MarketAdapter.FAVORITE_MARKETS));


                            In your view holder, change the onClick:



                            @Override
                            public void onClick(View v) {
                            //Get position of Adapter
                            int position = getAdapterPosition();
                            //Handle the click
                            mClickListener.onClick(position,adapterType);
                            }


                            I didn't test this but I think it will do the trick. Try it out.






                            share|improve this answer












                            You can add a attribute inside MarketAdapter so that you can tell what instance is that adapter.



                            Change your custom click listener to receive the adapter type:



                            public interface MarketClickListener {
                            //You can change this to receive any data you want from the adapter
                            void onClick(int position, int adapterType);
                            }


                            Add the constants, the identifier attribute and change the listener in your adatper:



                            public class MarketAdapter extends RecyclerView.Adapter<MarketAdapter.ViewHolder> {
                            //The constants types
                            public static int MOST_VISITED_MARKETS = 1;
                            public static int CLOSE_BY_MARKETS = 2;
                            public static int FAVORITE_MARKETS = 3;
                            //New attribute
                            private int adapterType;

                            ...
                            //Keep the listener
                            private final MarketClickListener mClickListener;

                            public MarketAdapter(Context context, MarketsItemsClickListener itemsClickListener, MarketClickListener clickListener, MarketLongClickListener longClickListener, int adapterType){
                            ...
                            //Set the type
                            adapterType = adapterType;
                            }

                            ...

                            }


                            In your activity:



                            mostVisitedRecyclerView.setAdapter(new MarketAdapter(this,itemsClickListener,clickListener,longClickListener,MarketAdapter.MOST_VISITED_MARKETS ));
                            closeByRecyclerView.setAdapter(new MarketAdapter(this,itemsClickListener,clickListener,longClickListener,MarketAdapter.CLOSE_BY_MARKETS));
                            favoritesRecyclerView.setAdapter(new MarketAdapter(this,itemsClickListener,clickListener,longClickListener,MarketAdapter.FAVORITE_MARKETS));


                            In your view holder, change the onClick:



                            @Override
                            public void onClick(View v) {
                            //Get position of Adapter
                            int position = getAdapterPosition();
                            //Handle the click
                            mClickListener.onClick(position,adapterType);
                            }


                            I didn't test this but I think it will do the trick. Try it out.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 21 at 16:42









                            Giovanne

                            784




                            784






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416182%2fusing-multiple-recyclerview-with-one-adapter-class%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Sphinx de Gizeh

                                Dijon

                                Guerrita