Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

recyclerview adapter multiple view types

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    class ViewHolder0 extends RecyclerView.ViewHolder {
        ...
        public ViewHolder0(View itemView){
        ...
        }
    }

    class ViewHolder2 extends RecyclerView.ViewHolder {
        ...
        public ViewHolder2(View itemView){
        ...
    }

    @Override
    public int getItemViewType(int position) {
        // Just as an example, return 0 or 2 depending on position
        // Note that unlike in ListView adapters, types don't have to be contiguous
        return position % 2 * 2;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
         switch (viewType) {
             case 0: return new ViewHolder0(...);
             case 2: return new ViewHolder2(...);
             ...
         }
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        switch (holder.getItemViewType()) {
            case 0:
                ViewHolder0 viewHolder0 = (ViewHolder0)holder;
                ...
                break;

            case 2:
                ViewHolder2 viewHolder2 = (ViewHolder2)holder;
                ...
                break;
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: nth fibonacci number java using for loop 
Java :: what is jar file in java 
Java :: Implementing the LinkedList Class in java list 
Java :: how to create a new imageview in android java 
Java :: positive numbers in array 
Java :: zoneddatetime java 
Java :: knapsack problem 
Java :: encryption 
Java :: how to create a java txt file from programm 
Java :: how to remove leading space in java 
Java :: recursion java fibonacci 
Java :: java println format 
Java :: Split string into a string array 
Java :: Java short Keyword 
Java :: camunda 
Java :: long in java 
Java :: String to java.sql.Date 
Java :: output of this 
Java :: exoplayer how to put loader while video is still loading android java 
Java :: Java program to print the character or a letter x using star 
Java :: print all prime no java 
Java :: android open app info programmatically 
Java :: how to display an integer in a textfield in java 
Java :: open google maps cycling navigation intent 
Java :: Betrag absolute abs java 
Java :: Java schleifen 
Java :: adding prefix zeros 
Java :: java loop array 
Java :: how to call values from methods in flutter 
Java :: Java Protected Access Modifier package two 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =