Recycleradapter with different layouts

Asked

Viewed 537 times

0

I would like to know how to use a different layout with the scheme below, If I am in Activity A showing Layout 1, if I am in Activity B show Layout 2, I saw that it is done with viewType , but I could not understand how viewType works, if you can explain how it works I thank you. in my case just have to change even the layout the rest is all the same.

public class RecyclerViewTeste extends RecyclerView.Adapter<RecyclerViewTeste .MyViewHolder> {

    private List<Blog> mQuestionList;
    Context mContext;


    class MyViewHolder extends RecyclerView.ViewHolder {

        View mView;
        TextView title,desc,nome,data,uid,cont,mRetes;
        ImageView mExpand;
        CircleImageView mCirclePerfil;

        MyViewHolder(View view) {
            super(view);
            mView = view;
            mContext = mView.getContext();

            title = (TextView) view.findViewById(R.id.post_title);
            desc = (TextView) view.findViewById(R.id.post_desc);
            nome = (TextView) view.findViewById(R.id.post_username);
            data = (TextView) view.findViewById(R.id.datarow);
            uid = (TextView) view.findViewById(R.id.uid);
        }
        public void setFtperfil(Context ctx, String ftperfil) {
            CircleImageView post_perfil = (CircleImageView) mView.findViewById(R.id.imagemPerfil);
            Picasso.with(ctx).load(ftperfil).into(post_perfil);
        }
    }

    public RecyclerViewClashOfClans(List<Blog> mQuestionList) {
        this.mQuestionList = mQuestionList;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.blog_row_cliente, parent, false);

        return new MyViewHolder(itemView);
    }



    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        final Blog blog = mQuestionList.get(position);
        holder.title.setText(blog.getTitle());
        holder.desc.setText(blog.getDesc());
        holder.nome.setText(blog.getNome());
        holder.data.setText(blog.getData());
        holder.uid.setText(blog.getId_post());
        holder.setFtperfil(getApplicationContext(), blog.getFoto());

        holder.mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent singleBlogIntent = new Intent(mContext, BlogSingleActivityClash.class);

                singleBlogIntent.putExtra("blog_id", blog.getId_post());
                mContext.startActivity(singleBlogIntent);

            }
        });
    }
    @Override
    public int getItemCount() {
        return mQuestionList.size();
    }
}
  • The layout of the list only would change, or change also the objects that would be shown, in case the mQuestionList?

  • in case only the layout, it turns out that I use the same code 5 times in my app only it is presented in different ways depending on where the user is, I just want to decrease codicing lines same...

1 answer

1


It takes more than getItemViewType to solve the problem. First you have to define what you want.

In your case, I found the approach of changing the layout of Recyclerview items according to the Activity you call. Like, are you reusing the same Recyclerview for each Activity? Why not implement a Recyclerview by Activity with your own Dapters?

But finally, explaining your question, who will define the layout types per Recyclerview item are the source data that you will load in the Adapter.

In your case it seems to be the List<Blog>. So you need to have some variable in your Blog class that differentiates one instance from the other according to the value of that variable, if not, create one and, whenever you create an object in that class, initialize the variable with the desired type, because all getItemViewType will do is read this object variable at each Array position.

For example: If your Blog class has a "type" variable (which can be recovered with a getType method), in getItemViewType() you can create a switch() that will make the method return the required type that you will work on the Adapter for each array item.

 private final int TIPO1 = 0;
 private final int TIPO2 = 1;
 private final int TIPO3 = 2;

 public int getItemViewType(int position) {
    int tipo = mQuestionList.get(position).getTipo();
    switch(tipo){
       case TIPO1: return TIPO1;
       case TIPO2: return TIPO2;
       case TIPO1: return TIPO3;
    }
}

With this, in the other Adapter methods (onCreateViewHolder and onBindViewHolder), you will also need to create similar switches to load the layout according to the type.

And of course, you have to have a different Viewholder for every necessary layout and you have to use according to the Switchs created in the above methods.

In this link, there is a good tutorial:

https://guides.codepath.com/android/Heterogenous-Layouts-inside-RecyclerView

  • I think I mean, like I use the firebase.. if in the model I send TYPE 1, when filling the list it will differentiate by TYPE "0,1,2,3" correct?

  • That should do it. Unless you already have some variable in your class that you can already differentiate one object from another by the value of the same, then you wouldn’t need to create a new.

  • That’s exactly what I didn’t understand,you cleared my mind, a few months ago I asked a question of how to use layout for destination and sender in a chat, only I didn’t understand this, now I know that just create a code to differentiate the client id, more on this issue I will continue with the separate Adapter even, Thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.