Android 如何使RecyclerView显示两个不同的片段
我正在和一群大学生朋友开发一个android应用程序,我们正在学习android开发,所以如果这个问题真的很简单,我们都很抱歉 在我们应用程序的其中一个部分中,我们需要显示一张卡片(其中包含用户的数据),然后在de Card下面显示一个相同类型的项目列表(大小可变)。以前,当我们需要创建一个列表时,我们使用了一个带有自定义适配器的recycler视图,这对于只包含一种类型的项目的列表非常有效,但是现在我们需要recyclerView来承载卡和项目列表,我们不知道如何做 现在,我们已经将卡片设置为静态,并且只将rcycler视图应用于卡片下的项目列表,这意味着只有列表是可滚动的。我们需要使整个屏幕可滚动 那么,我们如何才能做到这一点呢?我的直觉告诉我,我们需要将顶部的卡转换成一个片段,然后对自定义适配器进行一些更改,以便它可以处理两个片段(卡和卡下面的列表项)。recyclerView是解决我们问题的正确答案吗 希望您能帮助我们,以下是我们目前的代码: 教师档案片段Android 如何使RecyclerView显示两个不同的片段,android,android-fragments,android-recyclerview,Android,Android Fragments,Android Recyclerview,我正在和一群大学生朋友开发一个android应用程序,我们正在学习android开发,所以如果这个问题真的很简单,我们都很抱歉 在我们应用程序的其中一个部分中,我们需要显示一张卡片(其中包含用户的数据),然后在de Card下面显示一个相同类型的项目列表(大小可变)。以前,当我们需要创建一个列表时,我们使用了一个带有自定义适配器的recycler视图,这对于只包含一种类型的项目的列表非常有效,但是现在我们需要recyclerView来承载卡和项目列表,我们不知道如何做 现在,我们已经将卡片设置为
公共类TeacherProfileFragment扩展了FragmentsMethods,ItemAdapterListener{
私有文本视图教师姓名;
私人图像查看教师图像;
私人回收站查看回收站的评级;
私人回收服务查看回收服务教师课程;
专用评级列表适配器评级列表适配器;
私人课程适应者课程适应者;
私有ImageView教师卡背景;
公共教师档案片段(){
}
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
视图=充气机。充气(常量。碎片\u外形\u布局,容器,错误);
设置元素(视图);
addListeners();
返回视图;
}
@凌驾
公共空心设置图元(视图){
teacherCardBackground=(ImageView)view.findViewById(R.id.teacher\u card\u background);
Picasso.with(getActivity()).load(“https://newevolutiondesigns.com/images/freebies/google-material-design-wallpaper-17.jpg).fit().centerCrop()插入(教师卡背景);
//位图位图=((BitmapDrawable)teacherCardBackground.getDrawable()).getBitmap();
//palete p=palete.from(位图).generate();
//TeacherCardBackgroundColor(p.getVibrantColor(0x0000000));
teacherName=(TextView)view.findViewById(R.id.profile\u teacher\u mame);
setext(Storage.getSingelton().getInfo(这个,Storage.KEY\u TEACHER\u NAME));
teacherImage=(ImageView)view.findViewById(R.id.profile\u teacher\u image);
毕加索.with(getActivity()).load(Storage.getSingelton().getInfo(这是Storage.KEY\u TEACHER\u IMAGE)).transform(new circlettransform()).into(teacherImage);
recyclerViewTeacherCourses=(RecyclerView)view.findViewById(R.id.course\u列表);
//recyclerviewTeacherRatings=(RecyclerView)view.findViewById(R.id.rating_列表);
courseListAdapter=新的courseListAdapter(getActivity(),getData(“”);
courseListAdapter.setListener(this);
ratingListAdapter=新的ratingListAdapter(getActivity(),getStaticData());
//RecycleServiceWTeacherRatings.setAdapter(ratingListAdapter);
//setLayoutManager(新的LinearLayoutManager(getActivity());
设置适配器(CourseStatAdapter);
setLayoutManager(新的LinearLayoutManager(getActivity());
}
@凌驾
public void addListeners(){
}
@凌驾
已单击公共无效项(视图,课程对象){
Storage.getSingelton().Storage(对象,this);
Redirect.getSingelton().showFragment(这个,常量.TEACHER\u容器,常量.FRAGMENT\u TEACHER\u课程);
}
公共列表getStaticData(){
列表评级=新的ArrayList();
增加(新评级((浮动)4.0,“tecnica”);
增加(新评级((浮动)2.5,“salud”);
增加(新评级((浮动)1.3,“合并”);
增加(新评级((浮动)3.0,“desarrollo”);
回报率;
}
@凌驾
公共列表getData(字符串数据){
列出课程=新建ArrayList();
课程。添加(新课程(“Matematica”(浮动)4.0,“FISI”);
课程。添加(新课程(“Fisica”),(浮动)4.0,“FISI”);
课程。添加(新课程(“Matematica”(浮动)4.0,“FISI”);
课程。添加(新课程(“Matematica”(浮动)4.0,“FISI”);
返回课程;
}
}
CourseListAdapter
公共类CourseListAdapter扩展了RecyclerView.Adapter{
private Listcourses=Collections.emptyList();
私有项适配器列表器项适配器列表器;
私人停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场;
公共课程ListAdapter(上下文,列出课程){
layoutInflater=layoutInflater.from(上下文);
这个。课程=课程;
}
@凌驾
公共课程文件夹onCreateViewHolder(视图组父级,int-viewType){
视图=布局更平坦。充气(常数。航向\新建\项,父项,false);
CourseHolder CourseHolder=新CourseHolder(视图);
复课教师;
}
@凌驾
BindViewHolder上的公共无效(CourseHolder holder,内部位置){
Course=courses.get(位置);
持有者:设定要素(课程);
}
@凌驾
public int getItemCount(){
返回课程。大小();
}
公共void setListener(ItemAdapterListener侦听器){
this.itemAdapterListener=侦听器;
}
公共类CourseHolder扩展了RecyclerView.ViewHolder实现了ViewHolderSettings、View.OnClickListener{
私有文本视图课程名称;
普里瓦
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;
}
}
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());
}
}
}
<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>