【问题标题】:How to highlight ListView-Items如何突出显示 ListView-Items
【发布时间】:2011-05-23 16:24:58
【问题描述】:

我遇到了以下问题。

我有一个 ListView,其中包含一个由 imageview 和一个 textview 组成的自定义行。 textview的xml代码是

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="26px"
    android:layout_marginLeft="3px"
    android:singleLine="true"
    android:ellipsize="end"
    android:textColorHighlight="#FEC403"
/>

然后我有一个工作正常的 itemclicklistener,我想通过执行以下操作突出显示已单击的文本视图。

public void onItemClick(AdapterView<?> adaptview, View clickedview, int position,
                long id) {
            //TODO: ACTIONS
            String pathtofile = (String) adaptview.getItemAtPosition(position);
            View rowview = (View) adaptview.getChildAt(position);
            rowview.setSelected(true);}

我希望 xml 中的突出显示颜色为“#FEC403”(即浅橙色),但突出显示颜色仍然是灰色。那么如何正确设置highlightcolor呢?

提前致谢

编辑:

这是我最后的做法:

这是我的 ListView 项目 xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/rowselector"
>

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/musicicon"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:src="@drawable/musicicon"
    android:paddingLeft="3px"
/>


<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="26px"
    android:layout_marginLeft="3px"
    android:singleLine="true"
    android:ellipsize="end"
    android:focusable="false"
/>

和rowselector.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" />
</selector>

最后我的 OnItemClick 现在很短了:

@Override
public void onItemClick(AdapterView<?> adaptview, View clickedview, int position,
                long id) {
            //TODO: ACTIONS
            clickedview.setSelected(true);
}

【问题讨论】:

  • Android ListView Selector Color 的可能重复项
  • 不幸的是,这种技术不能用于突出显示列表视图中的多个项目,因为一次只能选择一个项目。

标签: android highlight listviewitem


【解决方案1】:
@Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long arg3)
        {
            for(int a = 0; a < parent.getChildCount(); a++)
            {
                parent.getChildAt(a).setBackgroundColor(Color.BLACK);
            }

            view.setBackgroundColor(Color.RED);

        }

【讨论】:

    【解决方案2】:

    您应该使用Selector

    This 问题及其答案可能会有所帮助....

    【讨论】:

      【解决方案3】:

      在您的活动中:

      listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView<?> parent, View view, int position, long itemid) {
                  itemOnclickList(position, view);
              }
          });
      
          public void itemOnclickList(int position, View view) {      
          if (listview.isItemChecked(position)) {                           
             view.setBackgroundDrawable(getResources().getDrawable(R.drawable.image_checked));            
          } else {    
          view.setBackgroundDrawable(getResources().getDrawable(R.drawable.image_uncheck));           
          }
          adapter.notifyDataSetChanged();
      
      }
      

      在您的适配器中:

        public View getView(int position, View view, ViewGroup parent) {
          View convertView = inflater.inflate(R.layout.listdocument_item, null);      
      
              ListView lvDocument = (ListView) parent;
              if (lvDocument.isItemChecked(position)) {
                  convertView.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.image_checked));               
              } else {
                  convertView.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.image_uncheck));               
              }
      
          return convertView;
      }
      

      祝你好运!

      【讨论】:

        【解决方案4】:

        这是使用选择器的外观:

        在drawable中创建一个名为selector_listview_item.xml的文件,看起来像(将颜色更改为您自己的颜色):

        <?xml version="1.0" encoding="utf-8"?>
        <selector xmlns:android="http://schemas.android.com/apk/res/android">
            <!-- pressed -->
            <item android:drawable="@color/md_grey_200" android:state_pressed="true" />
            <!-- default -->
            <item android:drawable="@color/white" />
        </selector>
        

        现在在描述列表行的布局中(例如 layout_list_row.xml),在根布局上添加 android:background="@drawable/selector_listview_item",因此布局看起来像:

        <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="@drawable/selector_listview_item" <!-- this is the important line -->
            android:orientation="vertical">
        
            <!-- more stuff here -->
        </LinearLayout>
        

        (注意:您可能希望在该列表视图行中的项目上添加android:focusable="false",以使该行可点击,如here 所述)

        完成。

        【讨论】:

          猜你喜欢
          • 2022-10-16
          • 1970-01-01
          • 2013-02-04
          • 2012-01-22
          • 1970-01-01
          • 1970-01-01
          • 2016-04-10
          • 2012-01-23
          • 1970-01-01
          相关资源
          最近更新 更多