RecyclerAdapter with different layouts

0

I would like to know how to use a different layout with the layout below, if I'm in Activity A show Layout 1, if I'm in Activity B show Layout 2, I saw it done with viewType, but I could not understand how the viewType works, if you can explain how it works I appreciate it. 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();
    }
}
    
asked by anonymous 26.06.2017 / 00:12

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 it strange to approach the layout of RecyclerView items according to the Activity that calls. Like, are you reusing the same RecyclerView for each Activity? Why not implement a RecyclerView by Activity with your own adapters?

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

In your case it seems to be List<Blog> . Then you need to have some variable in your Blog class that differentiates one instance from another according to the value of that variable, if it does not, create one and, whenever you create an object of that class, initialize the variable with the desired type, since everything that the 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 retrieved with a getType method), in getItemViewType () you can create a switch () that will make the method return the required type that 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.

Of course, you have to have a different ViewHolder for each layout you need and you have to use it according to the switches created in the above methods.

In this link, you have a good tutorial:

link

    
26.06.2017 / 16:40