ScrollView+自定义listview
布局文件
<RelativeLayout 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"
tools:context=".MainActivity" >
<ScrollView
android:id="@+id/scroll"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:fillViewport="true" >
<LinearLayout
android:id="@+id/lin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/but2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全选" />
<Button
android:id="@+id/but1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="反选" />
<Button
android:id="@+id/but3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消" />
</LinearLayout>
<my_view.My_listview
android:id="@+id/lv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</my_view.My_listview>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="不错" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="不错" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="不错" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="不错" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="不错" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
自定义listview
package my_view;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;
public class My_listview extends ListView {
public My_listview(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
int spec = MeasureSpec.makeMeasureSpec(400, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, spec);
}
// @Override
// protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// // TODO Auto-generated method stub
// int spec = MeasureSpec.makeMeasureSpec(800, MeasureSpec.AT_MOST);
// super.onMeasure(widthMeasureSpec, spec);
// }
}
baseadapter:
package adapter;
import java.io.IOException;
import java.util.ArrayList;
import vo.Goods;
import com.example.test_goods_listview.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
public class Myadapter extends BaseAdapter {
Context context;
ArrayList<Goods> list;
public Myadapter(Context context, ArrayList<Goods> list) {
super();
this.context = context;
this.list = list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(final int position, View view, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater layout = LayoutInflater.from(context);
view = layout.inflate(R.layout.show_goods, null);
Viewhodler hodler = null;
if (hodler == null) {
hodler = new Viewhodler();
hodler.show_title = (TextView) view.findViewById(R.id.show_tite);
hodler.show_content = (TextView) view
.findViewById(R.id.show_content);
hodler.show_price = (TextView) view.findViewById(R.id.show_price);
hodler.show_img = (ImageView) view.findViewById(R.id.show_img);
hodler.cb = (CheckBox) view.findViewById(R.id.checkBox1);
view.setTag(hodler);
} else {
hodler = (Viewhodler) view.getTag();
}
hodler.show_title.setText(list.get(position).getFoodtitle());
hodler.show_content.setText(list.get(position).getFoodcontent());
Bitmap bit;
try {
bit = BitmapFactory.decodeStream(context.getResources().getAssets()
.open(list.get(position).getImageurl()));
hodler.show_img.setImageBitmap(bit);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
hodler.show_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
hodler.show_price.setText(list.get(position).getFoodprice());
hodler.cb.setChecked(list.get(position).getFoodboolean());
hodler.cb.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (list.get(position).getFoodboolean()) {
list.get(position).setFoodboolean(false);
} else {
list.get(position).setFoodboolean(true);
}
}
});
return view;
}
class Viewhodler {
TextView show_title;
TextView show_content;
TextView show_price;
ImageView show_img;
CheckBox cb;
}
}
package com.example.test_goods_listview;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import my_view.My_listview;
import vo.Goods;
import xmlutil.GetPullUtils;
import adapter.Myadapter;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;
public class MainActivity extends Activity {
My_listview lv;
TextView show_title;
Context context;
ArrayList<Goods> list1;
ScrollView sc;
Myadapter adapter;
Button bt_selectall, bt_cancel, bt_deselectall;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
lv = (My_listview) findViewById(R.id.lv);
sc = (ScrollView) findViewById(R.id.scroll);
bt_selectall = (Button) findViewById(R.id.but1);// 反选
bt_cancel = (Button) findViewById(R.id.but2);// 全选
bt_deselectall = (Button) findViewById(R.id.but3);// 取消
bt_selectall.setOnClickListener(l);
bt_cancel.setOnClickListener(l);
bt_deselectall.setOnClickListener(l);
init();
initboolean();
adapter = new Myadapter(context, list1);
lv.setAdapter(adapter);
lv.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_UP) {
sc.requestDisallowInterceptTouchEvent(false);
} else {
sc.requestDisallowInterceptTouchEvent(true);
}
return false;
}
});
}
OnClickListener l = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == bt_cancel.getId()) {
for (int i = 0; i < list1.size(); i++) {
list1.get(i).setFoodboolean(true);
adapter.notifyDataSetChanged();
}
} else if (v.getId() == bt_deselectall.getId()) {
for (int i = 0; i < list1.size(); i++) {
list1.get(i).setFoodboolean(false);
adapter.notifyDataSetChanged();
}
} else if (v.getId() == bt_selectall.getId()) {
for (int i = 0; i < list1.size(); i++) {
list1.get(i).setFoodboolean(!list1.get(i).getFoodboolean());
}
adapter.notifyDataSetChanged();
}
}
};
private void init() {
// TODO Auto-generated method stub
try {
InputStream is = getResources().getAssets().open("data.xml");
GetPullUtils utils = new GetPullUtils();
list1 = utils.getDoods(is);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void initboolean() {
for (int i = 0; i < list1.size(); i++) {
list1.get(i).setFoodboolean(false);
}
}
}