簡體   English   中英

在RecyclerView中從ViewHolder刪除小部件

[英]Removing widgets from ViewHolder in RecyclerView

我想制作一個用於跟蹤我的支出的應用。 我有一個帶有類別表的數據庫。 在此表中,我保存了category_id(int),category_name(varchar),limit_week(int),limit_month(int)。 如果我不想有限制,則可以為null。 添加類別非常有效。

現在,我想在RecyclerView中顯示類別。 項目的布局如下所示:

<androidx.cardview.widget.CardView android:layout_height="wrap_content"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="@dimen/card_corner_radius"
app:cardBackgroundColor="@color/card_color"
app:contentPadding="@dimen/card_content_padding"
app:cardElevation="@dimen/card_elevation"
android:layout_margin="@dimen/card_margin">

<androidx.constraintlayout.widget.ConstraintLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/tvCat"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tvLimWeek"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/weekly_limit"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tvCat" />

    <TextView
        android:id="@+id/tvLimMonth"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/monthly_limit"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tvLimWeek" />

    <TextView
        android:id="@+id/tvLimWeekEnter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toEndOf="@id/tvLimMonth"
        app:layout_constraintTop_toTopOf="@id/tvLimWeek"
        android:layout_marginLeft="@dimen/margin_16sp"
        android:layout_marginStart="@dimen/margin_16sp" />
    <TextView
        android:id="@+id/tvLimMonthEnter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toEndOf="@id/tvLimMonth"
        app:layout_constraintTop_toTopOf="@id/tvLimMonth"
        android:layout_marginLeft="@dimen/margin_16sp"
        android:layout_marginStart="@dimen/margin_16sp" />

    <ProgressBar
        android:id="@+id/progressBarWeek"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/tvLimWeekEnter"
        app:layout_constraintTop_toTopOf="@+id/tvLimWeekEnter"
        android:max="100"/>

    <ProgressBar
        android:id="@+id/progressBarMonth"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/tvLimMonthEnter"
        app:layout_constraintTop_toTopOf="@+id/tvLimMonth"
        android:max="100"/>

</androidx.constraintlayout.widget.ConstraintLayout>

我只想顯示進度條(如果有限制),否則我想刪除它們。 這是我的RecyclerView適配器:

class CategoryAdapter(private val mCats: List<Category>, private val context: Context): RecyclerView.Adapter<CategoryViewHolder>(){
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CategoryViewHolder {
    return CategoryViewHolder(LayoutInflater.from(context).inflate(R.layout.categories_view, parent, false))
}

override fun getItemCount() = mCats.size

override fun onBindViewHolder(holder: CategoryViewHolder, position: Int) {
    val cat: Category = mCats[position]
    holder.tvCat.text = cat.cat_name
    if(cat.limit_week != null)
        holder.tvLimitWeekEnter.text = cat.limit_week.toString()
    else {
        holder.tvLimitWeek.height = 0
        holder.tvLimitWeekEnter.height = 0
    }
    if(cat.limit_month != null)
        holder.tvLimitMonthEnter.text = cat.limit_month.toString()
    else {
        holder.tvLimitMonth.height = 0
        holder.tvLimitMonthEnter.height = 0
    }
}

}

我不知道是否可以通過某種方式刪除TextView,所以我只是將高度設置為0。但是現在我需要刪除進度條,在這里我無法更改高度。 那么有沒有辦法刪除它們?

您可以在進度條上使用setVisibility(View.GONE)使其不可見,並且不占用任何空間用於布局。

您可以在進度條上更改可見性

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM