2

I am trying to create a custom adapter with a dynamic view according to the data retrieved. Everything is set up, I can differentiate the data coming and even implemented getItemViewType and my own getItem to set the type accordingly.

What I had:

@Override
public PostViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    PostItemView v = (PostItemView)inflater.inflate(R.layout.post_item_view, parent, false);
    PostViewHolder postViewHolder = new PostViewHolder(v);
    return postViewHolder;
}

Now given that getItemViewType is implemented and getViewTypeCount (not sure if really needed in recyclerview) I assume it will pick up the right viewType

PostItemView: concerned XML layout part looks like this:

...
<RelativeLayout
       android:id="@+id/post_wrapper"
       style="@style/Match">

        <include
            android:id="@+id/post_item_header_view"
            layout="@layout/post_item_header_view"/>

       <!--OLD XML includes this part and excludes FRAMELAYOUT-->
        <!--<include-->
            <!--android:layout_width="match_parent"-->
            <!--android:layout_height="wrap_content"-->
            <!--android:id="@+id/post_item_image_view"-->
            <!--android:layout_below="@id/post_item_header_view"-->
            <!--layout="@layout/post_item_image_view"-->
            <!--android:layout_alignParentRight="true"-->
            <!--android:layout_alignParentEnd="true" />-->
       <FrameLayout
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:id="@+id/post_item_image_view"
           android:layout_below="@id/post_item_header_view"
           android:layout_alignParentRight="true"
           android:layout_alignParentEnd="true"></FrameLayout>
        <include
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/post_item_text_view"
            android:layout_below="@id/post_item_image_view"
            layout="@layout/post_item_text_view"/>
        <include
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/post_item_action_view"
            android:layout_below="@id/post_item_text_view"
            layout="@layout/post_item_action_view"/>

   </RelativeLayout>
...

After I inflate PostItemView, how can I inflate a specific layout in its sub FrameLayout for each type I have as I iterate through them (I have 6)?

Is there another way to do it?

1 Answers1

0

Here's a dynamic approach:

Assuming you have your own adapter, turn it into an abstract class and create an abstract method called getDynamicView (this will not be your FrameLayout, but the layout you want to add to it). Your FrameLayout will be inflated as you create PostItemView object in your onCreateViewHolder.

Before you instantiate your ViewHolder, you have to get your view using the abstract method which you will have to implement later on in your subclass (adapter you will be eventually using).

@Override
public PostViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    PostItemView v = (PostItemView)inflater.inflate(R.layout.post_item_view, parent, false);
    /**
     this is the abstract method, getDynamicParent will NOT be null as it 
     is already included in your XML and will be inflated as you 
     instantiate PostItemView. Use the viewType value to inflate the proper view
    **/
    View dynamicView = getDynamicView(viewType, v.getDynamicView, v.getDynamicParent);

    if(!dynamicView.equals(v.getDynamiView)){
      v.getDynamicParent.removeAllViews(); //not sure of method name
      v.getDynamicParent.addView(dynamicView); //ViewGroup method
    }

    //you will be using it to bind items to it.
    v.setDynamicView(dynamicView);

    PostViewHolder postViewHolder = new PostViewHolder(v);
    return postViewHolder;
} 

Now create another class called MyAdapter that will extend this one, pass to it context (I assume) in the constructor and implement getDynamicView().

That's it. Your current solution is 80% complete. You can still do everything in one class but this is a better approach if you ever decide to re-use your main adapter later on.

Atieh
  • 230
  • 2
  • 16