-2

I am making an app in which I want to put texts , images and videos all in separate ViewHolders (similar to Instagram and Facebook). I know that we have to use a Recycler View with multiple ViewTypes . I am using firebase as my storage . How do I retrieve the items from firebase and place them in the appropriate ViewHolder . Please help me.

4 Answers4

1

public class GovtjobsAdapter extends RecyclerView.Adapter {

private List<GovtjobBean> govtjobList;

public class MyViewHolder extends RecyclerView.ViewHolder {
    public TextView tv_govtjob_title,tv_govtjob_lastdatetoapply,tv_govtjob_location;

    public MyViewHolder(View view) {
        super(view);
        tv_govtjob_title = (TextView) view.findViewById(R.id.tv_govtjobs_title);
        tv_govtjob_lastdatetoapply = (TextView) view.findViewById(R.id.tv_govtjob_lastdatetoapply);
        tv_govtjob_location = (TextView) view.findViewById(R.id.tv_govtjob_location);
    }
}


public GovtjobsAdapter(List<GovtjobBean> govtjobList) {
    this.govtjobList = govtjobList;
}

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

    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    GovtjobBean govtjoblist = govtjobList.get(position);
    holder.tv_govtjob_title.setText(govtjoblist.getGovt_title());
    holder.tv_govtjob_lastdatetoapply.setText(govtjoblist.getGovt_lastdatetoapply());
    holder.tv_govtjob_location.setText(govtjoblist.getGovt_location());
}

@Override
public int getItemCount() {
    return govtjobList.size();
}

}

0

You can use the getItemViewType(int position) method of your adapter to do this. In this method, you can check the type of item at the current position if it is an Image, Video or Text and return a specific value for each. Then in your onCreateViewHolder(ViewGroup parent, Int viewType) you can use that viewType parameter to inflate your views.

Bilal Naeem
  • 962
  • 7
  • 13
0

You will need to create a custom recycler view class where you override onCreateViewHolder(ViewGroup parent, Int viewType).

Here is my example:

(1) in your recyclerview class create custom view holder classes. Example:

class ViewHolderCurrent extends RecyclerView.ViewHolder {

    TextView locationTV;
    TextView conditionTV;
    ...

    public ViewHolderCurrent(View listItemView) {
        super(listItemView);

        locationTV = listItemView.findViewById(R.id.location_main_4);
        conditionTV = listItemView.findViewById(R.id.condition_main_4);
        ...
    }
}

(2) override the onCreateViewHolder to your custom view holders:

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    switch (viewType) {
        case 1:
            View viewCurrent = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item4, parent, false);
            return new ViewHolderCurrent(viewCurrent);

        case 2:
        ...

        case 3:
        ...
    }

    return null;
}

(3)Override the onBindViewHolder() to populate the viewholder of your choice. Example:

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    switch (holder.getItemViewType()) {
        case 1:
            ViewHolderCurrent viewHolderCurrent = (ViewHolderCurrent) holder;

            viewHolderCurrent.locationTV.setText(*text*);
            viewHolderCurrent.conditionTV.setText(*text*);

            break;

        case 2:
        ...
        }
}

My full source code is here

Haider Malik
  • 1,581
  • 1
  • 20
  • 23
0

You have to use recyclerview ViewTypes, check the example in this link:

Android RecyclerView Example – Multiple ViewTypes

Or you can use library like FastAdapter, it is awesome library that handle adapter items type:

https://github.com/mikepenz/FastAdapter

Mahmoud Waked
  • 357
  • 2
  • 8