<!-- item_ranking_list.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<!-- 排行榜标题 -->
<TextView
android:id="@+id/tv_ranking_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="排行榜标题"
android:textSize="18sp"
android:textStyle="bold" />
<!-- Top 3 歌曲 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="8dp">
<TextView
android:id="@+id/tv_song_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="歌曲1" />
<TextView
android:id="@+id/tv_song_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="歌曲2" />
<TextView
android:id="@+id/tv_song_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="歌曲3" />
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:spanCount="2" />
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.helloproject;
import java.util.ArrayList;
import java.util.List;
public class RankingList {
private String title;
private List<String> topSongs;
public RankingList(String title) {
this.title = title;
this.topSongs = new ArrayList<>();
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public List<String> getTopSongs() {
return topSongs;
}
public void addTopSong(String song) {
topSongs.add(song);
}
public String getTopSong(int index) {
if (index >= 0 && index < topSongs.size()) {
return topSongs.get(index);
}
return "";
}
}
package com.example.helloproject;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import com.example.helloproject.adapter.RankingListAdapter;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private RankingListAdapter adapter;
private List<RankingList> rankingLists;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerView);
// 初始化排行榜数据
initializeRankingLists();
// 创建并设置适配器
adapter = new RankingListAdapter(rankingLists);
recyclerView.setAdapter(adapter);
// 设置GridLayoutManager,每行显示两个项目
GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
recyclerView.setLayoutManager(layoutManager);
}
private void initializeRankingLists() {
rankingLists = new ArrayList<>();
// 创建排行榜1
RankingList rankingList1 = new RankingList("排行榜1");
rankingList1.addTopSong("歌曲1-1");
rankingList1.addTopSong("歌曲1-2");
rankingList1.addTopSong("歌曲1-3");
rankingLists.add(rankingList1);
// 创建排行榜2
RankingList rankingList2 = new RankingList("排行榜2");
rankingList2.addTopSong("歌曲2-1");
rankingList2.addTopSong("歌曲2-2");
rankingList2.addTopSong("歌曲2-3");
rankingLists.add(rankingList2);
// 添加更多排行榜...
}
}
package com.example.helloproject.adapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.helloproject.R;
import com.example.helloproject.RankingList;
import java.util.List;
public class RankingListAdapter extends RecyclerView.Adapter<RankingListAdapter.RankingListViewHolder> {
private List<RankingList> rankingLists;
public RankingListAdapter(List<RankingList> rankingLists) {
this.rankingLists = rankingLists;
}
@NonNull
@Override
public RankingListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_ranking_list, parent, false);
return new RankingListViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RankingListViewHolder holder, int position) {
RankingList rankingList = rankingLists.get(position);
holder.tvRankingTitle.setText(rankingList.getTitle());
holder.tvSong1.setText(rankingList.getTopSong(0));
holder.tvSong2.setText(rankingList.getTopSong(1));
holder.tvSong3.setText(rankingList.getTopSong(2));
}
@Override
public int getItemCount() {
return rankingLists.size();
}
public static class RankingListViewHolder extends RecyclerView.ViewHolder {
TextView tvRankingTitle, tvSong1, tvSong2, tvSong3;
public RankingListViewHolder(@NonNull View itemView) {
super(itemView);
tvRankingTitle = itemView.findViewById(R.id.tv_ranking_title);
tvSong1 = itemView.findViewById(R.id.tv_song_1);
tvSong2 = itemView.findViewById(R.id.tv_song_2);
tvSong3 = itemView.findViewById(R.id.tv_song_3);
}
}
}