0

I'm developing an android app with a group of colleage friends , we are learing android development as we go so if this question is really easy , we are all sorry .

In one of the sections of our apps , we need to show a Card (that contains data about a user ) and then , beneath de Card , a list (variable in size ) of items all of the same type . Previously , when we needed to do a list we used a recycler view with a custom adapter , and that worked perfectly with a list consisting of just one type of item , but now that we need the recyclerView to host both the Card and the list of items we don't know how to do it .

For now , we have made the Card static and only apply the rcycler view to the list of items beneath the Card , and that means that only the list is scrollable . We need to make the whole screen scrollable .

So , how can we achieve this? My gut tells me that we would need to turn that card at the top into a fragment and then make some changes to the our custom adapter so that it can handle both tipes of fragments (the card and the item of the list that goes beneath the card) . Is recyclerView the right answer to our problems?

Hope you can help us , here is our code so far :

TeacherProfileFragment

    public class TeacherProfileFragment extends Fragment implements FragmentsMethods,ItemAdapterListener<Course>{

    private TextView teacherName;
    private ImageView teacherImage;
    private RecyclerView recyclerviewTeacherRatings;
    private RecyclerView recyclerViewTeacherCourses;
    private RatingListAdapter ratingListAdapter;
    private CourseListAdapter courseListAdapter;
    private ImageView teacherCardBackground;

    public TeacherProfileFragment(){

    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(Constants.FRAGMENT_PROFILE_TEACHER_LAYOUT, container, false);
        setUpElements(view);
        addListeners();
        return view;
    }

    @Override
    public void setUpElements(View view) {

        teacherCardBackground= (ImageView) view.findViewById(R.id.teacher_card_background);
        Picasso.with(getActivity()).load("https://newevolutiondesigns.com/images/freebies/google-material-design-wallpaper-17.jpg").fit().centerCrop().into(teacherCardBackground);
        //Bitmap bitmap = ((BitmapDrawable)teacherCardBackground.getDrawable()).getBitmap();
        //Palette p = Palette.from(bitmap).generate();
        //teacherCardBackground.setBackgroundColor(p.getVibrantColor(0x0000000));
        teacherName=(TextView)view.findViewById(R.id.profile_teacher_mame);
        teacherName.setText(Storage.getSingelton().getInfo(this,Storage.KEY_TEACHER_NAME));
        teacherImage=(ImageView)view.findViewById(R.id.profile_teacher_image);
        Picasso.with(getActivity()).load(Storage.getSingelton().getInfo(this,Storage.KEY_TEACHER_IMAGE)).transform(new CircleTransform()).into(teacherImage);
        recyclerViewTeacherCourses=(RecyclerView)view.findViewById(R.id.course_list);
        //recyclerviewTeacherRatings=(RecyclerView)view.findViewById(R.id.rating_list);
        courseListAdapter=new CourseListAdapter(getActivity(),getData(""));
        courseListAdapter.setListener(this);
        ratingListAdapter=new RatingListAdapter(getActivity(),getStaticData());
        //recyclerviewTeacherRatings.setAdapter(ratingListAdapter);
        //recyclerviewTeacherRatings.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerViewTeacherCourses.setAdapter(courseListAdapter);
        recyclerViewTeacherCourses.setLayoutManager(new LinearLayoutManager(getActivity()));
    }

    @Override
    public void addListeners() {

    }

    @Override
    public void itemClicked(View view, Course object) {
        Storage.getSingelton().storage(object,this);
        Redirect.getSingelton().showFragment(this,Constants.TEACHER_CONTAINER,Constants.FRAGMENT_TEACHER_COURSE);
    }

    public List<Rating> getStaticData(){
        List<Rating> ratings=new ArrayList<>();
        ratings.add(new Rating((float) 4.0,"tecnica"));
        ratings.add(new Rating((float) 2.5,"salud"));
        ratings.add(new Rating((float) 1.3,"conmosion"));
        ratings.add(new Rating((float) 3.0,"desarrollo"));
        return ratings;
    }

    @Override
    public List<Course> getData(String data) {
        List<Course> courses=new ArrayList<>();
        courses.add(new Course("Matematica ", (float) 4.0,"FISI"));
        courses.add(new Course("Fisica", (float) 4.0,"FISI"));
        courses.add(new Course("Matematica", (float) 4.0,"FISI"));
        courses.add(new Course("Matematica", (float) 4.0,"FISI"));
        return courses;
    }
}

CourseListAdapter

    public class CourseListAdapter extends RecyclerView.Adapter<CourseListAdapter.CourseHolder>{
    private List<Course>courses= Collections.emptyList();
    private ItemAdapterListener itemAdapterListener;
    private LayoutInflater layoutInflater;
    public CourseListAdapter(Context context,List<Course> courses){
        layoutInflater=LayoutInflater.from(context);
        this.courses=courses;

    }
    @Override
    public CourseHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=layoutInflater.inflate(Constants.COURSE_NEW_ITEM,parent,false);
        CourseHolder courseHolder=new CourseHolder(view);
        return courseHolder;
    }

    @Override
    public void onBindViewHolder(CourseHolder holder, int position) {
        Course course=courses.get(position);
        holder.setElements(course);
    }

    @Override
    public int getItemCount() {
        return courses.size();
    }
    public void setListener(ItemAdapterListener listener){
        this.itemAdapterListener =listener;
    }

    public class CourseHolder extends RecyclerView.ViewHolder implements ViewHolderSetters<Course>,View.OnClickListener{
        private TextView courseName;
        private TextView courseFaculty;
        private RatingBar courseRating;
        private TextView courseMark;
        private View vista;
        private Course current;
        private ImageView initialLetterImage;
        public CourseHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
            courseName= (TextView) itemView.findViewById(R.id.profile_course_course_name);
            courseFaculty=(TextView) itemView.findViewById(R.id.course_faculty);
            initialLetterImage=(ImageView) itemView.findViewById(R.id.letterImageBackground);
            //courseRating=(RatingBar) itemView.findViewById(R.id.course_rating);
            //courseMark=(TextView) itemView.findViewById(R.id.course_mark);
            vista=itemView;
        }

        @Override
        public void onClick(View v) {
            itemAdapterListener.itemClicked(v,current);
        }

        @Override
        public void setElements(Course elements) {
            current=elements;
            courseName.setText(elements.getName());
            courseFaculty.setText(elements.getFaculty());
            ColorGenerator generator = ColorGenerator.MATERIAL;
            int color = generator.getColor(courseName.getText().charAt(0));
            TextDrawable.IBuilder builder = TextDrawable.builder().round();
            TextDrawable textDrawable = builder.build(courseName.getText().toString().charAt(0)+"", color);
            initialLetterImage.setImageDrawable(textDrawable);
            //courseMark.setText(elements.getRating()+"");
            //courseRating.setRating(elements.getRating());
        }
    }
}

FrangmentTeacherProfile

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment_teacher_profile">

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view_teacher_profile"
    android:layout_width="match_parent"
    android:layout_height="280dp"
    card_view:cardCornerRadius="0dp"
    card_view:cardElevation="5dp"
    card_view:cardPreventCornerOverlap="true"
    card_view:cardUseCompatPadding="true">

    <ImageView
        android:id="@+id/teacher_card_background"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center">

        <ImageView
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_marginBottom="5dp"
            android:id="@+id/profile_teacher_image"
            android:layout_centerHorizontal="true"
            />
        <!--style="@style/ProfileTeacherItemImage" -->
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/profile_teacher_mame"
            android:layout_below="@id/profile_teacher_image"
            android:textColor="@android:color/white"
            android:textSize="25dp"
            android:text="Frank Escobedo Bailon"
            android:gravity="center"
            android:maxLines="2"/>
        <!--style="@style/ProfileTeacherItemName"-->

        <RelativeLayout
            android:id="@+id/profile_rating_container"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_below="@id/profile_teacher_mame">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/profile_teacher_rating"
                android:textColor="@android:color/white"
                android:textSize="20dp"
                android:text="4.5"
                android:gravity="center"
                android:maxLines="1"
                android:layout_centerHorizontal="true"/>
            <ImageView
                android:id="@+id/teacher_profile_star_drawable"
                android:layout_width="wrap_content"
                android:layout_height="20dp"
                android:layout_toRightOf="@id/profile_teacher_rating"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="5dp"
                android:src="@mipmap/ic_grade_white_24dp"/>
        </RelativeLayout>


    </RelativeLayout>


</android.support.v7.widget.CardView>


<TextView
    android:id="@+id/profile_courses_label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/card_view_teacher_profile"
    android:layout_margin="5dp"
    android:textSize="15dp"
    android:text="Cursos Que Dicta"/>
<View
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:background="@color/eventDetailDividerColor"
    android:layout_toRightOf="@id/profile_courses_label"
    android:layout_below="@id/card_view_teacher_profile"
    android:layout_marginTop="15dp"/>


<android.support.v7.widget.RecyclerView
    android:id="@+id/course_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/profile_courses_label"
    style="@style/ProfileTeacherItemCourse">
</android.support.v7.widget.RecyclerView>

Francisco M.
  • 99
  • 1
  • 2
  • 9

1 Answers1

0

Before the recycler view (e.g. list view), there was an easy way to achieve this:

Just add the card view as header to the list.

With a recycler view you can't simply call addHeaderView(), but you can declare two view types inside your adapter for the recycler view.

With this link you should be able to do this :

Is there an addHeaderView equivalent for RecyclerView?

This way, you just need two layout files (one for the card view and one for the list items) and then you can inflate both layouts inside your adapter.

If you have further questions just ask!

Community
  • 1
  • 1
Oliver
  • 503
  • 1
  • 3
  • 13