本篇實作adapter,共有2個adapter,可寫在同一個class中,也可實作在其他的class
首先實作item,分為待選擇清單中的item,以及已選擇清單中的item
<?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="50dp"
android:background="@drawable/brownline">
<HorizontalScrollView
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@drawable/brownline"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/guideline142"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/tv_music_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:gravity="center"
android:text=""
android:textColor="@color/black"
android:textStyle="bold" />
</HorizontalScrollView>
<Button
android:id="@+id/btn_music_item"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@drawable/red_right"
android:gravity="center"
android:textColor="@color/white"
android:textStyle="bold"
app:backgroundTint="@null"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline142"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline142"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.75" />
</androidx.constraintlayout.widget.ConstraintLayout>
<?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"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/brownline">
<HorizontalScrollView
android:id="@+id/horizontalScrollView"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@drawable/brownline"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/guideline139"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/tv_music_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text=""
android:textColor="@color/black"
android:textStyle="bold" />
</HorizontalScrollView>
<Button
android:id="@+id/btn_music_item"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@drawable/red_left"
android:gravity="center"
android:textColor="@color/white"
android:textStyle="bold"
app:backgroundTint="@null"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline139"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline139"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.75" />
</androidx.constraintlayout.widget.ConstraintLayout>
使用setAllMusicList()及setSelectMusicList()刷新介面
public class MusicListAdapter extends RecyclerView.Adapter<MusicListAdapter.MusicListViewHolder> {
@NonNull
@Override
public MusicListAdapter.MusicListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.musiclist_recyclerview_item, parent, false);
return new MusicListViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MusicListAdapter.MusicListViewHolder holder, int position) {
holder.tv_music_item.setText(String.valueOf(musicData.musicName[position]));
if(musicList.contains(position)){
holder.btn_music_item.setVisibility(View.INVISIBLE);
}else{
holder.btn_music_item.setVisibility(View.VISIBLE);
}
holder.btn_music_item.setOnClickListener(v ->{
musicList.add(position);
holder.btn_music_item.setEnabled(false);
setAllMusicList();
setSelectMusicList();
});
}
@Override
public int getItemCount() {
return musicData.musicName.length;
}
public class MusicListViewHolder extends RecyclerView.ViewHolder {
private TextView tv_music_item;
private Button btn_music_item;
public MusicListViewHolder(@NonNull View itemView) {
super(itemView);
tv_music_item = itemView.findViewById(R.id.tv_music_item);
btn_music_item = itemView.findViewById(R.id.btn_music_item);
}
}
}
public class SelectMusicListAdapter extends RecyclerView.Adapter<SelectMusicListAdapter.SelectMusicListViewHolder> {
@NonNull
@Override
public SelectMusicListAdapter.SelectMusicListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.select_musiclist_recyclerview_item, parent, false);
return new SelectMusicListAdapter.SelectMusicListViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull SelectMusicListAdapter.SelectMusicListViewHolder holder, int position) {
if (musicList.size() != 0 && musicList != null) {
holder.tv_music_item.setText(String.valueOf(musicData.musicName[(int) musicList.get(position)]));
holder.btn_music_item.setOnClickListener(v ->{
musicList.remove(position);
setAllMusicList();
setSelectMusicList();
});
}
}
@Override
public int getItemCount() {
return musicList.size();
}
public class SelectMusicListViewHolder extends RecyclerView.ViewHolder {
private TextView tv_music_item;
private Button btn_music_item;
public SelectMusicListViewHolder(@NonNull View itemView) {
super(itemView);
tv_music_item = itemView.findViewById(R.id.tv_music_item);
btn_music_item = itemView.findViewById(R.id.btn_music_item);
}
}
}
再來就是增加棋盤顏色及音樂到專案中,分為ColorData及MusicData
這裡的board1、2、3是整張棋盤的圖片,建議可使用像素圖生成網站繪製
public class ColorData {
public int[] light_color = {
R.drawable.lightbrownbackground_grayline,
R.drawable.lightgreenbackground_grayline,
R.drawable.lightbluebackground_grayline
};
public int[] dark_color = {
R.drawable.darkbrownbackground_grayline,
R.drawable.darkgreenbackground_grayline,
R.drawable.darkbluebackground_grayline
};
public int[] colorImage = {
R.drawable.board1,
R.drawable.board2,
R.drawable.board3
};
}
public class MusicData {
public int[] music = {
R.raw.spring, //四季小提琴協奏曲<春>
R.raw.summer, //四季小提琴協奏曲<夏>
R.raw.autumn, //四季小提琴協奏曲<秋>
R.raw.winter //四季小提琴協奏曲<冬>
};
public String[] musicName = {
"四季小提琴協奏曲<春>",
"四季小提琴協奏曲<夏>",
"四季小提琴協奏曲<秋>",
"四季小提琴協奏曲<冬>"
};
}
值得注意的是,我們需要自行增添.mp3檔在raw資料夾中,才能抓取到音訊資料
這樣就可以正常選擇音樂及顏色並傳值到其他頁面了
成果: