dimanche 7 octobre 2018

change randomly listview items textview color

I have a listview within a recyclerview. listview visibility is set to gone. On click of recyclerview row item listview will be visible.

In xml layout listivew row item textview color is white. But I want to randomly change listview row item textview color to gray after it is set to visible.

Please check out the code below:

Recyclerview row item 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>

Recyclerview onclick Here i want to visible listview and change colors of its textviews based on positions saved in arraylist "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++) {

here i want to access all listview row item stuff but its not updating text color.

        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());
        }
    }

Please tell me where i am doing misktake. Thank you.




Aucun commentaire:

Enregistrer un commentaire