一.主要有经典的PullToReFreshLayout, springView,swipeRfreshLayout.
二,以pullToRefreshLayout为例.可以实现listview上加一个头,上拉加载,下拉刷新.
1.导入libary.
2.写一个类继承
PullToRefreshListView
3.代码如下.
public class Mylistview extends PullToRefreshListView {
private static final String TAG = "Mylistview";
private List mData;
private Context mContext;
private MyAdapter mAdapter;
public Mylistview(Context context, AttributeSet attrs) {
super(context, attrs);
}
public Mylistview(Context context) {
this(context,null);
mContext = context;
//构建假数据
mData = new ArrayList<>();
for (int i = 0; i < 100; i++) {
mData.add( ""+ i);
}
//构建listview的头信息
ImageView iv = new ImageView(context);
iv.setImageResource(R.mipmap.ic_launcher);
getRefreshableView().addHeaderView(iv);
//设置刷新的模式
setMode(Mode.BOTH);
mAdapter = new MyAdapter();
setAdapter(mAdapter);
//设置上拉刷新的和加载的监听
setOnRefreshListener(new OnRefreshListener2() {
@Override
public void onPullDownToRefresh(PullToRefreshBase refreshView) {
mData.add("dfsdf");
mAdapter.notifyDataSetChanged();
//刷新完成
postDelayed(new Runnable() {
@Override
public void run() {
onRefreshComplete();//刷新时间太短可能会失效
}
}, 2000);
}
@Override
public void onPullUpToRefresh(PullToRefreshBase refreshView) {
mData.add("dsfdf2323sdf");
mAdapter.notifyDataSetChanged();
//加载完成
postDelayed(new Runnable() {
@Override
public void run() {
onRefreshComplete();//刷新时间太短可能会失效
}
}, 2000);
}
});
//设置条目的点击事件
setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
position=position-2;
Toast.makeText(mContext, "条目"+position+"被点击", Toast.LENGTH_SHORT).show();
}
});
}
private class MyAdapter
extends BaseAdapter {
private int mNum;
private String mS;
@Override
public int getCount() {
return mData == null
? 0
: mData.size();
}
@Override
public Object getItem(int position) {
if (mData != null) {
return mData.get(position);
}
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder vh;
if (convertView == null) {
convertView = View.inflate(mContext,R.layout.item_listview , null);
vh=new ViewHolder();
vh.btn= (Button) convertView.findViewById(R.id.btn);
vh.text= (TextView) convertView.findViewById(R.id.text);
convertView.setTag(vh);
}else{
vh= (ViewHolder) convertView.getTag();
}
mS = mData.get(position);
vh.text.setText(mS);
vh.btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mS = mData.get(position);
mNum = Integer.parseInt(mS);
mNum--;
mData.set(position,mNum+"");
mAdapter.notifyDataSetChanged();
}
});
return convertView;
}
}
private static class ViewHolder {
Button btn;
TextView text;
}
}
三.springView的使用,包裹其他控件的方式进行刷新
1.导入依赖
compile 'com.liaoinstan.springview:library:1.2.6'
2.代码
public class MainActivity extends AppCompatActivity {
private Handler mHandler=new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
mData.add("dssd");
mSpringview.onFinishFreshAndLoad();
mAdapter.notifyDataSetChanged();
break;
case 1:
mData.addAll(Arrays.asList("dss","dss","sdsd"));
mAdapter.notifyDataSetChanged();
mSpringview.onFinishFreshAndLoad();
break;
}
}
};
SpringView mSpringview;
ListView mListview;
private ArrayList mData;
private MyAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
mListview = (ListView) findViewById(R.id.listview);
mSpringview = (SpringView) findViewById(R.id.springview);
iniData();
iniEvent();
}
private void iniEvent() {
mSpringview.setType(SpringView.Type.FOLLOW);
mSpringview.setListener(new SpringView.OnFreshListener() {
@Override
public void onRefresh() {
mHandler.sendEmptyMessageDelayed(0,2000);
}
@Override
public void onLoadmore() {
mHandler.sendEmptyMessageDelayed(1,2000);
}
});
}
private void iniData() {
mData = new ArrayList<>();
for (int i = 0; i < 20; i++) {
mData.add("dser" + i);
}
mAdapter = new MyAdapter();
mListview.setAdapter(mAdapter);
mSpringview.setHeader(new MeituanFooter(this));
mSpringview.setFooter(new DefaultFooter(this));
}
private class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return mData == null ? 0 : mData.size();
}
@Override
public Object getItem(int position) {
if (mData != null) {
return mData.get(position);
}
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv = null;
if (convertView == null) {
tv = new TextView(MainActivity.this);
} else {
tv = (TextView) convertView;
}
tv.setText(mData.get(position));
return tv;
}
}
}
3.布局
四.swipeRefreshLayout