build.gradle(app):
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.android.support:cardview-v7:28.0.0'
Activity_main.xml:- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout 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"
- android:orientation="vertical"
- tools:context=".MainActivity">
- <android.support.v7.widget.RecyclerView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:id="@+id/recycleView"
- android:layout_gravity="center_horizontal" />
- </LinearLayout>
複製代碼 row_item.xml:- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="158dp"
- android:paddingBottom="10dp"
- android:paddingLeft="10dp"
- android:paddingRight="10dp"
- android:paddingTop="10dp">
- <android.support.v7.widget.CardView
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:id="@+id/cardView"
- android:layout_width="match_parent"
- android:layout_height="128dp"
- android:gravity="center_vertical"
- app:cardBackgroundColor="#a0d0ff"
- app:cardCornerRadius="12dp"
- app:cardElevation="5dp">
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="128dp">
- <ImageView
- android:id="@+id/itemImage"
- android:layout_width="108dp"
- android:layout_height="108dp"
- android:layout_marginLeft="10dp"
- android:layout_marginTop="10dp" />
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="256dp"
- android:layout_height="128dp"
- android:orientation="vertical">
- <TextView
- android:id="@+id/itemText1"
- android:layout_width="match_parent"
- android:layout_height="64dp"
- android:layout_marginLeft="20dp"
- android:layout_marginTop="20dp"
- android:textSize="30sp" />
- <TextView
- android:id="@+id/itemText2"
- android:layout_width="match_parent"
- android:layout_height="64dp"
- android:layout_marginLeft="20dp" />
- </LinearLayout>
- </LinearLayout>
- </android.support.v7.widget.CardView>
- </LinearLayout>
複製代碼 MainActivity.java:- package ray.ap.recyclerviewtest;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.support.v7.widget.LinearLayoutManager;
- import android.support.v7.widget.RecyclerView;
- import java.util.ArrayList;
- public class MainActivity extends AppCompatActivity {
- String n[]={"水瓶座","雙魚座","白羊座","金牛座","雙子座","巨蟹座","獅子座","處女座","天秤座","天蠍座","射手座","魔羯座"};
- String date[]={"1月21日 - 2月18日","2月19日 - 3月20日","3月21日 - 4月19日","4月21日 - 5月21日","5月22日 - 6月21日","6月22日 - 7月22日","7月23日 - 8月22日","8月23日 - 9月22日","9月23日 - 10月23日","10月24日 - 11月21日","11月22日 - 12月20日","12月21日 - 1月20日"};
- int imgId[]={R.drawable.p0,R.drawable.p1,R.drawable.p2,R.drawable.p3,R.drawable.p4,R.drawable.p5,R.drawable.p6,R.drawable.p7,R.drawable.p8,R.drawable.p9,R.drawable.p10,R.drawable.p11};
- RecyclerView mRecyclerView;
- ArrayList<Card> mDataSet = new ArrayList<Card>();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mRecyclerView = (RecyclerView)this.findViewById(R.id.recycleView);
- for(int i = 0;i < 12;i++){
- Card cc = new Card();
- cc.name = n[i];
- cc.date = date[i];
- cc.image = this.getDrawable(imgId[i]);
- mDataSet.add(cc);
- }
- MyRecyclerViewAdapter myAdapter = new MyRecyclerViewAdapter(this,mDataSet);
- mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
- mRecyclerView.setAdapter(myAdapter);
- }
- }
複製代碼 MyRecycleViewAdapter.java:- package ray.ap.recyclerviewtest;
- import android.content.Context;
- import android.support.annotation.NonNull;
- import android.support.v7.widget.RecyclerView;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.ImageView;
- import android.widget.TextView;
- import android.widget.Toast;
- import java.util.ArrayList;
- public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {
- private static Context mContext;
- private static ArrayList<Card> mDataSet = new ArrayList<Card>();
- public MyRecyclerViewAdapter(Context context, ArrayList<Card> dataset){
- mContext = context;
- mDataSet = dataset;
- }
- @NonNull
- @Override
- public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
- return new ViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_item,viewGroup,false));
- }
- @Override
- public void onBindViewHolder(@NonNull ViewHolder myViewHolder, int position) {
- myViewHolder.itemView.setTag(position);
- myViewHolder.itemText1.setText(mDataSet.get(position).name);
- myViewHolder.itemText2.setText(mDataSet.get(position).date);
- myViewHolder.itemImage.setImageDrawable(mDataSet.get(position).image);
- }
- @Override
- public int getItemCount() {
- return mDataSet.size();
- }
- public static class ViewHolder extends RecyclerView.ViewHolder {
- final ImageView itemImage;
- final TextView itemText1;
- final TextView itemText2;
- public ViewHolder(@NonNull View itemView) {
- super(itemView);
- itemView.setOnClickListener(new View.OnClickListener(){
- @Override
- public void onClick(View view) {
- int pos = (int)view.getTag();
- Toast.makeText(mContext,"你點選的是."+mDataSet.get(pos).name+"!",Toast.LENGTH_SHORT).show();
- }
- });
- itemImage = (ImageView) itemView.findViewById(R.id.itemImage);
- itemText1 = (TextView) itemView.findViewById(R.id.itemText1);
- itemText2 = (TextView) itemView.findViewById(R.id.itemText2);
- }
- }
- }
複製代碼 Card.java:- package ray.ap.recyclerviewtest;
- import android.graphics.drawable.Drawable;
- public class Card {
- public String name;
- public String date;
- public Drawable image;
- }
複製代碼 |