dependencies {
implementation "androidx.recyclerview:recyclerview:1.1.0"
// For control over item selection of both touch and mouse driven selection
implementation "androidx.recyclerview:recyclerview-selection:1.1.0-rc01"
}
public class MainActivity extends AppCompatActivity {
MyRecyclerViewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 要傳入recycleView的資料
ArrayList<String> cc = new ArrayList<>();
cc.add("Horse");
cc.add("Cow");
cc.add("Camel");
cc.add("Sheep");
cc.add("Goat");
// 先用ID尋找哪個是你recycleView的ID
RecyclerView recyclerView = findViewById(R.id.acc);
//設定排序用LinearLayout的排法
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//創一個Adapter 後面參數 (this,cc)
//this是你現在在哪個activity(context)
//cc (list) 總共有五筆
//畫面就會顯示五個itemView 五個(recyclerview_row.xml)
adapter = new MyRecyclerViewAdapter(this, cc);
//接下來設定recyclview要綁上的是剛剛宣告的adapter
//所以adapter的作用是處理那五筆資料該如何顯示
recyclerView.setAdapter(adapter);
}
}
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/acc"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<?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="100dp"
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:id="@+id/ccccc"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:textSize="20sp"/>
</LinearLayout>
MyRecyclerViewAdapter.java
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {
private List<String> mDataList;
private LayoutInflater mInflater;
private ItemClickListener mClickListener;
// 剛剛context跟list就是透過這個傳進來 然後我們設mData=data
// 所以mData=Horse,Cow,Camel,Sheep,Goat
// 這裡的inflater是要可以載入黃色那塊 就是recyclerview_row.xml
MyRecyclerViewAdapter(Context context, List<String> data) {
this.mInflater = LayoutInflater.from(context);
this.mDataList = data;
}
//創造viewHolder
// 用inflater載入黃色這塊,並傳入viewHolder
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.recyclerview_row, parent, false);
return new ViewHolder(view);
}
// 這裡是黃色那塊尋找textView Id的,不找就不知道在哪
public class ViewHolder extends RecyclerView.ViewHolder {
TextView myTextView;
ViewHolder(View itemView) {
super(itemView);
myTextView = itemView.findViewById(R.id.ccccc);
itemView.setOnClickListener(this);
}
}
// 用viewHolder綁定資料
//position是mData的第幾個 向是第零個是Horse 第二個是Camel
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
String dddd = mData.get(position);
holder.myTextView.setText(dddd);
}
// 確定mData有幾個 , 可以試試看return1的話就只會有一個資料出來喔
@Override
public int getItemCount() {
return mDataList.size();
}
}