简体   繁体   中英

Change background color of ListView item from Fragment

I have a Fragment that is implementing a ListView . I have a global reference of this ListView in the Fragment class and i want to change the background color of the ListView item in updateStep(int position) method. I don't want to change the color of every item in the list. I am doing some processing in the Fragment and i want to change the color of the ListView item accordingly.

My updateStep method is simple:

public void updateStep(int position) {
        if(stepsListView != null)
             stepsListView.getChildAt(position).setBackgroundColor(Color.GREEN);    
}

But the getChildAt(position) is returning null.What is the other way to get the item and update the color of an item.

The list_item.xml looks like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:padding="8dp">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="@dimen/standard_text_size"
        android:textColor="@color/gray"
        android:text="@string/about_text_default"
        android:id="@+id/text_list_item">
    </TextView>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="16sp"
        android:textColor="@color/gray"
        android:text="@string/about_text_default"
        android:id="@+id/test_list_item_description">
    </TextView>
</LinearLayout>

and the fragment (that has a list) looks like this:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.carmanah.views.fragments.TestListFragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/text_heading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:textColor="@color/gray"
            android:textStyle="bold"
            android:textSize="@dimen/standard_heading_text_size"
            android:text="@string/heading_text_tests">

        </TextView>

        <ListView
            android:id="@+id/task_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:minHeight="@dimen/standard_text_size"
            android:divider="@color/gray"
            android:dividerHeight="2dip">

        </ListView>
    </LinearLayout>
</FrameLayout>

PS I am using CustomArrayAdapter for this list.

You can get only visible View from ListView because row views in ListView are reuseable. So, you should control the item position.

The below method helps you to get the view.

public View getViewByPosition(int pos, ListView listView) {
    final int firstListItemPosition = listView.getFirstVisiblePosition();
    final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;

    if (pos < firstListItemPosition || pos > lastListItemPosition ) {
        return listView.getAdapter().getView(pos, null, listView);
    } else {
        final int childIndex = pos - firstListItemPosition;
        return listView.getChildAt(childIndex);
    }
}

You can change background color of the item like this:

public void updateStep(int position) {
        if(stepsListView != null)
             getViewByPosition(position, stepsListView).setBackgroundColor(Color.GREEN);   
}

The other way to get the item & update the color of the same is by assigning the color in the getView method of your Custom Array Adapter. Juts make a Relative/Linear Layout having all the components and assign the color using the following statement:-

viewHolder.lin_item.setBackgroundColor(getResources().getColor(R.color.white));

where lin_item is the Liner Layout inside the ViewHolder & you can assign the color according to any condition of your choice.

The getView method already has position, so no need to worry about that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM