1

Unable to add second child in Recyclerview I am passing two different arrays to RecyclerAdapter to display two child layout with different data and views.Is there any solution to add different child layout using same header layout.I added horizontal Recyclerview in vertical Recyclerview and I want to display details like I attached the image

private void setupRecyclerView(RecyclerView recyclerView) {
     recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
     RecyclerAdapter recyclerAdapter = new RecyclerAdapter();

     int[] images = new int[]{
         R.drawable.finance,
         R.drawable.business,
         R.drawable.financejob,
         R.drawable.ecomchallenges
     };

     ArrayList<ChildView> childViews = new ArrayList<>();
     childViews.add(new ChildView(images[0], "The \"Best\" Startup Pitch Deck - How To Raise Venture Capital", "$100"));
     childViews.add(new ChildView(images[1], "An Entire MBA in 1 Course:Award Winning Business School Prof", "$100"));
     childViews.add(new ChildView(images[2], "What Finance Job is for You? Explanation of 14 Finance Roles", "$100"));
     childViews.add(new ChildView(images[3], "Learn To Build Beautiful HTML5 And CSS3 Websites In 1 Month", "$100"));

     int[] courseImage = new int[] {
         R.drawable.php,
         R.drawable.development,
         R.drawable.web,
         R.drawable.java
     };

     ArrayList<CourseByType> courseByTypes = new ArrayList<>();
     courseByTypes.add(new CourseByType("Technology", courseImage[0]));
     courseByTypes.add(new CourseByType("Business", courseImage[1]));
     courseByTypes.add(new CourseByType("Photography", courseImage[2]));
     courseByTypes.add(new CourseByType("Development", courseImage[3]));
     Log.d("","Above adapter");
     recyclerAdapter.addItem(new GroupView("Business", childViews));
     Log.d("","Below Child");
     recyclerAdapter.addCourseByType(new CourseByHeader("Technology", courseByTypes));
     Log.d("","Below Course");
     recyclerView.setAdapter(recyclerAdapter);

 }

This is the main fragment where I set the values to two different

arraylist ArrayList<ChildView> childViews = new ArrayList<>() 

and

ArrayList<CourseByType> courseByTypes = new ArrayList<>() 

Values of child views are passing properly but CourseByType values are not passing.This is the adapter class for this fragment class.

RecyclerAdapter.java

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {

    ArrayList<PassValues> containerArrayList;

    ArrayList<GroupView> groupViews;
    ArrayList<CourseByHeader>courseByHeaders;
    private static final int TYPE_HEADER = 0;
    private static final int TYPE_ITEM = 1;


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        View view = LayoutInflater.from(context).inflate(R.layout.group_title, parent, false);
        return new ViewHolder(view);
    }

    public RecyclerAdapter(){
        containerArrayList = new ArrayList<>();
        groupViews = new ArrayList<>();
        courseByHeaders = new ArrayList<>();
    }

    public void addContainer(PassValues container){
        containerArrayList.add(container);


    }
    public void addItem(GroupView groupView){
        Log.d("","Inside Group method");
        groupViews.add(groupView);
    }
    public void addCourseByType(CourseByHeader courseByHeader){
        Log.d("","Inside Course method");
        courseByHeaders.add(courseByHeader);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Log.d("", "Pass Values out of IF" + position);
        ChildViewAdapter childViewAdapter = new ChildViewAdapter();
        if(position == 0){
            GroupView groupView = groupViews.get(position);
            holder.title.setText(groupView.getTitle());
            Log.d("", "Passing Values" + groupView.getTitle());
            holder.recyclerView.setLayoutManager(new LinearLayoutManager(holder.recyclerView.getContext(), LinearLayoutManager.HORIZONTAL, false));
            holder.recyclerView.setOnFlingListener(null);
            childViewAdapter.addChild(groupView.getChildViewList());
            holder.recyclerView.setAdapter(childViewAdapter);
        }
        if (position == 1) {
            CourseByHeader courseByHeader = courseByHeaders.get(position);
            holder.title.setText(courseByHeader.getTitle());
            Log.d("", "Passing Values" + courseByHeader.getTitle());
            holder.recyclerView.setLayoutManager(new LinearLayoutManager(holder.recyclerView.getContext(), LinearLayoutManager.HORIZONTAL, false));
            holder.recyclerView.setOnFlingListener(null);
            childViewAdapter.addCourse(courseByHeader.getCourseByTypes());
            holder.recyclerView.setAdapter(childViewAdapter);
        }

    }

    @Override
    public int getItemCount() {
        if(getItemViewType(0) == TYPE_HEADER)
        return groupViews.size() ;
        if (getItemViewType(1) == TYPE_ITEM)
            return courseByHeaders.size();
        else return -1;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView title;
        RecyclerView recyclerView;
        public ViewHolder(View itemView) {
            super(itemView);

            title = (TextView)itemView.findViewById(R.id.course_title);
            recyclerView = (RecyclerView)itemView.findViewById(R.id.group_recycler);

        }
    }
}

This RecyclerAdapter contains one RecyclerView in that first row has one image and 3 textviews and 2nd row has 1 ImageView and 1 TextView. At position first,one image and 3 textviews are shown but it's not going on 2nd view

This is the view I getting after run on emulator.

enter image description here

This are two child for RecyclerViews

ChildView.java

public class ChildView {

    int image;
    String course, price;

    public ChildView(int image, String course, String price) {
        this.image = image;
        this.course = course;
        this.price = price;
    }


    public int getImage() {
        return image;
    }

    public String getCourse() {
        return course;
    }

    public String getPrice() {
        return price;
    }

}

CourseByType.java

public class CourseByType {

    String courseName;
    int courseImage;

    public CourseByType(String courseName, int courseImage) {
        this.courseName = courseName;
        this.courseImage = courseImage;
    }

    public String getCourseName() {
        return courseName;
    }

    public int getCourseImage() {
        return courseImage;
    }
}

CourseByHeader.java

public class CourseByHeader {

    String title;
    ArrayList<CourseByType> courseByTypes;

    public CourseByHeader(String title, ArrayList<CourseByType> courseByTypes) {
        this.title = title;
        this.courseByTypes = courseByTypes;
    }

    public String getTitle() {
        return title;
    }

    public ArrayList<CourseByType> getCourseByTypes() {
        return courseByTypes;
    }
}

GroupView.java

public class GroupView {
    String title;
    ArrayList<ChildView> childViewList;

    String courseBy;
    ArrayList<CourseByType> courseByTypes;

    public GroupView(String title, ArrayList<ChildView> childViewList) {
        this.title = title;
        this.childViewList = childViewList;
    }

    public String getTitle() {
        return title;
    }

    public ArrayList<ChildView> getChildViewList() {
        return childViewList;
    }
}

Groupview and CouseByType class have title and child list for recycleradapter

ChildViewAdapter.java

public class ChildViewAdapter extends RecyclerView.Adapter {

    ArrayList<ChildView> childViewList;
    ArrayList<CourseByType> courseByTypes;
    private static final int TYPE_HEADER = 0;
    private static final int TYPE_ITEM = 1;

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

    public class GroupHolder extends ViewHolder {
        public ImageView iamView;
        public TextView course, price;
        public GroupHolder(View itemView) {
            super(itemView);
            iamView = (ImageView) itemView.findViewById(R.id.course_image);
            course = (TextView) itemView.findViewById(R.id.course_by);
            price = (TextView) itemView.findViewById(R.id.price);
        }
    }

    public void addCourse(ArrayList<CourseByType> courseByType){
        courseByTypes = courseByType;
    }
    public void addChild(ArrayList<ChildView> childView){
        childViewList = childView;
    }

    public class Course extends ViewHolder {

        public ImageView courseTypeImage;
        public TextView courseType;

        public Course(View itemView) {
            super(itemView);
            courseTypeImage = (ImageView)itemView.findViewById(R.id.course_image);
            courseType = (TextView)itemView.findViewById(R.id.course_name_course);
        }
    }

    public ChildViewAdapter() {
        childViewList = new ArrayList<>();
        courseByTypes = new ArrayList<>();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        RecyclerView.ViewHolder vh = null;
        View v;
        if(viewType == TYPE_HEADER){
            v = LayoutInflater.from(context).inflate(R.layout.recycler_item, parent, false);
            return new GroupHolder(v);
        }if(viewType == TYPE_ITEM){
            v = LayoutInflater.from(context).inflate(R.layout.type_of_courses, parent, false);
            return new Course(v);
        }
        return vh;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof GroupHolder){
            Log.d("","instance of Group Holder");
            ChildView childView = childViewList.get(position);
            ((GroupHolder)holder).iamView.setImageResource(childView.getImage());
            ((GroupHolder)holder).course.setText(childView.getCourse());
            ((GroupHolder)holder).price.setText(childView.getPrice());
            return;
        }
        if(holder instanceof Course){
            Log.d("","instance of Course ");
            CourseByType courseByType = courseByTypes.get(position);
            ((Course)holder).courseTypeImage.setImageResource(courseByType.getCourseImage());
            ((Course)holder).courseType.setText(courseByType.getCourseName());
            return;
        }

    }


    @Override
    public int getItemCount() {
        int size;
        if(childViewList.size()>0){
            return size = childViewList.size();
        }else return size = courseByTypes.size();
    }

    @Override
    public int getItemViewType(int position) {
        if(childViewList.size() != 0 && childViewList.size()>0){
            return TYPE_HEADER;
        }else return TYPE_ITEM;
    }


}

This childview adapter has two view types first is one image and 3 text and second view type contain one image and one text.When I pass values from fragment only first view type get displayed and second view type not gets value from fragment.

V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
  • Please remove unnecessary code. It is too difficult for a person to understand what you are looking for – suku Oct 10 '16 at 00:06
  • Actually I made classes,adapters for each layout.Please give me solution what I use to display the output As like I attached the image in question.It's urgent I tried lots of solution but didn't get – user6913818 Oct 10 '16 at 05:51
  • Correct me if I am wrong: 1. You want two separate types of views- assume v1 & v2 2. For v1 you want to use data of class c1 and v2 data of class c2. – suku Oct 10 '16 at 06:00
  • please give solution what I use to display output like that – user6913818 Oct 10 '16 at 06:00
  • yes sir. I use horizontal recyclerview inside vertical recyclerview.vertical for header title and horizontal for displaying child layout – user6913818 Oct 10 '16 at 06:02
  • ok but in adapter how to set them at different position in above code I tried same – user6913818 Oct 10 '16 at 06:03
  • Is the horizontal recyclerview inside vertical recyclerview working? Is it scrollable vertially and horizontally? – suku Oct 10 '16 at 06:05
  • Yes sir. vertical scroll vertically and hozrizontal recycler horizontally – user6913818 Oct 10 '16 at 06:06

1 Answers1

0

To show multiple different views in a recyclerview, you have to override getItemViewType() in the recyclerview adapter.

 //getItemViewType enables dynamic viewholder creation
@Override
public int getItemViewType(int position) {
    //you will need to add a integer with variable name viewTypeCode
    //for view1 set viewTypeCode = 100 and for view2 set viewTypeCode = 200
    viewTypeCode = itemList.get(position).getViewTypeCode();
    return viewTypeCode;
}

This is how the onCreateViewHolder will be different for multiple viewtypes. You will have to modify yours like this

@Override
public FeedViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    switch (viewType) {
        case 100: return new FeedViewHolder(layoutInflater.inflate(R.layout.v1, parent, false),100);
        case 200: return new FeedViewHolder(layoutInflater.inflate(R.layout.v2, parent, false),200);
    }
    return null;
}

OnBindViewHolder will be similarly modified

 @Override
public void onBindViewHolder(final FeedViewHolder holder, int position) {
     viewTypeCode = itemList.get(position).getViewTypeCode();
    switch ( viewTypeCode) {
        case 100:
           //your code for v1

        case 200:
            //your code for v2
    }
}

Similarly the ViewHolder class is modified

class FeedViewHolder extends RecyclerView.ViewHolder{
   //declare variables here
  public FeedViewHolder(View v, int viewType) {
        super(v);
        switch (viewType) {
        //instead of itemView.findViewById you will have to use v.findViewById
        case 100:
           //your code for v1

        case 200:
            //your code for v2
    }
}

For further reference refer to this SO answer

Don't pass two separate list.Make a custom class like this-

class MyClass {
   int viewTypeCode;
   CustomClass1 c1;
   CustomClass2 c2;

//add the setter getter
}

In your activity while preparing the data.

List<MyClass> itemList = new ArrayList<>();
//put whatever logic you need to make the order of the list
//if CustomClass1 object is put then setViewTypeCode(100), setCustomClass2 = null
//if CustomClass2 object is put then setViewTypeCode(200), setCustomClass1 = null

After data is built, then send this to the adapter.

Community
  • 1
  • 1
suku
  • 10,507
  • 16
  • 75
  • 120
  • For header view where I put condition in same adapter or not and How I pass data from each class that I made for two views – user6913818 Oct 10 '16 at 07:54
  • How to pass two lists with different data in recycle adapter – user6913818 Oct 10 '16 at 07:56
  • where I pass and set header title to view v1 and view v2 . – user6913818 Oct 10 '16 at 08:12
  • after fetching customClass1 object then how to set value using that object.there are 5 feilds in CustomClass1 and also setter and getter for each – user6913818 Oct 10 '16 at 09:33
  • i am get confused in setting data.How to do that – user6913818 Oct 10 '16 at 10:12
  • First create a customclass1 object c1... put all the values to it. Then `myClass.setCustomClass1(c1)` – suku Oct 10 '16 at 13:21
  • i add the values to both class – user6913818 Oct 10 '16 at 13:50
  • //if CustomClass1 object is put then setViewTypeCode(100), setCustomClass2 = null //if CustomClass2 object is put then setViewTypeCode(200), setCustomClass1 = null – user6913818 Oct 12 '16 at 07:20
  • how to do that with code.How to check customclass1 object is put and then set viewTypecode=100 and set customClass2=null – user6913818 Oct 12 '16 at 07:22
  • This will depend on how you are getting the data for the two classes...it would be better if you ask a separate SO question asking about how it should be structured...this will depend from case to case – suku Oct 12 '16 at 07:28
  • List itemList = new ArrayList<>(); MyClass myClass=new MyClass(); ChildView childView=new ChildView(" Paint Exhibition","" + "XYZ","09-10-2016","15-10-2016",covers[0]); myClass.setC1(childView); – user6913818 Oct 12 '16 at 07:32
  • in my fragment I declared like this – user6913818 Oct 12 '16 at 07:32
  • Yes...now after this add lines myClass.setViewTypeCode(100); myClass.setC2(null); itemList.add(myClass); – suku Oct 12 '16 at 07:35
  • http://stackoverflow.com/questions/39867392/how-to-add-multiple-child-in-recycler-i-have-2-different-view-types-but-my-seco Please sir response to this question also – user6913818 Oct 13 '16 at 05:00
  • If my answer helped, can you please mark it correct? – suku Oct 13 '16 at 06:05
  • where I add header layout ?In viewtype using position zero or any other option.I want to just change child layout header title layout should same for all child – user6913818 Oct 14 '16 at 05:07
  • Just want to change horizontal recyclerview (child)layout.Please help me – user6913818 Oct 14 '16 at 05:08
  • I am not able to understand what you are saying...your code would have changed significantly ... So can you ask a separate stackoverflow question – suku Oct 14 '16 at 05:43
  • http://android-pratap.blogspot.in/2015/12/horizontal-recyclerview-in-vertical.html – user6913818 Oct 14 '16 at 08:43
  • I want output like that but in above link child layout are same – user6913818 Oct 14 '16 at 08:45