简体   繁体   English

Android自定义SimpleAdapter背景色

[英]Android Custom SimpleAdapter Background Color

I have been trying to implement getting the ListView Item Background Color to change based on its State using a Custom SimpleAdapter. 我一直在尝试使用自定义SimpleAdapter实现让ListView项目背景颜色根据其状态进行更改。 I was following code references found here and on other sites. 我一直在这里和其他站点上找到代码参考。 I have it partially working, but not completely. 我有部分工作,但不是全部。

First, my code: 首先,我的代码:
The XML for the Over-all GUI containing ListView: 包含ListView的总体GUI的XML:

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
      android:id="@+id/widget37"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:background="#ffffffff"
      xmlns:android="http://schemas.android.com/apk/res/android" 
   >

<ProgressBar
    android:id="@+id/progressBar"
    style="?android:attr/progressBarStyleLarge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" />

<TextView
    android:id="@+id/textViewMessage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="10dp"
    android:text="Building List..."
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#ff000000"
    android:textSize="22sp" />

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textViewMessage"
    android:layout_alignParentLeft="true"
    android:layout_marginTop="20dp">

    <ListView
        android:id="@id/listLocations"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="10dp"
        android:cacheColorHint="#00000000"
        android:choiceMode="singleChoice"
        android:clickable="true"
        android:scrollingCache="false"
        android:background="@color/transparent"
        android:listSelector="@drawable/listitem_selector">
    </ListView>
</RelativeLayout>

<Button
    android:id="@+id/buttonTryAgain"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="Try Again" />

Based on other postings, notice that I am using a listitem_selector as the background for the ListView so that individual Items can reflect the State in different colors. 根据其他发布,请注意,我使用listitem_selector作为ListView的背景,以便各个Items可以用不同的颜色反映State。

Next the XML for the listitem_selector: 接下来是listitem_selector的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/itemSelected" />
  <item android:state_pressed="true" android:drawable="@color/itemPressed" />

<item android:drawable="@color/text_white" />

Here is a 'wrinkle' due to the fact that each Item is comprised of 4 Textboxes - the Adapter is built to utilize the layout list_parkers_tmp 这是一个“皱纹”,原因是每个项目都包含4个文本框-适配器被构建为利用布局list_parkers_tmp

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:background="@drawable/listitem_selector">

<ImageView
    android:id="@+id/logo"
    android:layout_width="40dp"
    android:layout_height="50dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/hangtag" />

<TextView
    android:id="@+id/textViewPermit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/logo"
    android:text="Permit Number"
    android:textStyle="bold"
    android:textColor="#FF00AA00"
    android:textSize="22dp" />

<TextView
    android:id="@+id/textViewCategory"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/textViewPermit"
    android:paddingLeft="10dp"
    android:text="Category"
    android:textColor="#FF555555"
    android:textSize="16dp" />

<TextView
    android:id="@+id/textViewCars"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/logo"
    android:layout_alignParentRight="true"
    android:layout_toRightOf="@+id/logo"
    android:text="TextView"
    android:textColor="#FF000000"
    android:textSize="18dp" />

<TextView
    android:id="@+id/textStatus"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:paddingLeft="10dp"
    android:text="Status"
    android:textStyle="bold"        
    android:textColor="#FF0000"
    android:textSize="16dp" />

Code within the Activity: 活动中的代码:

// create our SelectedAdapter
selectedAdapter = new SelectedAdapter(getBaseContext(), itemList, R.layout.list_parkers_tmp,
     new String[]{"permit", "status", "category", "cars"},
     new int[]{R.id.textViewPermit, R.id.textStatus, R.id.textViewCategory, R.id.textViewCars});
selectedAdapter.notifyDataSetChanged();
listLocations.setAdapter(selectedAdapter);

And finally the Custom SimpleAdapter: 最后是Custom SimpleAdapter:

public class SelectedAdapter extends SimpleAdapter {
private int[] mTo;
private String[] mFrom;
private ViewBinder mViewBinder;
private List<? extends Map<String, ?>> mData;
private int mResource;
private int mDropDownResource;
private LayoutInflater mInflater;

public SelectedAdapter(Context context,
                       List<? extends Map<String, String>> data, int resource, String[] from,
                       int[] to) {
    super(context, data, resource, from, to);
    mData = data;
    mResource = mDropDownResource = resource;
    mFrom = from;
    mTo = to;
    mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

/**
 * @see android.widget.Adapter#getView(int, View, ViewGroup)
 */

// used to keep selected position in ListView
private int selectedPos = -1;    // init value for not-selected

public void setSelectedPosition(int pos) {
    selectedPos = pos;
    // inform the view of this change
    notifyDataSetChanged();
}

public int getSelectedPosition() {
    return selectedPos;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    //View v = convertView;
    View v = super.getView(position, convertView, parent);

    // only inflate the view if it's null
    if (v == null) {
        LayoutInflater vi = (LayoutInflater) Config.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //LayoutInflater vi
        //        = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.list_parkers_tmp, null);
    }

    // get text view
    String TextViewStr = "";
    String CategoryText = "";
    TextViewStr = this.getItem(position).toString();   // Get ALL of ListView Item's Text Values

    TextView viewPermit = (TextView) v.findViewById(R.id.textViewPermit);
    CategoryText = parseItemStr(TextViewStr,"permit=","");
    viewPermit.setText(CategoryText);

    TextView status = (TextView) v.findViewById(R.id.textStatus);
    CategoryText = parseItemStr(TextViewStr,"status=","cars=");
    status.setText(CategoryText);

    TextView viewCategory = (TextView) v.findViewById(R.id.textViewCategory);
    CategoryText = parseItemStr(TextViewStr,"category=","permit=");
    viewCategory.setText(CategoryText);

    TextView viewCars = (TextView) v.findViewById(R.id.textViewCars);
    CategoryText = parseItemStr(TextViewStr,"cars=","category=");
    viewCars.setText(CategoryText);

    // change the row color based on selected state
    if (selectedPos == position) {
        v.setSelected(true);
        v.setPressed(true);
        v.setBackgroundColor(R.color.amber_800);
    } else {
        v.setSelected(false);
        v.setPressed(false);
        v.setBackgroundColor(R.color.text_white);
    }

    /*
    // to use something other than .toString()
    MyClass myobj = (MyClass)this.getItem(position);
    label.setText(myobj.myReturnsString());
    */
    return (v);
}

public String parseItemStr (String ItemStr, String Category1, String Category2) {
    ItemStr = ItemStr.replace("}","");
    ItemStr = ItemStr.replace("{","");
    int loc1 = ItemStr.indexOf((Category1));
    int loc2 = 0;
    if (!Category2.equals("")) {
        loc2 = ItemStr.indexOf((Category2));
    } else {
        loc2 = ItemStr.length();
    }
    String shortStr = ItemStr.substring(loc1,loc2);
    shortStr = shortStr.replace(Category1,"").trim();
    int len = shortStr.length();
    if (len > 0){
        if (shortStr.substring(len-1,len).equals(",")) {
            shortStr = shortStr.substring(0,Math.max(0,len-1));
        }
    }

    return shortStr;
}

} }

I can watch the code execute and the GetView() executes as expected. 我可以看到代码执行,并且GetView()按预期执行。

First, when the ListView is initially created (no specific Item Selected), the individual Item's Background Color is NOT Transparent as expected, but Gray. 首先,在最初创建ListView时(未选择特定的项),单个项的背景颜色不是预期的透明,而是灰色。 That's not right. 那是不对的。

Then, when an Item is Selected the GetView() code portion where selectedPos == position executes as intended. 然后,当选择一个项目时,将按预期执行在其中执行selectedPos == position的GetView()代码部分。
But No Background Color change results from it. 但是不会因此而导致背景颜色变化。

Have I totally messed things up or have I just missed a few things? 我是完全搞砸了还是错过了几件事?

Try using setActivated. 尝试使用setActivated。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_activated="true" android:drawable="@color/amber_800" />
    <item android:state_pressed="true" android:drawable="@color/itemPressed" />
    <item android:drawable="@color/transparent" />
</selector>

Then just activate and deactivate 然后只需激活和停用

// change the row color based on selected state
if (selectedPos == position) {
    v.setActivated(true);
} else {
    v.setActivated(false);
}

I made some WAG's (Wild A** Guesses) and found the answer. 我做了一些WAG(Wild A ** Guesses),找到了答案。

The problem was in the getView() BackgroundColor parameters. 问题出在getView() BackgroundColor参数中。

if (selectedPos == position) {
    v.setSelected(true);
    v.setPressed(true);
    v.setBackgroundColor(R.color.amber_800);
} else {
    v.setSelected(false);
    v.setPressed(false);
    v.setBackgroundColor(R.color.text_white);
}

When I was attempting to use a Resource defined color such as: v.setBackgroundColor(R.color.amber_800); 当我尝试使用资源定义的颜色时,例如: v.setBackgroundColor(R.color.amber_800); it did not work. 这没用。
In fact occassionally the R.color.amber_800 was underlined in Red (an indication of a problem). 实际上, 有时R.color.amber_800用红色加下划线(表明有问题)。 The confusing part was that many times it was not underlined in Red and sometimes it was. 令人困惑的是,很多时候它没有用红色强调,有时是红色。

Regardless, when I changed those color parameters to: 无论如何,当我将这些颜色参数更改为:

v.setBackgroundColor(Color.YELLOW);
       and 
v.setBackgroundColor(Color.TRANSPARENT);  

Things began working. 事情开始起作用。

Does anyone know how to get the Resource color settings to work as expected? 有谁知道如何使“资源”颜色设置按预期工作?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM