2016-09-14 139 views
-1

我有一個Fragment正在實施ListView。我在Fragment類中擁有此ListView的全局參考,並且我想要更改updateStep(int position)方法中ListView項目的背景顏色。我不想更改列表中每個項目的顏色。我正在片段中進行一些處理,我想相應地更改ListView項目的顏色。從片段中更改ListView項目的背景顏色

updateStep方法很簡單:

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

getChildAt(position)正在恢復null.What是其他方式獲得該項目,並更新項目的顏色。

的list_item.xml看起來是這樣的:

<?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> 

和片段(具有列表)看起來像這樣:

<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> 

附:我爲此清單使用CustomArrayAdapter

回答

0

獲取項目&的另一種方式是更新顏色,方法是在自定義數組適配器的getView方法中指定顏色。伸出做出一個相對有/線性佈局中的所有組件和使用下面的語句指定顏色: -

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

其中lin_item是ViewHolder &您可以根據您所選擇的任何條件,指定顏色裏面的內膽佈局。

getView方法已經有了位置,所以不需要擔心。

+0

我不想改變每個項目的顏色列表中。我在Fragment中做了一些處理,我想相應地改變ListView項目的顏色。對不起,我以前沒有提到它。 –

+0

您可以在getView方法中執行處理,因爲您無法從此方法外部修改ListView中每個項目的顏色。有一件事是可以的,你可以傳遞數組列表作爲參數在updateStep中,然後相應地運行一個迭代器。 –

1

由於ListView中的行視圖是可重用的,因此您只能從ListView中獲得可見的視圖。所以,你應該控制物品的位置。

下面的方法可以幫助您獲得視圖。

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

您可以更改項目的背景色是這樣的:

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

謝謝你的回覆。我可以通過你的方法得到視圖,但顯然'setBackgroundColor(Color.Green)'方法沒有反映變化。你認爲我需要調用一些通知方法或其他東西嗎? –

+0

嗯,在這一行後,可能repaint()或notifyDataSetChanged();方法可以用於它。但我不認爲這是必要的。 – ziLk

+0

這並不是僅僅使用'setBackgroundColor(Color.GREEN)'來反映改變。我試過'stepsListView'上的'invalidate()'和'((CustomAdapter)stepsListView.getAdapter())。notifyDataSetChanged()',但它也不起作用。 –