2013-04-02 82 views
0

我有一個非常簡單的程序,實現一個ListView。當我在列表中選擇一個項目時,它會短暫地變成橙色,然後恢復爲黑色。不過,我想它留橙後,我選擇項(直到我清除的項目,或選擇不同的項目)。我試圖編碼但失敗。我認爲它必須接近正確。任何人都可以告訴我需要修改以下代碼才能使其工作?的ListView:所選項目應突出

文件:./res/layout/main.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:gravity="center"> 
    <ListView 
     android:id="@android:id/list"   android:layout_width="match_parent" 
     android:layout_height="match_parent" android:background="@drawable/list_selector"/> 
</LinearLayout> 

文件:./res/drawable/list_selector.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_selected="true"   android:drawable="@color/orange" /> 
    <item android:state_selected="false"  android:drawable="@android:color/black" /> 
</selector> 

文件:./res/values/colors。 XML

<?xml version="1.0" encoding="utf-8"?> 
    <resources> <color name="orange">#b0e0e6</color> </resources> 

文件:./src/com/commonsware/android/linearpct/LinearLayoutDemo.java

public class LinearLayoutDemo extends ListActivity implements OnItemClickListener { 
    private static final String[] items={"1", "2", "4", "8", "16", "32", "64", "128", "256", "512", "1024", "2048", "4096", "8192"}; 
    ListView myLV; 
    ArrayAdapter myAdapter; 

    @Override public void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 
     setContentView(R.layout.main); 
     Log.d("LinearLayoutDemo:", "********: onCreate() begin"); 
     getListView().setOnItemClickListener(this); 
     myAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items); 
     myLV = (ListView) findViewById(android.R.id.list); 
     myLV.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
     myLV.setAdapter(myAdapter); 
    } 

    @Override public void onItemClick(AdapterView<?> arg0, View view, int position, long lng) { 
     view.setSelected(true); 
     String selectedFromList =(String) (myLV.getItemAtPosition(position)); 
     Log.d("LinearLayoutDemo:", "********: OnItemClick: " + selectedFromList); 
    } 

    @Override public void onListItemClick(ListView parent, View view, int position, long id) { 
     super.onListItemClick(parent, view, position, id); 
     view.setSelected(true); 
     String selectedFromList =(String) (myLV.getItemAtPosition(position)); 
     Log.d("LinearLayoutDemo:", "********: OnItemClick: " + selectedFromList); 
    } 
} 

注:當我運行此代碼,從onListItemClick日誌消息()從來沒有出現在日誌中。那裏有什麼不對。

另外,我無法弄清楚如何調用super.onItemClick()在onItemClick()。這可能是一個線索。

回答

2

在list_selector.xml:

<item android:state_selected="true" android:drawable="@color/orange" /> 

當物體被聚焦與d-PAD稱爲(軌跡板)

<item android:state_activated="true" android:drawable="@color/orange" /> 

當在上下文操作欄中選擇對象時調用。 (僅API 11+)

在API

11-實施多的最好辦法是選擇自定義適配器,改變視圖bakground顏色。

class CPGAdapter extends ArrayAdapter<Pack> { 
public void UpdateItem() { 
    notifyDataSetChanged(); 
} 

@Override 
public View getView(int position, View v, ViewGroup parent) { 
    View mView = v; 
    if (mView == null) { 
     LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
       Context.LAYOUT_INFLATER_SERVICE); 
     mView = vi.inflate(R.layout.cpga_xml, null); 
    } // inflate 
    TextView text = (TextView) mView.findViewById(R.id.tv_cpgatitle); 
    // get views from ids 
    if (mView != null) { 
     if (getItem(position).Selected) { 
      mView.setBackgroundResource(R.color.pressed); 
     } else { 
      mView.setBackgroundResource(R.color.default); 
     } 
     text.setText(getItem(position).Name); 

    } 
    return mView; 
} 

    } 

,並在您的代碼時選擇視圖:

public void onItemClick(AdapterView<?> AV, View v, int POS, long Id) { 
       ((Pack) AV.getItemAtPosition(POS)).Selected = true; 
       v.setBackgroundResource(R.color.pressed); 
            // or (CPGAdapter(AV.getAdapter)).UpdateItem() 
} 
2

變化simple_list_item_1simple_list_item_activated_1。顏色非常難看,如果你想要漂亮的顏色,你必須擴展ArrayAdapter。

public class LinearLayoutDemo extends ListActivity implements OnItemClickListener { 
    private static final String[] items={"1", "2", "4", "8", "16", "32", "64", "128", "256", "512", "1024", "2048", "4096", "8192"}; 
    ListView myLV; 
    ArrayAdapter myAdapter; 

@Override public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    setContentView(R.layout.main); 
    Log.d("LinearLayoutDemo:", "********: onCreate() begin"); 

    myAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_activated_1, items); 
    myLV = (ListView) findViewById(android.R.id.list); 
    myLV.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
    myLV.setAdapter(myAdapter); 
    myLV.setOnItemClickListener(this); 
} 

@Override public void onItemClick(AdapterView<?> arg0, View view, int position, long lng) { 

    String selectedFromList =(String) (myLV.getItemAtPosition(position)); 
    Log.d("LinearLayoutDemo:", "********: OnItemClick: " + selectedFromList); 
} 


} 

你可以刪除list_selector.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:gravity="center"> 
<ListView 
    android:id="@android:id/list"   android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 
</LinearLayout> 
+0

謝謝,以後會試試這個。我會讓你知道它是怎麼回事。 –

+0

這不起作用。它沒有任何區別。 –

+0

爲什麼人們發佈不起作用的答案?他們爲他們工作嗎? –