问题
Working nicely with custom shape list items!
Question 1: how can I work with custom shapes for list items?
Question 2: how can I show which list item is selected?
Question 3: how can I initially set a list item?
EDIT: after a long search I found (with the help of a friend) the answers. I wrote them in the 'below' Answer.
ENJOY !!
回答1:
Finally (with the help of a friend) I found all answers. The code works across many versions. This ends a long search, I hope this is of help for you!
Answer 1: nice custom layout of your list items. Customshape and customshape_pressed. Change them, this is just an example.
Answer 2: after clicking on a list item, the customshape pressed colour is shown.
Answer 3: setting an initial list item. Yes, see the 'proof of the pudding'.
If you face any problems, draw the color with an alpha (as in this example). Of course you ask: why using both the listselector and list_item_background ... we only say ... this is the best way we found.
1 Main App:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView( R.layout.activity_main);
List<String> items = Arrays.asList( "First", "Two", "Three", "Four", "Five", "Six", "Seven");
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>( this, R.layout.string_entry_v2, items);
m_listView = (ListView) findViewById( R.id.list_2);
m_listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
m_listView.setAdapter(adapter2);
m_listView.setOnItemClickListener( new OnItemClickListener() {
@Override
public void onItemClick( AdapterView<?> parent, View view, int position, long id) {
Log.v( "List2", "Clicked");
}
});
// ==== SOLVING question 2: setting a default row - WITH feedback
m_listView.setItemChecked(2, true);
}
2 The layout files: activity_main.xml
<LinearLayout 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="com.example.listselectorexample2.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List selector" />
<ListView
android:id="@+id/list_2"
android:layout_width="match_parent"
android:layout_height="150dp"
android:listSelector="@drawable/row_selector"
android:scrollbars="vertical" />
</LinearLayout>
And the list-item entry: string_entry_v2.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="demo text"
android:background="@drawable/row_item_background" />
3 The drawable files for listselector: row_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_enabled="true" android:state_pressed="true"
android:drawable="@drawable/customshape_pressed" />
<item android:state_activated="true"
android:drawable="@drawable/customshape_pressed" />
<item android:drawable="@drawable/customshape" />
</selector>
And ... for the row: row_item_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_activated="true"
android:drawable="@drawable/customshape_pressed" />
<item android:drawable="@drawable/customshape" />
</selector>
4 The custom shapes for the list items: customshape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#77ececc9"
android:endColor="#77ececc9"
android:angle="270"/>
<corners
android:bottomRightRadius="15dp"
android:bottomLeftRadius="15dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp"/>
</shape>
And the customshap_pressed.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#b6dde4"
android:endColor="#b6dde4"
android:angle="270"/>
<corners
android:bottomRightRadius="15dp"
android:bottomLeftRadius="15dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp"/>
</shape>
回答2:
I found some unwanted lines in your code try to remove them
List<String> items = Arrays.asList( "First", "Two", "Three", "Four");
ArrayAdapter<String> adapter = new ArrayAdapter<String>( this, R.layout.string_entry, items);
ListView listview = (ListView) findViewById( R.id.the_list);
listview.setAdapter( adapter);
//adapter.notifyDataSetChanged();
//listview.invalidate();
listview.setOnItemClickListener( new OnItemClickListener() {
@Override
public void onItemClick( AdapterView<?> parent, View view, int position, long id) {
// view.setSelected(true);
Log.v( "List1", "Clicked");
}
});
- I have commented
adapter.notifyDataSetChanged();
because there are no changes to the items in theListView
after the adapter has been set. - I have commented
listview.invalidate();
because there is no use to make theListView
as dirty - I have removed the line
view.setSelected(true);
because there is changing the view state externally because it will done internally.
来源:https://stackoverflow.com/questions/30409421/listview-list-item-selection-and-initial-setting-custom-made-items