package com.example.rankactivity;
import android.os.Bundle;
import android.widget.GridView;
import androidx.appcompat.app.AppCompatActivity;
import com.example.rankactivity.adapter.RankingAdapter;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 创建多个排行榜项
ArrayList<RankingItem> rankingItemsList = new ArrayList<>();
rankingItemsList.add(createRankingItem(R.drawable.text,
"Song Title 1", "Artist 1",
"Song Title 2", "Artist 2",
"Song Title 3", "Artist 3"));
rankingItemsList.add(createRankingItem(R.drawable.text,
"Song Title A", "Artist A",
"Song Title B", "Artist B",
"Song Title C", "Artist C"));
// 创建适配器并设置给GridView
RankingAdapter adapter = new RankingAdapter(this, rankingItemsList);
GridView gridView = findViewById(R.id.gridView);
gridView.setAdapter(adapter);
}
private RankingItem createRankingItem(int imageResource, String song1Title, String song1Artist,
String song2Title, String song2Artist,
String song3Title, String song3Artist) {
Song song1 = new Song(song1Title, song1Artist);
Song song2 = new Song(song2Title, song2Artist);
Song song3 = new Song(song3Title, song3Artist);
return new RankingItem(imageResource, song1, song2, song3);
}
}
package com.example.rankactivity.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.rankactivity.R;
import com.example.rankactivity.RankingItem;
import java.util.ArrayList;
public class RankingAdapter extends BaseAdapter {
private Context context;
private ArrayList<RankingItem> rankingItems;
public RankingAdapter(Context context, ArrayList<RankingItem> rankingItems) {
this.context = context;
this.rankingItems = rankingItems;
}
@Override
public int getCount() {
return rankingItems.size();
}
@Override
public Object getItem(int position) {
return rankingItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.item_ranking, parent, false);
}
// 获取当前项的数据
RankingItem rankingItem = rankingItems.get(position);
// 设置排行榜图片
ImageView rankingImage = convertView.findViewById(R.id.ranking_image);
rankingImage.setImageResource(rankingItem.getRankingImageResource());
// 设置歌曲信息
TextView song1Title = convertView.findViewById(R.id.song1_title);
song1Title.setText(rankingItem.getSong1().getTitle());
TextView song1Artist = convertView.findViewById(R.id.song1_artist);
song1Artist.setText(rankingItem.getSong1().getArtist());
TextView song2Title = convertView.findViewById(R.id.song2_title);
song2Title.setText(rankingItem.getSong2().getTitle());
TextView song2Artist = convertView.findViewById(R.id.song2_artist);
song2Artist.setText(rankingItem.getSong2().getArtist());
TextView song3Title = convertView.findViewById(R.id.song3_title);
song3Title.setText(rankingItem.getSong3().getTitle());
TextView song3Artist = convertView.findViewById(R.id.song3_artist);
song3Artist.setText(rankingItem.getSong3().getArtist());
// 设置背景
if (position % 2 == 0) {
convertView.setBackgroundResource(R.drawable.background_even); // 偶数项背景
} else {
convertView.setBackgroundResource(R.drawable.background_odd); // 奇数项背景
}
return convertView;
}
}
package com.example.rankactivity;
public class RankingItem {
private int rankingImageResource;
private Song song1;
private Song song2;
private Song song3;
public RankingItem(int rankingImageResource, Song song1, Song song2, Song song3) {
this.rankingImageResource = rankingImageResource;
this.song1 = song1;
this.song2 = song2;
this.song3 = song3;
}
public int getRankingImageResource() {
return rankingImageResource;
}
public Song getSong1() {
return song1;
}
public Song getSong2() {
return song2;
}
public Song getSong3() {
return song3;
}
}
package com.example.rankactivity;
public class Song {
private String title;
private String artist;
public Song(String title, String artist) {
this.title = title;
this.artist = artist;
}
public String getTitle() {
return title;
}
public String getArtist() {
return artist;
}
public void setTitle(String title) {
this.title = title;
}
public void setArtist(String artist) {
this.artist = artist;
}
}
<!-- activity_main.xml -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="2"
android:verticalSpacing="16dp"
android:horizontalSpacing="16dp"
android:padding="16dp"
android:stretchMode="columnWidth"
android:gravity="center"/>
</LinearLayout>
<!-- item_ranking.xml -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="16dp">
<!-- 排行榜图片 -->
<ImageView
android:id="@+id/ranking_image"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center_vertical"
android:scaleType="centerCrop"
android:src="@drawable/text"/>
<!-- 歌曲信息 -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:paddingStart="16dp">
<!-- 歌曲1 -->
<TextView
android:id="@+id/song1_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="歌曲1标题"
android:textStyle="bold"/>
<TextView
android:id="@+id/song1_artist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="歌手1"/>
<!-- 歌曲2 -->
<TextView
android:id="@+id/song2_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="歌曲2标题"
android:textStyle="bold"/>
<TextView
android:id="@+id/song2_artist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="歌手2"/>
<!-- 歌曲3 -->
<TextView
android:id="@+id/song3_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="歌曲3标题"
android:textStyle="bold"/>
<TextView
android:id="@+id/song3_artist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="歌手3"/>
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="rectangle">
<solid android:color="#ECEFF1" /> <!-- Set your desired background color for even items -->
</shape>
</item>
</layer-list>
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="rectangle">
<solid android:color="#FFFFFF" /> <!-- Set your desired background color for odd items -->
</shape>
</item>
</layer-list>