android-recyclerview 相关问题

RecyclerView小部件是ListView和GridView的更高级和灵活的版本。

运行应用程序时不显示RecycleView

我正在尝试使用volley从mysql数据库获取数据并将其显示在recycleview中,但我有一个问题recycleview不显示。 这是 order_layout.xml : 我正在尝试使用 volley 从 mysql 数据库获取数据并将其显示在 recycleview 中,但我有一个问题,recycleview 没有显示。 这是 order_layout.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" android:id="@+id/cardHolder" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:background="@drawable/order_background" android:padding="8dp"> <LinearLayout android:id="@+id/orderView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:background="@drawable/orderid_background" android:orientation="vertical" android:padding="16dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="Order ID " android:textColor="@color/white" /> <TextView android:id="@+id/orderID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="6dp" android:text="1" android:textColor="@color/white" android:textSize="18sp" /> </LinearLayout> <LinearLayout android:id="@+id/linear1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="12dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/orderView" app:layout_constraintTop_toTopOf="parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Client Name:" android:textColor="@color/black" android:textSize="20sp" /> <TextView android:id="@+id/clientName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="10dp" android:text="test" android:textColor="@color/black" android:textSize="20sp" /> </LinearLayout> <LinearLayout android:id="@+id/linear2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="12dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/orderView" app:layout_constraintTop_toBottomOf="@+id/linear1"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Order Date:" android:textColor="@color/black" android:textSize="20sp" /> <TextView android:id="@+id/orderDate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="10dp" android:text="test" android:textColor="@color/black" android:textSize="20sp" /> </LinearLayout> <LinearLayout android:id="@+id/linear3" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="12dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/orderView" app:layout_constraintTop_toBottomOf="@+id/linear2"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Status:" android:textColor="@color/black" android:textSize="20sp" /> <TextView android:id="@+id/orderStatus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="10dp" android:text="test" android:textColor="@color/black" android:textSize="20sp" /> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout> activity_main.xml: <?xml version="1.0" encoding="utf-8"?> <layout 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"> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/RecycleV" android:layout_width="match_parent" android:layout_height="match_parent" tools:itemCount="2" tools:listitem="@layout/order_layout" /> <com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/addBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentBottom="true" android:layout_margin="16dp" android:backgroundTint="@color/cardview_dark_background" android:contentDescription="@string/adding_order" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:srcCompat="@drawable/baseline_add_24" app:tint="@color/white" /> </androidx.constraintlayout.widget.ConstraintLayout> </layout> MainActivity.kt package com.example.demo import android.os.Bundle import android.util.Log import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import androidx.recyclerview.widget.LinearLayoutManager import com.android.volley.Request import com.android.volley.toolbox.StringRequest import com.android.volley.toolbox.Volley import com.example.demo.databinding.ActivityMainBinding import org.json.JSONArray import org.json.JSONException class MainActivity : AppCompatActivity() { private val URL = "http://192.168.100.12/demo/Android/v1/getAll.php" lateinit var binding: ActivityMainBinding private lateinit var ordersList: ArrayList<Order> private lateinit var rcvAdapter: CustomAdapter override fun onCreate(savedInstanceState: Bundle?) { binding = ActivityMainBinding.inflate(layoutInflater) super.onCreate(savedInstanceState) setContentView(binding.root) ordersList = ArrayList() rcvAdapter = CustomAdapter(ordersList) binding.RecycleV.layoutManager = LinearLayoutManager(this) binding.RecycleV.setHasFixedSize(true) binding.RecycleV.adapter = rcvAdapter loadData() // binding.rv.apply { // layoutManager = LinearLayoutManager(this@MainActivity) // adapter = rcvAdapter // } // binding.sr.setOnRefreshListener { // loadData() // binding.sr.isRefreshing = false // } // binding.addBtn.setOnClickListener { // val intent = Intent() // } } private fun loadData() { val queue = Volley.newRequestQueue(this) val stringRequest = StringRequest(Request.Method.POST, URL, { response -> try { Log.e("anytext", response) val jsonArray = JSONArray(response) for (i: Int in 0 until jsonArray.length()) { val jsonObject = jsonArray.getJSONObject(i) val order = Order( jsonObject.getInt("ID"), jsonObject.getString("orderDate"), jsonObject.getString("status"), jsonObject.getString("name") ) ordersList.add(order) Log.e("order data", order.toString()) Log.e("array size", ordersList.size.toString()) } } catch (e: JSONException) { e.printStackTrace() } if (response.isEmpty()) { Toast.makeText(this, "There is no orders", Toast.LENGTH_SHORT).show() } }, { error -> Toast.makeText( this, error.message, Toast.LENGTH_LONG ).show() }) queue.add(stringRequest) Log.e("final", "success") } } 自定义适配器.kt package com.example.demo import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.TextView import androidx.recyclerview.widget.RecyclerView class CustomAdapter( private val ordersList: ArrayList<Order> ) : RecyclerView.Adapter<CustomAdapter.MyViewHolder>() { inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { val id: TextView = itemView.findViewById(R.id.orderID) val clientName: TextView = itemView.findViewById(R.id.clientName) val orderDate: TextView = itemView.findViewById(R.id.orderDate) val orderStatus: TextView = itemView.findViewById(R.id.orderStatus) } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder { val view = LayoutInflater.from(parent.context).inflate(R.layout.order_layout, parent, false) return MyViewHolder(view) } override fun getItemCount(): Int { return ordersList.size } override fun onBindViewHolder(holder: MyViewHolder, position: Int) { val order = ordersList[position] holder.apply { id.text = order.id.toString() orderStatus.text = order.status orderDate.text = order.orderDate clientName.text = order.clientName } } } 订单.kt package com.example.demo data class Order(val id: Int, val orderDate: String, val status: String, val clientName: String) 我尝试更改 activiy_main.xml 和 order_layout.xml 的布局,但没有任何效果,并且还尝试记录以了解数据是否已获取,我发现它们已被检索 您没有在任何地方更新适配器。 向 ordersList 添加内容后,您需要让 RecyclerView 的适配器知道。 例如: fun update(val newOrdersList: List<Order>) { ordersList.clearAll() ordersList.addAll(newOrdersList) notifyDataSetChanged() // This is deprecated but works - demonstration only } 整个 notifyDatasetChanged 可能会给您带来一个优化 RecyclerView 性能的新兔子洞,但为了给您一个简单的答案,我不会深入讨论它。

回答 1 投票 0

为什么当您在回收器视图中删除项目时动画会变得混乱

我在回收者视图中列出了位置列表。当我按下删除图标按钮时,该按钮的项目应该被删除。 覆盖 fun onBindViewHolder(holder: ViewHolder,position: Int...

回答 1 投票 0

binding.root 代表什么?

以下是我的代码片段。 类 DashBoardHolder(val 绑定: ActivityDashBoardBinding) : RecyclerView.ViewHolder(binding.root) { 内部变量 tvName: TextView = itemView.findViewById(R.id.

回答 2 投票 0

水平滚动不适用于 ViewPager2 Android 中的 RecylerView

我正在尝试在 ViewPager2 内实现 Horizontal RecyclerView,但它不起作用。 我正在尝试在 ViewPager2 中实现 Horizontal RecyclerView,但它不起作用。 <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/black" android:paddingLeft="10dp" android:paddingRight="10dp"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/rvMusicList" android:layout_width="match_parent" android:layout_height="wrap_content" app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" android:orientation="horizontal" tools:listitem="@layout/items_music" app:layout_constraintTop_toTopOf="parent" android:layout_marginTop="30dp"/> ... 我的 Kotlin 代码如下 rvBottomMenu.apply { layoutManager = LinearLayoutManager(this@MainActivity, RecyclerView.HORIZONTAL, false) adapter = bottomMenuAdapter setHasFixedSize(true) } 我尝试了以下链接解决方案,但没有成功: 文字 你可以这样尝试!内部视图寻呼机适配器,同时设置回收器视图适配器将 LinearLayoutManger 设置为水平 recylcerView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));

回答 1 投票 0

我的 recycleView 不适用于 dropdrown 选择(Android Kotlin)

MainActivity.kt: 包 com.example.smstest 导入 android.annotation.SuppressLint 导入 android.content.BroadcastReceiver 导入 android.content.Context 导入 android.content.Intent 导入...

回答 1 投票 0

LinearLayoutManager 无法解析 getOrientation()

我有一个在片段中实现的 RecyclerView,我正在尝试向代码中添加分隔线。我让 RecylcerView 按预期工作,但 LinearLayoutManager 无法解析 getOrientati...

回答 4 投票 0

如何像WhatsApp一样根据日历日期和时间对聊天消息进行分组?

我正在开发聊天应用程序,但遇到问题。如果有人可以提供帮助,我想在一组消息(例如 WhatsApp)上显示日期和时间。例如我们有这个: 输入图像描述...

回答 1 投票 0

NestedScrollView 内的 Recyclerview 关闭底部表单

我有一个带有地图的页面,如果用户单击按钮,则会展开底页。在此底页中,有 2 个选项卡,其中有一个列表。如果我从 d 开始滚动列表...

回答 1 投票 0

单击删除图像图标时从 recyclerView 中删除所选项目

我想从 recyclerView 列表中删除所选项目。 在我的列表中,我每行都有删除图像图标,我想在单击删除图像图标时删除项目。怎样才能做到呢? 我的适配器代码这是看我的...

回答 2 投票 0

SQLite 数据库在 Android Studio 上保存图像

我尝试将图像数据存入sqlite数据库。在我想获取数据库中的数据并使用回收器视图之后。但其他数据成功图像数据未进入回收者视图 这是我的

回答 1 投票 0

如何添加适配器包括适配器recycleview android?

我正在尝试使用适配器包含适配器 recyleview 错误显示来修复此错误。在此 AdapterGrid 下方包含 AdapterRecipes 添加适配器错误显示。我尝试在下面显示此错误,添加图像如何修复

回答 1 投票 0

RecyclerView在android(androidx)中的依赖关系?

我为 RecycleView 使用了实现“com.google.android.material:material:1.0.0”。在布局中,当我输入“recycle”时,它给出的建议为 androidx.appcompat.app.AlertController$RecycleListView...

回答 3 投票 0

如何将recyclerview添加到项目中

尝试导入android.support.v7.widget.RecyclerView,但没有成功,还添加了 编译'com.android.support:recyclerview-v7:23.1.1' 到 build.gradle,但它也没有帮助

回答 6 投票 0

为什么在recyclerview适配器中看不到item的id?

我正在使用视图绑定功能编写一个recyclerview,但我无法访问项目中item.xml 文件中存在的项目的id。这是适配器的代码,它...

回答 1 投票 0

adapter.notifyDataSetChange() 不起作用

我有一个回收视图,它包含几行。 每行都有一个图像视图(心形)用于喜欢和不喜欢,我需要的是每次通过单击更改图像视图的图像。 当我明星...

回答 5 投票 0

调用notifyDataSetChanged时,仅更新RecyclerView中的最后一项

我想建立一个具有选择功能的分段列表。 为此,我的布局文件中有多个 RecyclerView(例如 5 个),并且每个 RecyclerView 的数据列表都是动态的。正如您在...中看到的那样。

回答 1 投票 0

在 NestedScrollView 内时,回收器视图加载大数据非常慢

我在 NestedScrollView 中添加了 RecyclerView。基本上我希望 RecyclerView 与其他视图一起滚动。我面临的问题是,对于一小部分数据,它工作正常,但是......

回答 5 投票 0

在附带PageSnapHelper的RecyclerView中使用TouchImageView?

我的应用程序正在做一个在 RecyclerView 上使用 TouchImageView 的画廊。 我试图使用此类在附有 PageSnapHelper 的 RecyclerView 中显示多个全屏图像 这有效...

回答 2 投票 0

在android中滚动RecyclerView时SeekBar进度发生变化

我正在开发一款类似于 What’s Up 的聊天应用程序。我遇到的问题是我在回收器视图中有搜索栏和播放图标。当我点击播放符号时,歌曲就会播放...

回答 1 投票 0

在android studio中点击回收器视图时如何检索数据库id?

我一直在开发一个Android应用程序,当我单击回收器视图中的特定行时,我需要获取数据库ID。当我之前编写这个应用程序时,我更新并删除了记录基于...

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.