【发布时间】:2021-10-17 07:23:38
【问题描述】:
以下是我的活动布局文件
<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">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
app:layout_constraintTop_toTopOf="parent"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
android:background="#B71C1C"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
<RelativeLayout
android:id="@+id/progresslayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff">
<ProgressBar
android:id="@+id/progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:indeterminateTint="@color/colorPrimaryDark"
tools:targetApi="lollipop" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
以下是我的回收站代码
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="350dp"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_margin="20dp"
app:cardCornerRadius="8dp"
app:cardElevation="6dp">
<LinearLayout
android:id="@+id/llcontent"
android:layout_width="match_parent"
android:background="#E91E63"
android:weightSum="6"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_weight="3.3"
android:layout_width="290dp"
android:layout_height="wrap_content">
<TextView
android:id="@+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name of the file"
android:padding="8dp"
android:textSize="18sp"
android:textStyle="bold"
android:layout_marginLeft="80dp"
android:textColor="#ffffff"/>
<TextView
android:id="@+id/txtQuote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/txtTitle"
android:text="name_of_the_author"
android:padding="8dp"
android:textColor="#ffffff"
android:textSize="19sp"/>
<TextView
android:id="@+id/txtQuoteid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="rs_299"
android:padding="8dp"
android:layout_toRightOf="@id/txtTitle"
android:textSize="15sp"
android:textStyle="bold"
android:textColor="#ffffff"/>
</RelativeLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
问题是因为对于 textquote 文本视图,我正在获取并尝试显示大文本内容,某些内容未显示。如何解决此问题?。回收器的大小根据第一个内容自动修复的第一个回收站视图请帮助我
descactivitycode
class descactivity : AppCompatActivity() {
lateinit var recyclerview: RecyclerView
lateinit var adapter: Horizontal_RecyclerView
lateinit var layoutmanager: RecyclerView.LayoutManager
lateinit var progressBar: ProgressBar
lateinit var progressLayout: RelativeLayout
lateinit var button: Button
lateinit var toolbar: Toolbar
val bookInfoList = arrayListOf<Book>()
val ratingComparator = Comparator<Book> { book1, book2 ->
book1.title.compareTo(book2.title, true)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_descactivity)
recyclerview = findViewById(R.id.recyclerView)
progressBar = findViewById(R.id.progressbar)
layoutmanager = LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false)
progressLayout = findViewById(R.id.progresslayout)
progressLayout.visibility = View.VISIBLE
toolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar)
val intent = intent
val selectionOption = intent.getStringExtra("option")
val selection = intent.getStringExtra("option1")
if (selectionOption != null) {
val queue = Volley.newRequestQueue(this)
val url = "https://quotesapp-api.herokuapp.com/getquotes"
if (ConnectionManager().checkConnectivity(this as Context)) {
val JSONObjectRequest =
object : JsonObjectRequest(Request.Method.GET, url, null, Response.Listener {
println("response is $it")
val success = it.getBoolean("success")
try {
if (success) {
progressLayout.visibility = View.GONE
val data = it.getJSONArray("data")
for (i in 0 until data.length()) {
val bookJsonObject = data.getJSONObject(i)
val BookObject = Book(
bookJsonObject.getString("_id"),
bookJsonObject.getString("title"),
bookJsonObject.getString("quoteid"),
bookJsonObject.getString("quote")
//bookJsonObject.getInt("__v")
)
bookInfoList.add(BookObject)
adapter =
Horizontal_RecyclerView(this as Context, bookInfoList)
recyclerview.adapter = adapter
recyclerview.layoutManager = layoutmanager
}
}
} catch (e: JSONException) {
Toast.makeText(this, "some unknown error occurred", Toast.LENGTH_SHORT)
.show()
}
}, Response.ErrorListener {
Toast.makeText(this, "volley error occured", Toast.LENGTH_SHORT).show()
println("error is $it")
}) {
override fun getHeaders(): MutableMap<String, String> {
val headers = HashMap<String, String>()
return headers
}
}
queue.add(JSONObjectRequest)
}
}
适配器
class Horizontal_RecyclerView(val context: Context, val itemList:ArrayList<Book>):RecyclerView.Adapter<Horizontal_RecyclerView.MyViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val view=LayoutInflater.from(parent.context).inflate(R.layout.row,parent,false)
return MyViewHolder(view)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val book=itemList[position]
holder.txtTitle.text=book.title
holder.txtQuote.text=book.quote
holder.txtQuoteid.text=book.quoteid
}
override fun getItemCount(): Int {
return itemList.size
}
class MyViewHolder(itemView:View):RecyclerView.ViewHolder(itemView) {
val txtTitle: TextView =itemView.findViewById(R.id.txtTitle)
val txtQuote: TextView =itemView.findViewById(R.id.txtQuote)
val txtQuoteid: TextView =itemView.findViewById(R.id.txtQuoteid)
// val llcontent: LinearLayout =itemView.findViewById(R.id.llcontent )
}
}
【问题讨论】:
-
你能把你的java/kotlin代码贴在这里吗?
-
@FlyingDutchman 我已添加代码
-
添加您的 Horizontal_RecyclerView 适配器代码。
-
@AmarIlindra 我已添加。我只觉得布局文件有问题
标签: android xml android-layout android-recyclerview