【问题标题】:Listview - list item selection and initial setting (custom made items)Listview - 列表项选择和初始设置(定制项)
【发布时间】:2015-05-23 05:59:45
【问题描述】:

很好地使用自定义形状列表项!

问题 1:如何为列表项使用自定义形状?

问题 2:如何显示选择了哪个列表项?

问题3:如何初始设置列表项?

编辑:经过长时间的搜索,我(在朋友的帮助下)找到了答案。我把它们写在“下面”的答案中。

尽情享受吧!

【问题讨论】:

    标签: android android-layout android-listview


    【解决方案1】:

    最后(在朋友的帮助下)我找到了所有答案。 该代码适用于许多版本。漫长的搜索到此结束,希望对您有所帮助!

    答案 1:您的列表项的自定义布局很好。 Customshape 和 customshape_pressed。改变它们,这只是一个例子。

    答案 2:单击列表项后,显示自定义形状按下的颜色。

    答案 3:设置初始列表项。是的,请参阅“布丁的证明”。

    如果您遇到任何问题,请使用 alpha 绘制颜色(如本例所示)。当然你会问:为什么同时使用 listselector 和 list_item_background ...我们只说 ...这是我们找到的最佳方式。

    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

    <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

    <?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的drawable文件: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

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

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

    还有customhap_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】:

      我在您的代码中发现了一些不需要的行尝试删除它们

      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");
          }
          });
      
      • 我已评论adapter.notifyDataSetChanged();,因为在设置适配器后ListView 中的项目没有任何变化。
      • 我评论了listview.invalidate();,因为把ListView 弄脏是没有用的
      • 我删除了view.setSelected(true); 行,因为它会在内部完成,因为它会在外部更改视图状态。

      【讨论】:

      • 调用 view.setSelected(true) 不是一个好主意;尝试为 xml 中的列表视图添加 android:listSelector="@drawable/list_selector"。
      • 在列表视图中,您不能将项目设置为默认值。
      • 可以将一个项目设置为默认值,只要我使用简单的白色背景。见下文。这段代码我用了几个月:## m_listview.setItemChecked(listCurrentSelectedPosition, true); ## m_listview.setSelection(listCurrentSelectedPosition);那时我不使用任何选择器。这不适用于定制的背景代码。我不知道为什么
      猜你喜欢
      • 1970-01-01
      • 2018-04-28
      • 1970-01-01
      • 1970-01-01
      • 2019-08-21
      • 1970-01-01
      • 2016-01-22
      • 1970-01-01
      • 2014-03-22
      相关资源
      最近更新 更多