设定 xml 布局
首先在 .xml 文件中放置一个 RecyclerView
1 2 3 4 5 6 7 8
| <androidx.recyclerview.widget.RecyclerView android:id="@+id/unpair_device_list" android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollbars="vertical" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />
|
其次新建一个 .xml 文件用于对 RecyclerView 中每一条数据进行布局,姑且称为「子布局」
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| <LinearLayout android:id="@+id/unpair_linear" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:background="@drawable/recycler_touch_bg" android:minHeight="28dp" android:layout_marginTop="8dp" app:layout_constraintTop_toTopOf="parent">
<TextView android:id="@+id/unpair_device_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:width="200dp" android:singleLine="false" android:text="name" />
<Space android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0.5" />
<TextView android:id="@+id/unpair_device_status" android:layout_width="wrap_content" android:layout_height="wrap_content" android:width="84dp" android:gravity="end" android:text="status" />
<ImageButton android:id="@+id/device_detail" android:layout_width="32dp" android:layout_height="32dp" android:layout_marginEnd="4dp" android:layout_marginStart="4dp" android:background="@android:color/transparent" android:contentDescription="@string/device_detail_ico" android:scaleType="fitCenter" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" app:srcCompat="@drawable/back_ico" /> </LinearLayout>
|
构建 RecyclerViewAdapter
接着新建一个 RecyclerViewAdapter.kt(这里以 kotlin 为例)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| class UnpairRecyclerViewAdapter(private val context: Activity) :RecyclerView.Adapter<UnpairRecyclerViewAdapter.ViewHolder>() { private var devices: List<BluetoothDevice> = listOf() class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { val unpairDeviceName: TextView = itemView.findViewById(R.id.unpair_device_name) val unpairDeviceStatus: TextView = itemView.findViewById(R.id.unpair_device_status) } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { val view = LayoutInflater.from(parent.context).inflate(R.layout.unpair_list_item, parent, false) return ViewHolder(view) } override fun onBindViewHolder(holder: ViewHolder, position: Int) { val device = devices[position] if (!Utils.checkPermission(context, BLUETOOTH_CONNECT)) { Utils.requestPermission(context, BLUETOOTH_CONNECT) return } holder.unpairDeviceName.text = device.name ?: "Unknown" holder.unpairDeviceStatus.text = "未配对" } override fun getItemCount(): Int { return devices.size } @SuppressLint("NotifyDataSetChanged") fun setUnpairList(deviceList: List<BluetoothDevice>) { devices = deviceList context.runOnUiThread { Handler(Looper.getMainLooper()).post { notifyDataSetChanged() } } } }
|
在 Activity 中实例化
接下来需要在目标 activity 中加载RecycleView的适配器和更新数据
1 2 3 4 5 6 7 8 9 10 11 12
| val pairDevicesList: RecyclerView = view.findViewById(R.id.pair_device_list)
val pairAdapter = PairRecyclerViewAdapter(requireActivity(), this)
pairDevicesList.adapter = pairAdapter
pairDevicesList.layoutManager = LinearLayoutManager(requireActivity())
pairAdapter.setUnpairList()
|