簡體   English   中英

Android自定義SimpleAdapter背景色

[英]Android Custom SimpleAdapter Background Color

我一直在嘗試使用自定義SimpleAdapter實現讓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" />

根據其他發布,請注意,我使用listitem_selector作為ListView的背景,以便各個Items可以用不同的顏色反映State。

接下來是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" />

這是一個“皺紋”,原因是每個項目都包含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" />

活動中的代碼:

// 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);

最后是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;
}

}

我可以看到代碼執行,並且GetView()按預期執行。

首先,在最初創建ListView時(未選擇特定的項),單個項的背景顏色不是預期的透明,而是灰色。 那是不對的。

然后,當選擇一個項目時,將按預期執行在其中執行selectedPos == position的GetView()代碼部分。
但是不會因此而導致背景顏色變化。

我是完全搞砸了還是錯過了幾件事?

嘗試使用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>

然后只需激活和停用

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

我做了一些WAG(Wild A ** Guesses),找到了答案。

問題出在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);
}

當我嘗試使用資源定義的顏色時,例如: v.setBackgroundColor(R.color.amber_800); 這沒用。
實際上, 有時R.color.amber_800用紅色加下划線(表明有問題)。 令人困惑的是,很多時候它沒有用紅色強調,有時是紅色。

無論如何,當我將這些顏色參數更改為:

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

事情開始起作用。

有誰知道如何使“資源”顏色設置按預期工作?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM