У меня есть ListView внутри RecyclerView.Видимость ListView установлена на gone
.При щелчке элемента строки RecyclerView, ListView будет виден.
В макете XML элемент строки ListView TextView имеет белый цвет.Но я хочу произвольно изменить цвет элемента TextView элемента строки ListView на серый после того, как он установлен в видимый.
Пожалуйста, проверьте код ниже:
Элемент строки Recyclerview XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/dark_gray">
<RelativeLayout
android:id="@+id/challenges_row_item"
android:layout_width="match_parent"
android:layout_height="70dp"
android:paddingLeft="15dp"
android:background="@color/list_item_bg">
<TextView
android:id="@+id/tv_week"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="WEEK"
android:textSize="18sp"
android:textColor="@color/white"
android:layout_centerVertical="true"/>
<ImageView
android:id="@+id/star_icon"
android:layout_toRightOf="@+id/tv_week"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@mipmap/ic_launcher"
android:layout_marginLeft="20dp"
android:layout_centerVertical="true"/>
<TextView
android:id="@+id/tv_score"
android:layout_toRightOf="@+id/star_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="50"
android:textSize="18sp"
android:textColor="@color/white"
android:layout_marginLeft="20dp"
android:layout_centerVertical="true"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_alignParentRight="true"
android:gravity="center_vertical">
<TextView
android:id="@+id/tv_challengeCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="18sp"
android:textColor="@color/white"
android:layout_centerVertical="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/"
android:textSize="18sp"
android:textColor="@color/white"
android:layout_centerVertical="true" />
<TextView
android:id="@+id/tv_total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7"
android:textSize="18sp"
android:textColor="@color/white"
android:layout_centerVertical="true"
android:layout_marginRight="20dp"/>
<TextView
android:id="@+id/tv_arrow_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=">"
android:textSize="18sp"
android:textColor="@color/white"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"/>
</LinearLayout>
</RelativeLayout>
<View
android:id="@+id/challenges_bottom_border"
android:layout_below="@+id/challenges_row_item"
android:layout_width="match_parent"
android:layout_height="1.5dp"
android:background="@color/black" />
<RelativeLayout
android:id="@+id/challenges_sub_rel"
android:layout_below="@+id/challenges_row_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone">
<ListView
android:id="@+id/sub_listview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</RelativeLayout>
</RelativeLayout>
Recyclerview onclick
Здесь я хочу увидеть ListView и изменить цвета его TextViews на основе позиций, сохраненных в массиве positions_list
.
holder.challenges_row_item.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//visible sub listview
if(holder.challenges_sub_rel.getVisibility() == View.VISIBLE) {
holder.challenges_sub_rel.setVisibility(View.GONE);
//holder.tv_challengeCount.setText(String.valueOf(COUNT));
}
else {
holder.challenges_sub_rel.setVisibility(View.VISIBLE);
final String[] desc = context.getResources().getStringArray(R.array.challenges_sub_rv_desc);
final String[] challenge_total = context.getResources().getStringArray(R.array.challenge_total);
String[] challenge_scores = context.getResources().getStringArray(R.array.challenge_scores);
ChallesgesSubListViewAdapter adapter = new ChallesgesSubListViewAdapter(context,
R.layout.challenges_sub_rv_item, desc, challenge_total, challenge_scores);
holder.sub_listview.setAdapter(adapter);
for(int i=0; i<holder.sub_listview.getCount(); i++) {
Здесь я хочу получить доступ ко всем элементам строки ListView, но он не обновляет цвет текста.
if(positions_list.get(i) <= holder.sub_listview.getLastVisiblePosition()){
View old_view = holder.sub_listview.getChildAt(positions_list.get(i) - holder.sub_listview.getFirstVisiblePosition());
TextView desc_tv = (TextView) old_view.findViewById(R.id.desc_tv);
TextView challenge_count_tv = (TextView) old_view.findViewById(R.id.challenge_count_tv);
TextView slash_tv = (TextView) old_view.findViewById(R.id.slash_tv);
TextView challenge_total_tv = (TextView) old_view.findViewById(R.id.challenge_total_tv);
TextView scores_tv = (TextView) old_view.findViewById(R.id.scores_tv);
ImageView tick_icon = (ImageView) old_view.findViewById(R.id.tick_icon);
desc_tv.setTextColor(context.getResources().getColor(R.color.gray));
challenge_count_tv.setTextColor(context.getResources().getColor(R.color.gray));
slash_tv.setTextColor(context.getResources().getColor(R.color.gray));
challenge_total_tv.setTextColor(context.getResources().getColor(R.color.gray));
scores_tv.setTextColor(context.getResources().getColor(R.color.gray));
scores_tv.setVisibility(View.GONE);
tick_icon.setVisibility(View.VISIBLE);
//update count tv
challenge_count_tv.setText(challenge_total_tv.getText());
}
}
Скажите, пожалуйста, где я делаю ошибку.