2012-11-03 187 views
1

我有一個自定義列表視圖,其中包含7個文本視圖..我已經創建了一個自定義的線性佈局,然後我填充它...我什麼想要做的是設置一個自定義顏色...當用戶點擊列表項並保持突出顯示,直到他點擊另一個項目...我將如何做到這一點... ...更改自定義列表視圖中列表項的背景顏色

這是自定義佈局對於listItems中......

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

    <TextView 
     android:id="@+id/TvVLdate" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:paddingLeft="8dp" 
     android:paddingTop="8dp" 
     android:textColor="#000000" 
     android:textSize="15dp" /> 

    <TextView 
     android:id="@+id/TvVLtime" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@+id/TvVLdate" 
     android:paddingLeft="8dp" 
     android:paddingTop="8dp" 
     android:textColor="#000000" 
     android:textSize="15dp" /> 

    <TextView 
     android:id="@+id/TvVLcardno" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@+id/TvVLtime" 
     android:paddingLeft="8dp" 
     android:paddingTop="8dp" 
     android:textColor="#000000" 
     android:textSize="15dp" /> 

    <TextView 
     android:id="@+id/TvVLsaletext" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/TvVLdate" 
     android:paddingLeft="8dp" 
     android:paddingTop="8dp" 
     android:text="Sale transaction:Rs" 
     android:textColor="#000000" 
     android:textSize="15dp" /> 

    <TextView 
     android:id="@+id/TvVLamnt" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/TvVLsaletext" 
     android:layout_toRightOf="@+id/TvVLsaletext" 
     android:paddingTop="8dp" 
     android:textColor="#000000" 
     android:textSize="15dp" /> 

    <TextView 
     android:id="@+id/TvVLtexttip" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/TvVLamnt" 
     android:layout_toRightOf="@+id/TvVLamnt" 
     android:paddingLeft="8dp" 
     android:paddingTop="8dp" 
     android:text="Tip:Rs" 
     android:textColor="#000000" 
     android:textSize="15dp" /> 

    <TextView 
     android:id="@+id/TvVLtip" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/TvVLtexttip" 
     android:layout_toRightOf="@+id/TvVLtexttip" 
     android:paddingTop="8dp" 
     android:textColor="#000000" 
     android:textSize="15dp" /> 

</RelativeLayout> 

而我的列表佈局..

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#E0E0E0" 
    android:orientation="vertical" > 

    <include 
     android:id="@+id/Hvoid" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     layout="@layout/header" /> 

    <RelativeLayout 
     android:id="@+id/RLvoid" 
     android:layout_width="match_parent" 
     android:layout_height="40dp" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/Hvoid" 
     android:orientation="horizontal" > 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dp" 
      android:layout_marginTop="12dp" 
      android:text="Main Menu" 
      android:textColor="#000000" 
      android:textSize="15dp" /> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_centerVertical="true" 
      android:paddingBottom="5dp" 
      android:paddingTop="5dp" 
      android:src="@drawable/ivoid" /> 
    </RelativeLayout> 

    <View 
     android:id="@+id/Vvoidtop" 
     android:layout_width="fill_parent" 
     android:layout_height="2dp" 
     android:layout_below="@+id/RLvoid" 
     android:background="@android:color/darker_gray" /> 

    <ListView 
     android:id="@+id/Lvvoid" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_above="@+id/Bconfirm" 
     android:layout_below="@+id/Vvoidtop" 
     android:background="#E0E0E0" 
     android:cacheColorHint="#00000000" 
     android:divider="@android:color/transparent" 
     android:dividerHeight="20dp" > 
    </ListView> 

    <Button 
     android:id="@+id/Bconfirm" 
     android:layout_width="fill_parent" 
     android:layout_height="40dp" 
     android:layout_above="@+id/Vvoidbot" 
     android:layout_marginTop="8dp" 
     android:layout_marginBottom="8dp" 
     android:layout_marginLeft="20dp" 
     android:layout_marginRight="20dp" 
     android:background="@drawable/cbconfirm" /> 

    <View 
     android:id="@+id/Vvoidbot" 
     android:layout_width="fill_parent" 
     android:layout_height="2dp" 
     android:layout_above="@+id/Tvfooter" 
     android:background="@android:color/darker_gray" /> 

    <TextView 
     android:id="@+id/Tvfooter" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:gravity="center_horizontal" 
     android:text="© India Transact Services Ltd." 
     android:textColor="#000000" 
     android:textSize="15dp" /> 

</RelativeLayout> 

而繪製的顏色xml文件...

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!-- Selected Item --> 
    <item android:drawable="@android:color/darker_gray" android:state_selected="true"/> 
    <item android:drawable="@android:color/darker_gray" android:state_focused="true"/> 
    <!-- Default Item --> 
    <item android:drawable="@android:color/white" /> 
</selector> 

而且我的代碼....

public class Void extends Activity { 
    ListView lmenu; 
    View Vlistselector; 
    String s; 
    View layout; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.voidlist); 

     Menu Menu_data[] = new Menu[] { 
       new Menu("29/09/2012", "05:40", "2234 xxxx xxxx 1990", "100", 
         "10"), 
       new Menu("09/12/2012", "00:34", "2234 xxxx xxxx 1990", "99", 
         "12"), 
       new Menu("29/09/2012", "12:20", "2234 xxxx xxxx 1990", "111", 
         "0"), 
       new Menu("31/01/2012", "13:40", "2234 xxxx xxxx 1990", "105", 
         "19"), 
       new Menu("13/10/2012", "01:40", "2234 xxxx xxxx 1990", "203", 
         "1"), 
       new Menu("23/11/2012", "07:40", "2234 xxxx xxxx 1990", "44", 
         "5") }; 

     MenuAdapter adapter = new MenuAdapter(this, R.layout.voidlisttext, 
       Menu_data); 

     lmenu = (ListView) findViewById(R.id.Lvvoid); 

     lmenu.setAdapter(adapter); 
     // lmenu.setSelector(R.drawable.listcolorselector); 
     lmenu.setOnItemClickListener(new OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> ada, View v, int position, 
        long id) { 
       // TODO Auto-generated method stub 

       /* 
       * v.setSelected(true); 
       */ 
       v.setBackgroundColor(Color.parseColor("#FCD5B5")); 

       if (!(Vlistselector == null) && Vlistselector != v) 
        Vlistselector.setBackgroundColor(Color 
          .parseColor("#EEEEEE")); 
       Vlistselector = v; 

       Toast.makeText(getApplicationContext(), 
         String.valueOf(position + 1), Toast.LENGTH_LONG).show(); 
      } 
     }); 

     findViewById(R.id.BLogout).setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       finish(); 
      } 
     }); 
    } 

    public class Menu { 
     public String date; 
     public String time; 
     public String cardno; 
     public String amnt; 
     public String tip; 

     public Menu() { 
      super(); 
     } 

     public Menu(String date, String time, String cardno, String amnt, 
       String tip) { 
      super(); 
      this.date = date; 
      this.time = time; 
      this.cardno = cardno; 
      this.amnt = amnt; 
      this.tip = tip; 

     } 
    } 

    public class MenuAdapter extends ArrayAdapter<Menu> { 

     Context context; 
     int layoutResourceId; 
     Menu data[] = null; 

     public MenuAdapter(Context context, int layoutResourceId, Menu[] data) { 
      super(context, layoutResourceId, data); 
      this.layoutResourceId = layoutResourceId; 
      this.context = context; 
      this.data = data; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View row = convertView; 
      MenuHolder holder = null; 

      if (row == null) { 
       LayoutInflater inflater = ((Activity) context) 
         .getLayoutInflater(); 
       row = inflater.inflate(layoutResourceId, parent, false); 

       holder = new MenuHolder(); 
       holder.date = (TextView) row.findViewById(R.id.TvVLdate); 
       holder.time = (TextView) row.findViewById(R.id.TvVLtime); 
       holder.cardno = (TextView) row.findViewById(R.id.TvVLcardno); 
       holder.amnt = (TextView) row.findViewById(R.id.TvVLamnt); 
       holder.tip = (TextView) row.findViewById(R.id.TvVLtip); 
       row.setTag(holder); 

      } else { 
       holder = (MenuHolder) row.getTag(); 
      } 

      Menu Menu = data[position]; 
      holder.date.setText(Menu.date); 
      holder.time.setText(Menu.time); 
      holder.cardno.setText(Menu.cardno); 
      holder.amnt.setText(Menu.amnt); 
      holder.tip.setText(Menu.tip); 

      return row; 
     } 

     public class MenuHolder { 
      TextView date; 
      TextView time; 
      TextView cardno; 
      TextView amnt; 
      TextView tip; 
     } 
    } 
} 

我想是不是顏色較暗的灰色一套新的自定義顏色... 。 我是否以正確的方式做到了,或者有沒有其他辦法可以做到?

回答

0
View listColor;//declare this at the top 

做它的onclick的ListViewItem的

@Override 
protected void onListItemClick(ListView list, View view, int position, long id) { 

if(listColor!=null){ 
    listColor.setBackgroundColor(Color.BLACK); 
    listColor=view; 
}else{ 
    listColor=view; 
} 
listColor.setBackgroundColor(Color.BLUE); //intead of blue you can change it to something you want like darkergray 
//show toast on the clicked item 
String selectedValue = (String) getListAdapter().getItem(position); 
Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show(); 

} 
+0

ok..let我試試.... – Audi

+0

使用此代碼它,它只有一個item..but突出的許多項目選擇問題...我的意思是..例如..如果我有8個項目在列表中..而且我只能看到3個項目(其餘我必須滾動查看)..如果我點擊第一個項目...它會突出顯示第四和第七項... – Audi

+0

你能告訴你如何爲你的列表視圖編寫適配器嗎? –

0

對於這種情況,您應該在運行時使用setBackground()而不是依靠選擇器。

相關問題