2013-06-02 38 views
5

我在我的應用程序的代碼,點擊它時使用了AdapterView.OnItemClickListener檢測點擊一個ListView。問題是,當我點擊一個項目時,該項目的背景會變成白色,而不是默認的橙色。就像這樣:enter image description here安卓:在列表中的項目點擊時不會變成橙色?

此外,當我不使用適配器視圖,單擊的項目變成橘紅色,沒有任何問題。如何讓點擊項目的背景變成橙色?

編輯:列表

佈局:main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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" 
    android:orientation="vertical" 
    tools:context=".MainActivity" 
    android:background="#00000000"> 


     <!-- ListView (grid_items) --> 

     <ListView android:id="@+id/listview" 
      android:layout_height="fill_parent" 
      android:textSize="15px" 
      android:layout_width="fill_parent" 
      android:background="#00000000"> 
     </ListView> 

</RelativeLayout> 

的onCreate():

public void onCreate(Bundle savedInstanceState) {try{ 
    super.onCreate(savedInstanceState); 


    setContentView(R.layout.main); 
    lv= (ListView)findViewById(R.id.listview); 
    lv.setBackgroundColor(Color.TRANSPARENT); 
    lv.setCacheColorHint(Color.TRANSPARENT); 
    //......calculations 
    for(int q = 0; q <v; q++){ 
     HashMap<String, String> map = new HashMap<String, String>(); 
     map.put("col_1", array[q]); 
     fillMaps.add(map); 
     lv.setOnItemClickListener(onListClick); 
    } 
    //......calculations 
    } 

適配器視圖:使用

private AdapterView.OnItemClickListener onListClick=new AdapterView.OnItemClickListener(){ 

public void onItemClick(AdapterView<?> parent,View view, int position, long id) 
{ 

lv.setBackgroundColor(Color.TRANSPARENT); 
lv.setCacheColorHint(Color.TRANSPARENT); 
//.....calculations 
} 

自定義主題:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="CustomWindowTitleBackground"> 
     <item name="android:background">#323331</item> 
    </style> 

    <style name="CustomTheme" parent="android:Theme"> 
     <item name="android:windowTitleSize">35dip</item> 
     <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item> 
    </style> 
</resources> 
+1

嗨,如何發佈您正在使用的代碼? – slevon

+1

什麼是當前使用的主題? –

+0

@slevon新增.... –

回答

8

自定義列表視圖中的項目,最好的辦法是在一個XML文件來創建它們,然後使用一個BaseAdapter或任何種類列表適配器,該充氣.XML佈局和與您的數據填滿。爲了定製的一大亮點的動畫,你需要的是創建一個項目的不同狀態的狀態可繪製。

所以你去「新的XML文件」 - >「資源類型繪製」 - >「形狀」的名稱,並完成 你會看到這段代碼:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" > 


</shape> 

讓我們創建一個背景一個ListItem按壓狀態:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle" > 

    <solid android:color="#86d47f" this is your custom color /> 

</shape> 

和非按壓狀態是另一個文件:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle" > 

    <solid android:color="#00000000" /> 

</shape> 

然後列表選擇器文件將被用作列表視圖項目的背景: 轉到「新的xml文件」 - >「繪製」 - >「列表選擇器」將其命名爲「item_background」 - >完成

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="300" !!this is the fade duration time for your animation> 

    <item android:drawable="@drawable/bg_drawable" android:state_pressed="true"></item> 
    <item android:drawable="@drawable/transparend"></item> 

</selector> 

然後創建一個項目一個XML文件,定製你想要的,但對於主要佈局設置

android:background="@drawable/item_background" 

在這裏你去,一切都運行完美... 這裏是我的適配器類:

public class ListAdapter extends ArrayAdapter<String> { 
LayoutInflater inflater; 

public ListAdapter(Context context, int textViewResourceId, 
     List<String> objects) { 
    super(context, textViewResourceId, objects); 
    inflater = LayoutInflater.from(context); 

} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    String str_value = this.getItem(position); 
    if (convertView == null) { 
     convertView = inflater.inflate(R.layout.list_item, null); 
    } 
    TextView tv = (TextView) convertView.findViewById(R.id.textView1); 
    tv.setText(str_value); 
    return convertView; 
} 

} 

和你在這裏,你應該將其標記爲一個答案.. list

+0

在行'',android默認橙色的十六進制是什麼? –

+0

Alex

+0

Alex

0

這樣一個簡單的解決方案是STE在onClicklistenener本身查看istself的顏色:

private AdapterView.OnItemClickListener onListClick=new 
AdapterView.OnItemClickListener(){ 

public void onItemClick(AdapterView<?> parent,View view, int position, long id) 
{ 

    lv.setBackgroundColor(Color.TRANSPARENT); 
    lv.setCacheColorHint(Color.TRANSPARENT); 
    // I added this 
    if(is the item checked){ 
     view.setBackgroundColor(Color.BLUE); //or whatever 
    }else{ 
     view.setBackgroundColor(00000000); 
    } 
    //.....calculations 
} 

你需要掌握的氣象信息的項目被選中與否。

如果你不想使用onClickListener但只使用風格(這可能是最好的方式)做什麼,我不能幫你furher的時刻。

編輯:順便說一下我不明白爲什麼YOUT每次都做到這一點在日onClickListener:

lv.setBackgroundColor(Color.TRANSPARENT); 
lv.setCacheColorHint(Color.TRANSPARENT); 

希望這有助於反正。

+0

感謝您的回答。如何檢查項目是否被選中?和'view.setBackgroundColor()'中的視圖是什麼? –

+1

_view_是被acically點擊的ListItem。關於如何檢查項目是否被選中,請嘗試閱讀:http://stackoverflow.com/questions/8463373/android-cant-get-listview-item-selected – slevon

+0

有一個問題。默認的橙色突出顯示實際上是一種動畫,而不是一種顏色。我想指出一件事:看問題中的截圖。選擇「阿拉巴馬州」,其背景爲白色,但看看「阿拉巴馬州」的頂部邊緣,可見一些橙色。這可能是點擊動畫!這是否可以告訴你什麼可能是錯的? –

0

試試這個設置任何自定義選擇:

在「styles.xml」創建此風格:

<style name="MyList"> 
     <item name="android:drawSelectorOnTop">true</item> 
     <item name="android:listSelector">@drawable/my_list_selector</item> 
</style> 

而在你的佈局只是這種風格添加到ListView:

<ListView 
.... 
style="@style/MyList" 
.... /> 

而且在「繪製」你可以定義「MY_ list_selector」你想要的顏色或圖片:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:state_pressed="true"> 
     <shape> 
      <solid android:color="@color/state_pressed" /> 
     </shape> 
    </item> 
    <item android:state_focused="true"> 
     <shape> 
      <solid android:color="@color/state_focused" /> 
     </shape> 
    </item> 
    <item android:drawable="@android:color/transparent" /> 

</selector> 

添加這些顏色值設爲您的‘colors.xml’:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <color name="state_pressed">#BB7dbcd3</color> 
    <color name="state_focused">#777dbcd3</color> 
    <color name="transparent">#00000000</color> 
</resources> 
0

我有一個名爲list_selector_background_longpress.9在我的繪製文件夾,裏面有一個白色在上面的橙色線的照片。所以我用holo_orange_dark顏色的圖片改了它,並且解決了它。對於這樣的問題,這是一個愚蠢簡單的解決方案。

相關問題