今天要來延續昨天的部分,首先來修改一下card_item的地方。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="120dp"
android:layout_height="120dp"
android:padding="5dp">
<!--bg05與footprint04為牌的背面,腳印的部分可以改成其他的圖示。-->
<ImageView
android:id="@+id/photo_stroke"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg05" />
<ImageView
android:layout_centerInParent="true"
android:layout_width="70dp"
android:layout_height="60dp"
android:background="@drawable/footprint04" />
<!--下方有設置隱藏的屬性,在觸發翻轉的動作後會顯示之後再recyclerView中設置的圖片,並依照先前生成的數列來判斷要顯示的圖片為何。-->
<ImageView
android:layout_margin="3dp"
android:id="@+id/photo_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible" />
<!--邊框。-->
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg06" />
<!--未使用,用來標示元件位置-->
<TextView
android:id="@+id/info_text"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentEnd="true"
android:layout_margin="5dp"
android:background="@drawable/round07"
android:gravity="center"
android:textColor="#ffffff"
android:visibility="gone"
android:layout_alignParentRight="true" />
</RelativeLayout>
接下來是recyclerViewAdapter的部分,imageResource的地方為drawable中的圖示,用來後續顯示的圖示,在mData的部分為前面的數列,這裡會判斷數值為何,為1的話就會將該item位置的圖示設置為imageResource[0],因為前面只會產生1~7的數值,所以這個部分就要再-1才會符合imageResource的內容。
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {
private String[] mData = new String[0];
private LayoutInflater mInflater;
private ItemClickListener mClickListener;
private Integer[] imageResource = {R.drawable.red_card, R.drawable.orange_card, R.drawable.yellow_card,
R.drawable.green_card, R.drawable.blue_card, R.drawable.indigo_card, R.drawable.purple_card};
private int checkEvent;
private int score;
private int selectCard;
private ArrayList<ImageView> ivNumberList;
private ArrayList<Integer> selectList;
private ArrayList<View> itemViewList;
private Handler handler;
private ArrayList<String> mImgRandomSelect;
//選取狀態
private int[] mSelectRegister;
MyRecyclerViewAdapter(Context context, String[] data, int[] selectRegister) {
handler = new Handler();
selectList = new ArrayList<>();
ivNumberList = new ArrayList<>();
itemViewList = new ArrayList<>();
mImgRandomSelect = new ArrayList<>();
this.mInflater = LayoutInflater.from(context);
this.mData = data;
this.mSelectRegister = selectRegister;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.card_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.myTextView.setText(mData[position]);
holder.mImageView.setImageResource(imageResource[Integer.parseInt(mData[position]) - 1]);
}
@Override
public int getItemCount() {
return mData.length;
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView myTextView;
ImageView mImageView;
ImageView cardView;
ObjectAnimator animator = ObjectAnimator.ofFloat(itemView, "rotationY", 0, 180);
ObjectAnimator animatorBack = ObjectAnimator.ofFloat(itemView, "rotationY", 180, 360);
ViewHolder(View itemView) {
super(itemView);
myTextView = itemView.findViewById(R.id.info_text);
mImageView = itemView.findViewById(R.id.photo_item);
cardView = itemView.findViewById(R.id.photo_stroke);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
}
}
void setClickListener(ItemClickListener itemClickListener) {
this.mClickListener = itemClickListener;
}
public interface ItemClickListener {
void onItemClick(View view, int position, int score);
}
}
通過上面的操作,已經完成所有元件的顯示,明天要進入到點擊的部分來判斷可能選擇的結果與動畫的撥放。