这几天都在开发app,关于好多控件的使用,前不久吧服务端弄通了之后,搞搞客户端,然而发现在客户端中最常用的控件就是listview了,今天,我来发布一下我的自定义listview的方法吧!
import java.text.SimpleDateFormat;
import java.util.Date;
import android.R.interpolator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.example.agbc.R;
import com.lidroid.xutils.ViewUtils;
public class RefershListView extends ListView implements OnScrollListener,
android.widget.AdapterView.OnItemClickListener{
public View viewhandler;
public LinearLayout refre_hander_root;
public LinearLayout refer_hander_view;
public ProgressBar refer_hander_progressba;
public TextView it_refer_hand_text;
public TextView it_refer_hand_data;
public ImageView refer_hander_imagview;
public int MeasuredHeight;
public View CustmerView;
public RotateAnimation AnimationUp;
public RotateAnimation AnimationDown;
private int downY=-1;
private int myfirstVisibleItem=-1;
private int conrrentOpen=PULL_FEFERSH;
private static final int PULL_FEFERSH = 0;//下拉刷新
private static final int RELEASE_FEFERSH = 1;//释放刷新
private static final int IS_FEFERSH = 2;//正在刷新
private RefershListLister refershListLister;
private View viewfooterView;
private int foothight;
private boolean isload=false;
public RefershListView(Context context) {
super(context);
initHander();
InitFooter();
//初始化刷新头
this.setOnScrollListener(this);
}
public RefershListView(Context context, AttributeSet attrs) {
super(context, attrs);
initHander();
InitFooter();
this.setOnScrollListener(this);
}
@SuppressLint("NewApi")
public RefershListView(Context context, AttributeSet attrs,
int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initHander();
InitFooter();
this.setOnScrollListener(this);
}
/**
* 底部角
*/
private void InitFooter() {
viewfooterView=View.inflate(getContext(), R.layout.refer_footer, null);
//viewfooterView.setVisibility(View.GONE);
viewfooterView.measure(0, 0);
foothight=viewfooterView.getMeasuredHeight();
viewfooterView.setPadding(0, -foothight, 0, 0);
this.addFooterView(viewfooterView);
}
private void initHander() {
viewhandler=View.inflate(getContext(), R.layout.refresh_hander,null);
ViewUtils.inject(this,viewhandler);
refre_hander_root=(LinearLayout) viewhandler.findViewById(R.id.refre_hander_root);
Log.i(VIEW_LOG_TAG, "文江的测试"+viewhandler);
/**
* 刷新头
*/
refer_hander_view=(LinearLayout) viewhandler.findViewById(R.id.refer_hander_view);
/**
* 获取进度条
*/
refer_hander_progressba=(ProgressBar) viewhandler.findViewById(R.id.refer_hander_progressba);
it_refer_hand_text=(TextView) viewhandler.findViewById(R.id.it_refer_hand_text);
it_refer_hand_data=(TextView) viewhandler.findViewById(R.id.it_refer_hand_data);
refer_hander_imagview=(ImageView) viewhandler.findViewById(R.id.refer_hander_imagview);
//侧拉刷新头的高度
refer_hander_view.measure(0,0);
/**
* 获取测量的高度
*/
MeasuredHeight=refer_hander_view.getMeasuredHeight();
//隐藏刷新头,负的高度:
refer_hander_view.setPadding(0, -MeasuredHeight, 0, 0);
/**
* 添加头
*/
this.addHeaderView(viewhandler);
//初始化动画
initAmnation();
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
downY=(int)ev. getY();
break;
case MotionEvent.ACTION_MOVE:
if (downY==-1) {
downY=(int) ev.getY();
}
int movY=(int) ev.getY();
int padding=-MeasuredHeight+movY-downY;
int []listviewlocation=new int[2];
this.getLocationOnScreen(listviewlocation);//获取listview在屏幕上面左上角的坐标
int listviewlocationY=listviewlocation[1];
int[] custmoreViewation=new int[2];//添加布局的左上角的坐标
CustmerView.getLocationOnScreen(custmoreViewation);
int custmoreViewationY=custmoreViewation[1];
//不做刷新
if (listviewlocationY>custmoreViewationY) {
break;
}
if (IS_FEFERSH==conrrentOpen) {
break;
}
/**
* 下拉刷新的条件
*/
if (padding>-MeasuredHeight&&myfirstVisibleItem==0) {
if (padding>0&&conrrentOpen==PULL_FEFERSH) {
//完整拖拽
conrrentOpen=RELEASE_FEFERSH;
setCurrentOpent();
}
if (padding<0&&conrrentOpen==RELEASE_FEFERSH) {
//不完整拖1
conrrentOpen=PULL_FEFERSH;
setCurrentOpent();
}
refer_hander_imagview.setPadding(0, padding, 0, 0);
return true;
}
break;
case MotionEvent.ACTION_UP:
//复位
downY=-1;
//up释放时间
if (conrrentOpen==PULL_FEFERSH) {
refer_hander_imagview.setPadding(0, MeasuredHeight, 0, 0);
}
if (conrrentOpen==RELEASE_FEFERSH) {
refer_hander_imagview.setPadding(0, 0, 0, 0);
conrrentOpen=IS_FEFERSH;
setCurrentOpent();
//刷新业务逻辑处理
if (null!=refershListLister) {
refershListLister.refershListLinsterDown();
}
handler.sendEmptyMessageDelayed(0, 2000);
onReferShFinsh();
}
// return true;
break;
default:
break;
}
return super.onTouchEvent(ev);
}
//刷新接收后
private void onReferShFinsh() {
//上拉刷新
if (IS_FEFERSH==conrrentOpen) {
conrrentOpen=PULL_FEFERSH;
refer_hander_progressba.setVisibility(View.VISIBLE);
refer_hander_imagview.setVisibility(View.INVISIBLE);
refer_hander_view.setPadding(0, -MeasuredHeight, 0, 0);
}
if (isload) {
isload=false;
viewfooterView.setPadding(0, -foothight,0, 0);
}
}
Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
onReferShFinsh();
};
};
public void addCustmerView(View view){
CustmerView=view;
//添加到线性布局中(更)
refre_hander_root.addView(CustmerView);
}
public void setCurrentOpent(){
switch (conrrentOpen) {
case RELEASE_FEFERSH:
it_refer_hand_text.setText("释放刷新");
refer_hander_imagview.startAnimation(AnimationUp);
break;
case PULL_FEFERSH:
it_refer_hand_text.setText("下拉刷新");
refer_hander_imagview.startAnimation(AnimationDown);
break;
case IS_FEFERSH:
it_refer_hand_text.setText("正在刷新");
refer_hander_progressba.setVisibility(View.VISIBLE);
refer_hander_imagview.clearAnimation();//清除动画
refer_hander_imagview.setVisibility(View.GONE);
it_refer_hand_data.setText(getFRferTime());
break;
default:
break;
}
}
private String getFRferTime() {
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date=new Date();
return simpleDateFormat.format(date);
}
/**
* 初始化动画,旋转的动画
*/
private void initAmnation() {
//参数从什么角度到什么角度,依赖位置
AnimationUp=new RotateAnimation(0, -180,
Animation.RELATIVE_TO_SELF,
0.5f,
Animation.RELATIVE_TO_SELF,
0.5f);
//旋转时长
AnimationUp.setDuration(200);
//旋转过后停到原来的位置
AnimationUp.setFillAfter(true);
AnimationDown=new RotateAnimation(-180, 0,
Animation.RELATIVE_TO_SELF,
0.5f,
Animation.RELATIVE_TO_SELF,
0.5f);
//旋转时长
AnimationDown.setDuration(200);
//旋转过后停到原来的位置
AnimationDown.setFillAfter(true);
}
/**
* 监听滑动状态的方法
*/
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
//看见的是最后一个条目
//OnScrollListener.SCROLL_STATE_FLING;//快速滚动
//OnScrollListener.SCROLL_STATE_IDLE;//滚动状态发生改变的
//OnScrollListener.SCROLL_STATE_TOUCH_SCROLL;//一直触摸着在滚动
if (scrollState == SCROLL_STATE_IDLE
|| scrollState == SCROLL_STATE_FLING) {
if (getLastVisiblePosition()==getCount()-1&&!isload) {
isload=true;
viewfooterView.setPadding(0, 0, 0, 0);//初始化位置
setSelection(getCount());// 设置ListView显示位置
//加载业务逻辑(回调)
if (null!=refershListLister) {
refershListLister.refershListLinsterUp();
handler.sendEmptyMessageDelayed(0, 2000);
}
}
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
myfirstVisibleItem=firstVisibleItem;
}
public interface RefershListLister{
public void refershListLinsterUp();
public void refershListLinsterDown();
}
public void OnReferListeer(RefershListLister refershListLister){
this.refershListLister=refershListLister;
}
OnItemClickListener mItemClickListener;
/**
* 响应Item点击
*/
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
if (mItemClickListener != null) {
mItemClickListener.onItemClick(parent, view, position
- getHeaderViewsCount(), id);// 将原始position减去HeaderView的数量,才是准确的position
}
}
/**
* 处理Item点击事件
*/
@Override
public void setOnItemClickListener(
android.widget.AdapterView.OnItemClickListener listener) {
mItemClickListener = listener;
super.setOnItemClickListener(this);
}
}
下面的就是布局文件了
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/refre_hander_footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<ProgressBar
android:id="@+id/xlistview_footer_progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible" />
<TextView
android:id="@+id/xlistview_footer_hint_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/xlistview_footer_hint_normal" />
</LinearLayout>
//----------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/refre_hander_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/refer_hander_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<!-- 刷新头不 "invisible"占位置,但不可见 -->
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp" >
<ProgressBar
android:id="@+id/refer_hander_progressba"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible" />
<ImageView
android:id="@+id/refer_hander_imagview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/pull_to_refresh_arrow" />
</FrameLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/it_refer_hand_text"
android:layout_width="134dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingRight="40dp"
android:text="下拉刷新" />
<TextView
android:id="@+id/it_refer_hand_data"
android:layout_width="129dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingRight="40dp"
android:text="20343" />
</LinearLayout>
</LinearLayout>
<!-- 添加轮播图的 效果 -->
</LinearLayout>
大概就是这么多了,若需要源码的小伙伴,联系我哦