Удаление виджетов из ViewHolder в RecyclerView - PullRequest
0 голосов
/ 20 марта 2019

Я хочу сделать приложение для отслеживания моих расходов. У меня есть база данных с таблицей под названием категории. В этой таблице я сохраняю category_id (int), category_name (varchar), limit_week (int), limit_month (int). Если я не хочу иметь пределы, они могут быть нулевыми. Добавление категорий работает отлично.

Теперь я хочу отобразить категории в 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
    }
}

}

Я не знаю, смогу ли я каким-либо образом удалить TextViews, поэтому я просто установил высоту на 0. Но теперь мне нужно удалить индикаторы выполнения, где я не могу изменить высоту. Так есть ли способ удалить их?

Ответы [ 2 ]

2 голосов
/ 20 марта 2019

Вы можете использовать setVisibility (View.GONE) на ваших индикаторах выполнения, чтобы сделать их невидимыми и не занимать места для макета.

0 голосов
/ 20 марта 2019

вы можете изменить видимость на вашем индикаторе прогресса

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...