java - 如何更新 ViewPager 中选定项目的样式?
问题描述
我想更新 ViewPager 中所选项目的样式。我怎样才能做到这一点?
这是UI设计。如您所见,选择了“十一月”选项卡,因此相应的栏以黄色突出显示。
我实现这个的方式是一个带有自定义项(白色,未突出显示)的 ViewPager:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:layout_width="142dp"
android:layout_height="275dp"
android:layout_margin="0dp"
app:cardElevation="0dp"
app:cardBackgroundColor="@android:color/transparent">
<TextView
android:id="@+id/monthly_spend_month"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/December"
android:textColor="@color/aluminum"
android:textSize="20sp"
android:textStyle="normal"
android:layout_marginStart="22dp"
android:layout_marginTop="34dp"/>
<RelativeLayout
android:id="@+id/monthly_spend_card"
android:layout_marginTop="120dp"
android:layout_marginStart="22dp"
android:layout_width="120dp"
android:layout_height="155dp"
android:background="@drawable/monthlyspend_card_bg_inactive">
<TextView
android:id="@+id/monthly_spend_text"
android:textColor="@color/almost"
android:layout_marginTop="50dp"
android:layout_marginStart="22dp"
android:text="Total
spent"
android:textSize="@dimen/balanceDescTextSize"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/monthly_spend_amount"
android:textColor="@color/almost"
android:layout_marginTop="10dp"
android:layout_marginStart="22dp"
android:layout_below="@id/monthly_spend_text"
android:text="$ 254.98"
android:textSize="@dimen/balanceDescTextSize"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
我计划以编程方式进行突出显示。
这是一些适配器代码:
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
layoutInflater = LayoutInflater.from(context);
View view = layoutInflater.inflate(R.layout.monthlyspend_item, container, false);
TextView monthlySpendMonth, monthlySpendAmount;
monthlySpendCard = view.findViewById(R.id.monthly_spend_card);
monthlySpendMonth = view.findViewById(R.id.monthly_spend_month);
monthlySpendAmount = view.findViewById(R.id.monthly_spend_amount);
monthlySpendMonth.setText(monthlySpendings.get(position).getMonth());
monthlySpendAmount.setText(monthlySpendings.get(position).getAmountSpent());
container.addView(view, 0);
return view;
}
任何帮助,将不胜感激!
解决方案
您应该在所选项目位置更改您的模型(在您的情况下为“每月支出”)。
然后通知适配器
adapter.notifyDataSetChanged()
最后,根据模型自定义选择视图。例如:
if(monthlySpendings.get(position).isSelected) monthlySpendCard.setBackgroundColor(Color.parseColor("#fcd14b"));
推荐阅读
- php - SQLSTATE [22007]:无效的日期时间格式:1366 不正确的整数值:问题
- spring - Spring Boot REST API 返回部分响应
- javascript - 如何以所需格式显示时间?
- android - 重新安装相同版本的应用程序后,警报不起作用
- javascript - 如何在不更改当前 URL 的情况下打开新窗口?
- javascript - 如何排序,查找并将一个元素放在顶部?
- r - 在数据框列表中的某个列上应用 gsub
- python - How can I display kind="swarm" and kind="point" on the same catplot in Seaborn?
- flutter - Flutter Android Studio 使花括号从新行开始
- assembly - 如何在 Flat Assembler 中进行冒泡排序?