简单的listview测试用
public class PutdownActivity extends Activity {
private ListView listview;
private ArrayList<String> listdata;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//去除标题栏
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
//RefrshListView 自定义的listview
listview = (RefrshListView)findViewById(R.id.lv);
listdata = new ArrayList<String>();
for(int i = 0;i<30;i++){
listdata.add("这是listview数据"+i);
}
listview.setAdapter(new MyAdapter());
}
class MyAdapter extends BaseAdapter{
public int getCount() {
return listdata.size();
}
public Object getItem(int position) {
return listdata.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
TextView textview = new TextView(parent.getContext());
textview.setTextSize(18f);
textview.setText(listdata.get(position));
return textview;
}}
}
需要给listview头部加一个 HeaderView ,需要自定义listview。
public class RefrshListView extends ListView {
/**
* 包涵下拉刷新的Listview
* @param context
*/
private View mHeaderView;
public RefrshListView(Context context) {
super(context);
init();
}
public RefrshListView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public RefrshListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
/**
* 初始化头布局,脚布局,滚动监听
*/
private void init() {
initHeaderView();
}
/**
* 初始化头布局
*/
private void initHeaderView() {
//通过inflata方法加载布局
mHeaderView = View.inflate(getContext(), R.layout.layout_header_list,null);
//测量
mHeaderView.measure(0, 0);
//获得头部的高度
int mHeaderViewHeight = mHeaderView.getMeasuredHeight();
mHeaderView.setPadding(0,-mHeaderViewHeight, 0, 0);
addHeaderView(mHeaderView);
}
}
layout_header_list.xml的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:orientation="horizontal" >
<FrameLayout
android:layout_width="50dp"
android:layout_height="50dp">
<ImageView
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/common_listview_headview_red_arrow"/>
<ProgressBar
android:layout_height="match_parent"
android:layout_width="match_parent"
android:indeterminateDrawable="@drawable/ring_shape"/>
</FrameLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="下拉刷新"
android:textColor="#f00"
android:textSize="18sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:singleLine="true"
android:text="最后刷新事件:2016年8月21日23:59:05"
android:textColor="#666"
android:textSize="12sp"/>
</LinearLayout>
</LinearLayout>
自定义的ring_shape
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="-360"
android:pivotX="50%"
android:pivotY="50%">
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:innerRadiusRatio="2.5"
android:thicknessRatio="10"
android:useLevel="false">
<gradient
android:startColor="#ff0000"
android:centerColor="#44ff0000"
android:endColor="#00000000"
android:type="sweep"/>
</shape>
</rotate>