ListView
在value下定義顏色
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="red">#ff0000</color>//紅色:ff0000
<color name="white">#ffffff</color>//白色:ffffff
</resources>
getView方法中,convertView(性能優化:減少內存,提高速度)
水果的例子:
Fruit類:
public class Fruit {
private String name;
private int img;
// private boolean isChecked;
public String getName() {
return name;
}
public int getImg() {
return img;
}
public Fruit(String name, int img) {
this.name = name;
this.img = img;
}
public void setName(String name) {
this.name = name;
}
public void setImg(int img) {
this.img = img;
}
// public boolean isChecked(){
// return isChecked;
// }
//
// public void setIsChecked(boolean isChecked) {
// this.isChecked = isChecked;
// }
}
FruitAdapter
public class FruitAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private List<Fruit> mFruit;
private boolean[] mAllCheckBox;
public FruitAdapter(LayoutInflater inflater, List<Fruit> fruit) {
mInflater=inflater;
mFruit=fruit;
mAllCheckBox=new boolean[fruit.size()];
}
public void itemSelect(int position){
mAllCheckBox[position]=!mAllCheckBox[position];
notifyDataSetChanged();
}
//全選方法
public void checkedAll() {
for(int i=0;i<mAllCheckBox.length;i++){
mAllCheckBox[i]=true;
notifyDataSetChanged();
}
}
//反選方法
public void checkedOther(){
for (int i=0;i<mAllCheckBox.length;i++){
mAllCheckBox[i]=! mAllCheckBox[i];
notifyDataSetChanged();
}
}
@Override
public int getCount() {
return mFruit.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Fruit fruit=mFruit.get(position);
ViewHolder vh=null;
if (convertView==null){
vh=new ViewHolder();
convertView=mInflater.inflate(R.layout.item_fruit,null);
vh.checkBox= (CheckBox) convertView.findViewById(R.id.checkbox);
vh.imageView= (ImageView) convertView.findViewById(R.id.imageview);
vh.textView= (TextView) convertView.findViewById(R.id.textview);
convertView.setTag(vh);
}else{
vh= (ViewHolder) convertView.getTag();
}
vh.textView.setText(fruit.getName());
vh.imageView.setImageResource(fruit.getImg());
vh.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// mFruit.get(position).setIsChecked(isChecked);
mAllCheckBox[position]=isChecked;
notifyDataSetChanged();
}
});
// vh.checkBox.setChecked(fruit.isChecked());
vh.checkBox.setChecked(mAllCheckBox[position]);
return convertView;
}
//緩存數據
class ViewHolder{
ImageView imageView;
CheckBox checkBox;
TextView textView;
}
}
FruitActivity
public class FruitActivity extends Activity {
private ListView mListView;
private List <Fruit> mFruit;
private FruitAdapter mAdapter;
private Button mButtonHeader;
private Button mButtonFooter;
private View viewHeader;
private View viewFooter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_layout);
mListView= (ListView) findViewById(R.id.listview);
LayoutInflater inflater=getLayoutInflater();
initData();
mAdapter=new FruitAdapter(inflater,mFruit);
//頂部添加全選按鈕
viewHeader=inflater.inflate(R.layout.header_layout,null);
mButtonHeader= (Button) viewHeader.findViewById(R.id.button_selectAll);
mButtonHeader.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mAdapter.checkedAll();
}
});
mListView.addHeaderView(viewHeader);
//底部添加反選按鈕
viewFooter=inflater.inflate(R.layout.footer_layout,null);
mButtonFooter= (Button) viewFooter.findViewById(R.id.footer);
mButtonFooter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mAdapter.checkedOther();
}
});
mListView.addFooterView(viewFooter);
//對ListView監聽
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mAdapter.itemSelect(position-1);//不減1會數組越界
Log.d("FRIUT","選中的水果是"+mFruit.get(position-1).getName());
}
});
}
private void initData() {
mFruit=new ArrayList<>();
for(int i=0;i<5;i++) {
Fruit apple=new Fruit("蘋果",R.mipmap.apple);
Fruit apricot=new Fruit("杏子",R.mipmap.apricot);
Fruit banana=new Fruit("香蕉",R.mipmap.banana);
mFruit.add(apple);
mFruit.add(apricot);
mFruit.add(banana);
}
}
}
ListView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listview"
android:cacheColorHint="#00000000"//添加透明色
android:divider="#ff0000"//添加紅色分隔線
android:dividerHeight="2dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>