列表视图 - 列表项的选择和初始设置(定制项目) [英] Listview - list item selection and initial setting (custom made items)

查看:209
本文介绍了列表视图 - 列表项的选择和初始设置(定制项目)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自定义形状列表项工作很好!

Working nicely with custom shape list items!

问题1:我怎么可以自定义形状工作列表项?

Question 1: how can I work with custom shapes for list items?

问题2:我怎么能显示哪些列表项被选中?

Question 2: how can I show which list item is selected?

问题3:我怎么能初始设置列表项?

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.

享受!

推荐答案

最后(与一个朋友的帮助下),我发现所有的答案。
在code在许多版本的作品。这结束长时间的搜索,我希望这是帮助你!

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!

答1:您的列表项的漂亮的自定义布局。 CUSTOMSHAPE和customshape_ pressed。改变他们,这只是一个例子。

Answer 1: nice custom layout of your list items. Customshape and customshape_pressed. Change them, this is just an example.

答2:单击列表项之后,CUSTOMSHAPE pressed颜色显示。

Answer 2: after clicking on a list item, the customshape pressed colour is shown.

答3:设置初始列表项。是的,看到路遥知马力。

Answer 3: setting an initial list item. Yes, see the 'proof of the pudding'.

如果您遇到任何问题,绘制一个字母的颜色(如本例)。当然,你会问:为什么同时使用listselector和list_item_background ......我们只能说......这是我们发现的最好方法。

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主要应用:

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的布局文件:activity_main.xml中

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>

和列表项条目:string_entry_v2.xml

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 listselector的绘制文件:row_selector.xml

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>

和...该行:row_item_background.xml

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列表项的自定义形状:customshape.xml

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> 

而customshap_ pressed.xml:

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> 

这篇关于列表视图 - 列表项的选择和初始设置(定制项目)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆