【问题标题】:Set a background color to a selected ListView Item in android在android中为选定的ListView项设置背景颜色
【发布时间】:2011-08-28 15:45:21
【问题描述】:

我有一个显示多个项目的列表视图。现在我想滚动到某个特定项目(例如第 33 个项目)。我知道这可以通过

myList.setSelection(32);

但在 UI 上,该项目没有收到任何突出显示(因为它处于触摸模式?!)。如何为该项目应用特定的背景颜色?我试过了

myList.getSelection().getSelectedView().setBackgroundColor(Color.Red);

但我得到一个 NullPointerException,因为 getSelectedView() 返回 null。有没有办法实现这种突出显示?我必须以某种方式通知用户哪个项目是“活动”项目...

【问题讨论】:

    标签: android listview android-emulator selection android-listview


    【解决方案1】:

    可能最简单的方法是使用 State List Drawable 作为背景。这使您可以根据相关控件是否被选中/聚焦/禁用/等来应用不同的样式。

    有关 State List Drawables 的介绍,请查看 this article

    【讨论】:

      【解决方案2】:

      换行

      myList.getSelection().getSelectedView().setBackgroundColor(Color.Red);
      

      myList.getSelectedView().setBackgroundColor(getResources().getColor(Color.RED));
      

      【讨论】:

      【解决方案3】:

      我通过以下方式尝试并解决了问题。

      创建了一个接收位置的javaBean 类。喜欢以下。

      public class Global {
      
          public static int mListPosition = 0;
      
          public static int getListPosition() {
              return mListPosition;
          }
      
          public static void setListPosition(int mListPosition) {
              Global.mListPosition = mListPosition;
          }
      }
      

      然后从OnListItemClickListener()我设置选中项的位置。喜欢关注

      mList.setOnItemClickListener(new OnItemClickListener() {
      
          public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
              Global.setListPosition(arg2);                   
          }
      });
      

      然后在适配器类中执行以下操作

      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          View v = convertView;
          if (v == null) {
              LayoutInflater vi = (LayoutInflater) mCtx
                      .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              v = vi.inflate(R.layout.details_retailer_list_row, null);
      
              mHolder = new ViewHolder();
              v.setTag(mHolder);
      
              mHolder.mDetailsRetailerLayout = (LinearLayout) v
                      .findViewById(R.id.details_cart_retailer_row_layout);
              mHolder.mDetailsRetailer = (TextView) v
                      .findViewById(R.id.detailsRetailerRow);
      
          } else {
              mHolder = (ViewHolder) v.getTag();
          }
      
          final CartDetailsRetailerBean mDetailsRetailerBean = mItems
                  .get(position);
          if (position == Global.getListPosition()) {
              mHolder.mDetailsRetailerLayout
                      .setBackgroundResource(R.drawable.image1);
          } else {
              mHolder.mDetailsRetailerLayout
                      .setBackgroundResource(R.drawable.image2);
          }
      
          if (mDetailsRetailerBean != null) {
              Log.i("Global Position", "" + Global.getListPosition());
              mHolder.mDetailsRetailer.setText(mDetailsRetailerBean
                      .getRetailerName());
          }
          return v;
      }    
      

      试试这个来改变 Android 列表视图中的选定行背景颜色变化。

      【讨论】:

        【解决方案4】:
        1. 要突出显示选定的项目,您应该为 listView 请求焦点并在 UI 线程中运行 setSelection()。这对我有用:
        runOnUiThread(new Runnable() {
            public void run() {
                myList.requestFocus();
                myList.setSelection(position);
            }
        });
        

        2.将特定颜色应用于项目。您可以使用自定义的 listItem。

        一个。为您的 listView 设置自定义的 lisItem:

        ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, R.layout.customizedlistitem,arrListView);
        myList.setAdapter(listAdapter);
        

        b.在你的布局文件夹中,创建customizedlistitem.xml

        <TextView xmlns:android="http://schemas.android.com/apk/res/android"  
            android:id="@+id/customizedlistitem"   
            android:layout_width="match_parent"   
            android:layout_height="wrap_content"
            android:background="@drawable/customizedbackground"/>   
        

        c。在你的drawable文件夹中,像这样创建customizedbackground.xml:

        <selector xmlns:android="http://schemas.android.com/apk/res/android">
            <item android:state_selected="true" android:state_pressed="false" android:drawable="@color/RED"></item>
            <item android:state_pressed="true" android:drawable="@color/GREEN"></item>      
            <item android:drawable="@color/BLACK"></item>   <!-- for other state -->        
        </selector>
        

        d。确保在您的项目中定义了颜色 RED、GREEN 和 BLACK(您可以在 values 文件夹下的 color.xml 中定义它):

        <?xml version="1.0" encoding="utf-8"?>
        
        <resources>  
             <color name="RED">#FF0000</color>
             <color name="GREEN">#008000</color>
             <color name="BLACK">#000000</color>
        </resources>
        

        【讨论】:

          【解决方案5】:

          nullPointerException 可能是因为您处于触摸模式,当您处于触摸模式时,setSelection 并没有真正选择 listView 上的项目,它只是“查看”它,如果您知道我的意思的话。

          你必须做一个定制的适配器(通过创建一个扩展你当前使用的适配器的类:])

          并且在您的自定义适配器类中,您必须重写 getView(int position, View convertView, ViewGroup parent) 方法,如下所示:

          @Override
          public View getView(int position, View convertView, ViewGroup parent) {
              if (convertView == null)
                  convertView = inflater.inflate(android.R.layout.simple_list_item_1, null);
              String content = lista.get(position).toString();
              TextView text = (TextView) convertView.findViewById(android.R.id.text1);
              text.setText(content);
              convertView.setBackgroundColor(lista.get(position).getBackgroundColor());
              return convertView;
          }
          

          在代码中,变量“lista”是我的适配器上的一些项目的列表。

          因此,当您选择完您的项目后,您必须为您拥有的每个项目调用 getView,或者您可以从 listView before 调用方法 invalidateViews 来设置选择(如果您选择后调用 invalidateViews ,选择将被忽略(至少在 touchmode :] ))

          【讨论】:

            【解决方案6】:

            试试这个,它会工作-

            myList.getChildAt(myList.getSelectedItemPosition()).setBackgroundColor(Color.RED);
            

            【讨论】:

              【解决方案7】:

              xml文件中的ListView:

              <ListView android:id="@+id/android:list" android:layout_width="fill_parent"
                      android:layout_height="wrap_content" 
                      android:layout_below="@+id/Tablayoutdesign"
                      android:cacheColorHint="#000000"
                      android:dividerHeight="1dip"
                      android:layout_marginTop="63dip"
                      android:layout_marginBottom="40dip"
              
                      />
              

              使用 listselector 名称创建新的 xml,例如:

              <?xml version="1.0" encoding="utf-8"?>
              <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
              
                <!-- Selected --> 
                <item 
                  android:state_focused="true" 
                  android:state_selected="false" 
                  android:drawable="@drawable/focused"/> 
              
                <!-- Pressed -->
                <item 
                  android:state_selected="true" 
                  android:state_focused="false"
                  android:drawable="@drawable/selected" /> 
              
              </selector>
              

              创建 colors.xml 文件,如:

              <resources>
                  <drawable name="focused">#ff5500</drawable>
                  <drawable name="selected">#FF00FF</drawable>
              </resources>
              

              在您的 java 代码中:

              ListView lv= (ListView) findViewById(R.id.list);
              lv.setSelector( R.drawable.listselector);
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2013-08-06
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2013-11-06
                • 2011-10-26
                • 2014-03-21
                相关资源
                最近更新 更多