如何在Android Studio上显示多个RecyclerView?

huangapple go评论74阅读模式
英文:

How to display multiple recycler views on Android Studio?

问题

非常新手对于Android应用开发。我不确定是否在提出正确的问题,但是以下是我的情况。我有一个简单的成绩单应用,外观如下:

(图片链接被屏蔽,无法显示)

我的目标是在应用中显示多个课程。我正在使用RecyclerView来实现这一点,使用我的活动中的以下onCreate函数将课程信息传递给适配器:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_grade);

    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowTitleEnabled(false);

    for(int i = 0; i<2; i++)
    {
        Course course = Course.generateRandomCourse();

        recyclerView = (RecyclerView) findViewById(R.id.togglegrades);
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        mAdapter = new RecyclerViewAdapter(course);
        recyclerView.setAdapter(mAdapter);
    }
}

我的循环目标是在应用中显示两门课程,但事实并非如此。我不确定是否需要重新设计我的Recycling Adapter以接受课程列表,或者是否有一种简单的方法来显示多个课程。

非常感谢任何帮助!感谢您的时间 如何在Android Studio上显示多个RecyclerView?

编辑:在XML中添加了2个可循环视图并尝试同时调用两者后,这是我使用的代码:

Course course = Course.generateRandomCourse();
Course course1 = Course.generateRandomCourse();

recyclerView = (RecyclerView) findViewById(R.id.togglegrades);
recyclerView1 = (RecyclerView) findViewById(R.id.togglegrades1);

layoutManager = new LinearLayoutManager(this);
layoutManager1 = new LinearLayoutManager(this);

recyclerView.setLayoutManager(layoutManager);
recyclerView1.setLayoutManager(layoutManager1);

mAdapter = new RecyclerViewAdapter(course);
mAdapter1 = new RecyclerViewAdapter(course1);

recyclerView.setAdapter(mAdapter);
recyclerView1.setAdapter(mAdapter1);

现在输出只是重叠在一起。

(图片链接被屏蔽,无法显示)

编辑2:下面包括完整的适配器代码。

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
{
    private static final int TYPE_HEADER = 0;
    private static final int TYPE_ITEM = 1;

    private List<Course> courses = new ArrayList<>();
    private ArrayList<Assignment> assignments;
    private String course;
    private String average;

    public RecyclerViewAdapter(List<Course> courses)
    {
        for(Course c : courses)
        {
            course = c.getCourseTitle();
            assignments = c.getAssignments();
            if(assignments.size()==0)
            {
                average = "0";
            }
            else
            {
                Integer grade_total = new Integer(0);
                Integer assignment_total = new Integer(0);
                for (Assignment i : assignments)
                {
                    grade_total += i.getAssignmentGrade();
                    assignment_total++;
                }
                average = Integer.toString(grade_total /assignment_total);
            }
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType)
    {
        if(viewType == TYPE_HEADER)
        {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.header_course,parent,false);
            return new ViewHolderHeader(v);
        }
        else if(viewType == TYPE_ITEM)
        {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_course, parent, false);
            return new ViewHolderItem(v);
        }
        else return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {
        if(holder instanceof ViewHolderHeader)
        {
            ViewHolderHeader VHHeader = (ViewHolderHeader)holder;
            VHHeader.title.setText(course);
            VHHeader.average.setText("Average: " + average + "%");
        }
        else if(holder instanceof ViewHolderItem)
        {
            String assignemnt_name = assignments.get(position-1).getAssignmentTitle();
            String assignment_grade = Integer.toString(assignments.get(position-1).getAssignmentGrade());
            ViewHolderItem VHItem = (ViewHolderItem)holder;
            VHItem.name.setText(assignemnt_name);
            VHItem.grade.setText(assignment_grade + "%");
        }
    }

    @Override
    public int getItemCount()
    {
        return assignments.size() + 1;
    }

    private boolean isPositionHeader(int position)
    {
        if(position == 0)
            return true;
        else
            return false;
    }

    @Override
    public int getItemViewType(int position)
    {
        if(isPositionHeader(position))
            return TYPE_HEADER;
        return TYPE_ITEM;
    }

    public class ViewHolderHeader extends RecyclerView.ViewHolder
    {
        TextView title;
        TextView average;
        public ViewHolderHeader(View view)
        {
            super(view);
            this.title = (TextView)itemView.findViewById(R.id.course);
            this.average = (TextView)itemView.findViewById(R.id.average);
        }
    }

    public class ViewHolderItem extends RecyclerView.ViewHolder
    {
        TextView name;
        TextView grade;
        public ViewHolderItem(View view)
        {
            super(view);
            this.name = (TextView)itemView.findViewById(R.id.assignment);
            this.grade = (TextView)itemView.findViewById(R.id.grade);
        }
    }
}

(图片链接被屏蔽,无法显示)

我已经根据你提供的内容完成了翻译,请查看上面的翻译结果。如果你还有其他问题或需要进一步帮助,请随时提问。

英文:

very new to Android App development here. I'm not sure if I'm asking the right question, but here's my situation. I have a simple gradebook app that looks like the following:

如何在Android Studio上显示多个RecyclerView?

My goal is to display multiple courses on the app. I am using the RecyclerView to implement this, using the following onCreate function in my activity to pass the course information to the adapter:

    @Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grade);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
for(int i = 0; i&lt;2; i++)
{
Course course = Course.generateRandomCourse();
recyclerView = (RecyclerView) findViewById(R.id.togglegrades);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
mAdapter = new RecyclerViewAdapter(course);
recyclerView.setAdapter(mAdapter);
}

My goal for the for loop was to have two courses show up, but this is not the case. I'm not sure whether I need to rework my Recycling Adapter to take a list of courses, or if there is a simple way to display multiple courses.

Any help is much appreciated! Thank you for your time 如何在Android Studio上显示多个RecyclerView?

EDIT: After adding 2 recyclers in XML and trying to invoke both, this is the code I used:

        Course course = Course.generateRandomCourse();
Course course1 = Course.generateRandomCourse();
recyclerView = (RecyclerView) findViewById(R.id.togglegrades);
recyclerView1 = (RecyclerView) findViewById(R.id.togglegrades1);
layoutManager = new LinearLayoutManager(this);
layoutManager1 = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView1.setLayoutManager(layoutManager1);
mAdapter = new RecyclerViewAdapter(course);
mAdapter1 = new RecyclerViewAdapter(course1);
recyclerView.setAdapter(mAdapter);
recyclerView1.setAdapter(mAdapter1);

Now the output is just overlapped.

如何在Android Studio上显示多个RecyclerView?

EDIT 2: Including full adapter code below.

public class RecyclerViewAdapter extends RecyclerView.Adapter&lt;RecyclerView.ViewHolder&gt;
{
private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;
private List&lt;Course&gt; courses = new ArrayList&lt;&gt;();
private ArrayList&lt;Assignment&gt; assignments;
private String course;
private String average;
public RecyclerViewAdapter(List&lt;Course&gt; courses)
{
for(Course c : courses)
{
course = c.getCourseTitle();
assignments = c.getAssignments();
if(assignments.size()==0)
{
average = &quot;0&quot;;
}
else
{
Integer grade_total = new Integer(0);
Integer assignment_total = new Integer(0);
for (Assignment i : assignments)
{
grade_total += i.getAssignmentGrade();
assignment_total++;
}
average = Integer.toString(grade_total /assignment_total);
}
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType)
{
if(viewType == TYPE_HEADER)
{
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.header_course,parent,false);
return new ViewHolderHeader(v);
}
else if(viewType == TYPE_ITEM)
{
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_course, parent, false);
return new ViewHolderItem(v);
}
else return null;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
if(holder instanceof ViewHolderHeader)
{
ViewHolderHeader VHHeader = (ViewHolderHeader)holder;
VHHeader.title.setText(course);
VHHeader.average.setText(&quot;Average: &quot; + average + &quot;%&quot;);
}
else if(holder instanceof ViewHolderItem)
{
String assignemnt_name = assignments.get(position-1).getAssignmentTitle();
String assignment_grade = Integer.toString(assignments.get(position-1).getAssignmentGrade());
ViewHolderItem VHItem = (ViewHolderItem)holder;
VHItem.name.setText(assignemnt_name);
VHItem.grade.setText(assignment_grade + &quot;%&quot;);
}
}
@Override
public int getItemCount()
{
return assignments.size() + 1;
}
private boolean isPositionHeader(int position)
{
if(position == 0)
return true;
else
return false;
}
@Override
public int getItemViewType(int position)
{
if(isPositionHeader(position))
return TYPE_HEADER;
return TYPE_ITEM;
}
public class ViewHolderHeader extends RecyclerView.ViewHolder
{
TextView title;
TextView average;
public ViewHolderHeader(View view)
{
super(view);
this.title = (TextView)itemView.findViewById(R.id.course);
this.average = (TextView)itemView.findViewById(R.id.average);
}
}
public class ViewHolderItem extends RecyclerView.ViewHolder
{
TextView name;
TextView grade;
public ViewHolderItem(View view)
{
super(view);
this.name = (TextView)itemView.findViewById(R.id.assignment);
this.grade = (TextView)itemView.findViewById(R.id.grade);
}
}
}

答案1

得分: 1

你需要在你的activity_grade xml布局中有两个RecycleView,并且对每个循环内的内容执行相同的操作。

你当前的循环正在执行的操作是创建一个随机课程,然后将该课程设置到名为togglegradesRecyclerView中,然后在第二次迭代中,它再次在同一个RecyclerView上执行相同的操作。

所以,在xml布局中添加第二个RecyclerView后,我会用类似这样的代码替换循环部分:

recyclerView1 = (RecyclerView) findViewById(R.id.togglegrades);
recyclerView2 = (RecyclerView) findViewById(R.id.togglegrades2);

layoutManager = new LinearLayoutManager(this);

recyclerView1.setLayoutManager(layoutManager);
recyclerView2.setLayoutManager(layoutManager);

mAdapter = new RecyclerViewAdapter(course);
// 可以在这里创建不同的课程,然后将它设置给一个新的适配器变量
recyclerView1.setAdapter(mAdapter);
recyclerView2.setAdapter(mAdapter);
英文:

You need to have 2 RecycleViews in your activity_grade xml layout, and do what you're doing inside the for loops for each.
What your for loop is currently doing, is creating a random course, and then setting that course to the RecyclerView called togglegrades, then on the second iteration, it does the same thing again, on the same RecyclerView

So what I would do after adding a second RecyclerView in the xml layout, is replace the for loop with something like this:

recyclerView1 = (RecyclerView) findViewById(R.id.togglegrades);
recyclerView2 = (RecyclerView) findViewById(R.id.togglegrades2);
layoutManager = new LinearLayoutManager(this);
recyclerView1.setLayoutManager(layoutManager);
recyclerView2.setLayoutManager(layoutManager);
mAdapter = new RecyclerViewAdapter(course);
// Maybe create a different course here and set it to a new adapter variable
recyclerView.setAdapter(mAdapter);
recyclerView2.setAdapter(mAdapter);

答案2

得分: 1

你可以以非常优化的方式完成它。请查看名为《Recyclerview多视图类型》的教程1。在这里,您无需创建多个RecyclerView,可以使用单个RecyclerView来处理多种类型的数据。

英文:

You can do it in a very optimal way. Please see the tutorial titled Recyclerview multiple view type. Here you don't need to create multiple recycler views and you can use a single recycler view for multiple types of data.

答案3

得分: 1

为什么使用了两个RecyclerView?

你的布局看起来很简单,只需使用一个带有两种视图类型的RecyclerView。

这是一个示例

英文:

Why do you use two RecyclerView?

Your Layout looks simple, just use one recyclerView with two viewType.

This is a sample

答案4

得分: 1

你不需要使用多个RecyclerView。

只需查看您的代码,您会找到为什么代码无法正常工作的答案。

RecyclerView类似于ListView,您需要传递一个列表。但是在您的代码中,您只传递了一个对象。

您不应该将RecyclerView放在for循环内部,因为RecyclerView的目的是存储列表,而不是一个对象。

因此,您应该将List传递给RecyclerView。

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_grade);

    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowTitleEnabled(false);

    recyclerView = (RecyclerView) findViewById(R.id.togglegrades);
    layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);

    List<Course> listCourse = new ArrayList<>();

    for (int i = 0; i < 2; i++)
    {
        listCourse.add(Course.generateRandomCourse());
    }

    mAdapter = new RecyclerViewAdapter(listCourse);
    recyclerView.setAdapter(mAdapter);
}

在适配器中,您可以使用这个课程列表。

要了解更多信息,您可以查看此链接,了解如何使用RecyclerView和Adapter:RecyclerView with Adapter

在您的适配器中,请查看以下代码:

private boolean isPositionHeader(int position)
{
    if(position == 0)
        return true;
    else
        return false;
}

// ...

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
    Course c = courses.get(position);
    // ... 这里是您的代码 ...

    ViewHolderHeader VHHeader = (ViewHolderHeader) holder;
    VHHeader.title.setText(course);
    VHHeader.average.setText("Average: " + average + "%");

    // 在这里,您可以在适配器中设置一个新的RecyclerView来显示assignments
}

// ...

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
{
    private static final int TYPE_HEADER = 0;
    private static final int TYPE_ITEM = 1;

    private List<Course> courses = new ArrayList<>();

    public RecyclerViewAdapter(List<Course> courses)
    {
        this.courses = courses;
    }

    // ... 其他方法 ...

    public class ViewHolderHeader extends RecyclerView.ViewHolder
    {
        // ... 这里是您的代码 ...
    }
}

注意:上述代码片段中的某些部分可能需要根据您的实际情况进行调整,特别是在处理布局和适配器逻辑方面。

英文:

You do not need to use multiple RecyclerView.

Just look at your code and you will find the answer why your code is not working.

RecyclerView is similar like listview where you have to pass a list. But in your code you are passing just 1 object of course.

You must not add recyclerview inside for loop, as recyclerview purpose is to store list,not 1 object.

So you should pass List<Course> to the recyclerview.

 @Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grade);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
recyclerView = (RecyclerView) findViewById(R.id.togglegrades);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
List&lt;Course&gt; listCourse=new ArrayList();
for(int i = 0; i&lt;2; i++)
{
listCourse.add(Course.generateRandomCourse());
}
mAdapter = new RecyclerViewAdapter(listCourse);
recyclerView.setAdapter(mAdapter);
}

And inside the adapter you can use this list of course.

For more reference you can check this link, on how to use RecyclerView with Adapter

EDIT: In your adapter look at this code:

 private boolean isPositionHeader(int position)
{
if(position == 0)
return true;
else
return false;
}

Here position == 0 is static code, which will not work for dynamic code.If your list contains 1 Header and 1 Item, than you can change simply position%2 == 0 condition. else you have to create other recyclerview inside adapter.

Check out below code and comments I have added , at the end of onBindViewHolder you can add new recyclerview, for that you have to add recyclerview in your layout also.

Checkout how you can use List&lt;Course&gt; courses in your constructor and onBindViewHolder.

public class RecyclerViewAdapter extends RecyclerView.Adapter&lt;RecyclerView.ViewHolder&gt;{
private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;
private List&lt;Course&gt; courses = new ArrayList&lt;&gt;();
public RecyclerViewAdapter(List&lt;Course&gt; courses)
{
this.courses = courses;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType)
{
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.header_course,parent,false);
return new ViewHolderHeader(v);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
Course c=courses.get(position);
String course = c.getCourseTitle();
String assignments = c.getAssignments();
if(assignments.size()==0)
{
average = &quot;0&quot;;
}
else
{
Integer grade_total = new Integer(0);
Integer assignment_total = new Integer(0);
for (Assignment i : assignments)
{
grade_total += i.getAssignmentGrade();
assignment_total++;
}
average = Integer.toString(grade_total /assignment_total);
}
ViewHolderHeader VHHeader = (ViewHolderHeader)holder;
VHHeader.title.setText(course);
VHHeader.average.setText(&quot;Average: &quot; + average + &quot;%&quot;);
//Here you can set new recyclerview in your adapter for assignments
}
@Override
public int getItemCount()
{
return courses.size();
}
/*private boolean isPositionHeader(int position)
{
//Change logic here as per your list, because you have list not single object. 
//if(position == 0)
//    return true;
//else
//    return false;
}*/
//Remove this code as you have not different views in your list
/*@Override
public int getItemViewType(int position)
{
if(isPositionHeader(position))
return TYPE_HEADER;
return TYPE_ITEM;
}*/
public class ViewHolderHeader extends RecyclerView.ViewHolder
{
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.header_course,parent,false);
return new ViewHolderHeader(v);
}
}

huangapple
  • 本文由 发表于 2020年9月18日 10:07:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63948360.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定