RecyclerView在Android Studio的功能是一項可以上下捲動的橫列欄。
這個依賴可以使用關鍵字搜尋版本來套入不同的Android版本
implementation "androidx.recyclerview:recyclerview-selection:1.1.0"
implementation 'androidx.recyclerview:recyclerview:1.2.1'
套入dependencies後需要點選右上方的Sync Now才可以後續操作
選擇需要放置的layout中即可,這邊筆記是開啟新檔案所以在主頁面的.xml
檔案放置recyclerview元件
<?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="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
XML畫面呈現預覽
<?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="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
XML預覽呈現
預覽結果:可以明顯看出藍色線條只到文字的大小而不會整個XML都占滿。
以上為畫面的刻劃,可以結合GuideLine進行刻劃達到不同畫面比例皆可以通用的效果。
class ViewHolder extends RecyclerView.ViewHolder{
private TextView tvname,tvdate,tvtime,tvrewardpoint;
public ViewHolder(@NonNull View itemView) {
super(itemView);
tvname = itemView.findViewById(R.id.textView);
tvdate = itemView.findViewById(R.id.textView2);
tvtime = itemView.findViewById(R.id.textView3);
tvrewardpoint = itemView.findViewById(R.id.textView4);
}
public void itemView(HashMap<String, String> stringStringHashMap) {
}
}
@NonNull
@Override
public ListAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.listitem,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ListAdapter.ViewHolder holder, int position) {
holder.tvname.setText(arrayList.get(position).get("name_infomation"));
holder.tvdate.setText("YYYY/MM/DD");
holder.tvtime.setText("hh/mm");
holder.tvrewardpoint.setText(arrayList.get(position).get("rewardpoint_infomation"));
}
@Override
public int getItemCount() {
return arrayList.size();
}
ListAdapter listAdapter;
RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
listAdapter = new ListAdapter();
recyclerView.setAdapter(listAdapter);
listAdapter.makedata();
}
public void makedata(){
//HashMap.put(Key,Value);
for (int i = 0;i<100;i++){
HashMap<String,String> hashMap = new HashMap<>();
hashMap.put("name_infomation","商店代號:"+String.format("%02d",i+1));
hashMap.put("date_infomation",String.valueOf(new Random().nextInt(80) + 20));
hashMap.put("time_infomation",String.valueOf(new Random().nextInt(80) + 20));
hashMap.put("rewardpoint_infomation",String.valueOf(
(Integer.parseInt(hashMap.get("date_infomation"))
+(Integer.parseInt(hashMap.get("time_infomation"))))^2+48/99*2
));
arrayList.add(hashMap);
}
}