天天看点

Header Footer

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@null">

    </ListView>


</LinearLayout>
           
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical">

    <ImageView
        android:id="@+id/imageview_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/apple"/>
    <TextView
        android:id="@+id/textview_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="苹果"
        android:padding="15dp"/>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:id="@+id/textview_oldprice"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="原价 10.0"
            android:padding="10dp"/>
        <TextView
            android:id="@+id/textview_newprice"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="现价 9.9"
            android:padding="10dp"/>

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="right|center_vertical">
        <CheckBox
            android:id="@+id/checkbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:focusable="false"/>
    </LinearLayout>


</LinearLayout>
           
public class Fruit {
    private int img;
    private String name;
    private String oldprice;
    private String newprice;

    /**
     *
     * @param img
     * @param name
     * @param oldprice
     * @param newprice
     */
    public Fruit(int img, String name, String oldprice, String newprice) {
        this.img = img;
        this.name = name;
        this.oldprice = oldprice;
        this.newprice = newprice;
    }

    public String getOldprice() {
        return oldprice;
    }

    public void setOldprice(String oldprice) {
        this.oldprice = oldprice;
    }

    public String getNewprice() {
        return newprice;
    }

    public void setNewprice(String newprice) {
        this.newprice = newprice;
    }

    public int getImg() {
        return img;
    }

    public void setImg(int img) {
        this.img = img;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}
           
public class FruitAdapter  extends BaseAdapter{
    private List<Fruit> mData;
    private LayoutInflater mInflater;
    private boolean []  mManagerAllCheckBox;
    public FruitAdapter(List<Fruit> mData,LayoutInflater mInflater){
        this.mData=mData;
        this.mInflater=mInflater;
        mManagerAllCheckBox=new boolean[mData.size()];
    }
    public void isFanXuan(){
        for (int i = ; i <mManagerAllCheckBox.length ; i++) {
            mManagerAllCheckBox[i]=!mManagerAllCheckBox[i];
        }
        notifyDataSetChanged();
    }
    public void isAllChoose(){
      for (int i=;i<mManagerAllCheckBox.length;i++){
          mManagerAllCheckBox[i]=true;
      }
        notifyDataSetChanged();
    }
    public void isChoose(int positin){
        mManagerAllCheckBox[positin]=!mManagerAllCheckBox[positin];
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return mData.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) {
        ViewHoder vh=null;
        if (convertView==null){
            convertView=mInflater.inflate(R.layout.messager,null);
            vh=new ViewHoder();
            vh.imageView_title= (ImageView) convertView.findViewById(R.id.imageview_title);
            vh.textView_name= (TextView) convertView.findViewById(R.id.textview_name);
            vh.textView_oldprice= (TextView) convertView.findViewById(R.id.textview_oldprice);
            vh.textView_newprince= (TextView) convertView.findViewById(R.id.textview_newprice);
            vh.checkBox= (CheckBox) convertView.findViewById(R.id.checkbox);
            convertView.setTag(vh);
        }else{
            vh=(ViewHoder)convertView.getTag();
        }
        Fruit fruit=mData.get(position);
        vh.imageView_title.setImageResource(fruit.getImg());
        vh.textView_name.setText(fruit.getName());
        vh.textView_oldprice.setText(fruit.getOldprice());
        vh.textView_oldprice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);//给字体话中划线
        Spanned spanned= Html.fromHtml("<font color='#ff0000'>现价9.9</font>");
        //把Spanned和String类型放一起会变成String类型
        vh.textView_newprince.setText(spanned);
        vh.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mManagerAllCheckBox[position]=isChecked;
               notifyDataSetChanged();
            }
        });
        vh.checkBox.setChecked(mManagerAllCheckBox[position]);
        return convertView;
    }
    class ViewHoder{
        ImageView imageView_title;
        TextView textView_name;
        TextView textView_oldprice;
        TextView textView_newprince;
        CheckBox checkBox;
    }
}
           
<?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">
    <Button

        android:id="@+id/button_all"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="全选"/>

</LinearLayout>
           
<?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">

    <Button
        android:id="@+id/button_fanxuan"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="反选"/>
</LinearLayout>
           
public class MainActivity extends Activity {
    private List<Fruit> mData;
    private ListView mListView;
    private LayoutInflater mInflater;
    private  FruitAdapter fruitAdaper;
    private View mHeader;
    private Button mButAll;
    private View mFooter;
    private Button mButFanXuan;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mListView= (ListView) findViewById(R.id.listview);
        mInflater=getLayoutInflater();
        initData();
        //header
        mHeader=mInflater.inflate(R.layout.header,null);
        mListView.addHeaderView(mHeader);
        mButAll= (Button)mHeader.findViewById(R.id.button_all);
        mButAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fruitAdaper.isAllChoose();
            }
        });
        //footer
        mFooter=mInflater.inflate(R.layout.footer,null);
        mListView.addFooterView(mFooter);
        mButFanXuan= (Button) mFooter.findViewById(R.id.button_fanxuan);
        mButFanXuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fruitAdaper.isFanXuan();
            }
        });
        fruitAdaper=new FruitAdapter(mData,mInflater);
        mListView.setAdapter(fruitAdaper);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                 fruitAdaper.isChoose(position-);
            }
        });

    }

    private void initData() {
        mData=new ArrayList<>();
        Fruit apple=new Fruit(R.mipmap.apple,"苹果","原价10.0","");
        Fruit pineapple=new Fruit(R.mipmap.pineapple,"菠萝","原价10.0","");
        Fruit orange=new Fruit(R.mipmap.orange,"橙子","原价10.0","");
        Fruit strawberry=new Fruit(R.mipmap.strawberry,"草莓","原价10.0","");
        for (int i=;i<;i++){

            mData.add(apple);
            mData.add(pineapple);
            mData.add(orange);
            mData.add(strawberry);
        }

    }
           

继续阅读