ListView 下拉刷新效果的模拟:
实现方法:主要利用线程延迟加载更新数据来模拟分页的过程
步骤:
(1)在listview中的addFooterView 方法添加一个view用于显示"玩命加载"
(2)listview设置监听,利用其onScroll和onScrollStateChanged方法达到何时启动线程
(3)利用线程更新数据,然后在主线程中更UI
代码:
适配器代码:
public class MyAdapter extends BaseAdapter {
Context context;
ArrayList<Person> list;
public MyAdapter(Context context, ArrayList<Person> list) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView==null){
convertView= LayoutInflater.from(context).inflate(R.layout.list_item,null);
viewHolder=new ViewHolder();
viewHolder.name= (TextView) convertView.findViewById(R.id.list_name);
viewHolder.age= (TextView) convertView.findViewById(R.id.list_age);
convertView.setTag(viewHolder);
}else {
viewHolder= (ViewHolder) convertView.getTag();
}
viewHolder.name.setText(""+list.get(position).getName());
viewHolder.age.setText(""+list.get(position).getAge());
System.out.println(position+"------"+convertView);
return convertView;
}
static class ViewHolder{
TextView name,age;
}
}
MainActivity:
public class MainActivity extends AppCompatActivity implements AbsListView.OnScrollListener {
final static int Data=1;
ArrayList<Person> list=new ArrayList<>();
int index;
ListView listView;
MyAdapter myAdapter;
View view;
int lastIndex;//可显示的最后一条数据的变量
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case Data:
myAdapter.notifyDataSetChanged();
break;
}
}
};
private void Initdate() {
for (int i=0;i<16;i++){
Person person=new Person();
person.setName("content");
person.setAge(""+index);
index++;
list.add(person);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Initdate();
listView= (ListView) findViewById(R.id.list_view);
view=getLayoutInflater().inflate(R.layout.low_view,null);
listView.addFooterView(view);
listView.setOnScrollListener(this);
myAdapter=new MyAdapter(this,list);
listView.setAdapter(myAdapter);
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {//状态改变触发
if(scrollState==SCROLL_STATE_IDLE&&myAdapter.getCount()==lastIndex ){
//当停止滑动,并且
new thread().start();
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
// firstVisibleItem 手机屏上显示的第一个item view 的ID
//visibleItemCount 手机屏上最多一次可以显示的Item 数
// totallItemCount 总item数,是list数据加上 “玩命加载”那个view,所以是item总数+1
lastIndex=firstVisibleItem+visibleItemCount-1;
Log.d("firstVisibleItem",""+firstVisibleItem);
Log.d("visibleItemCount",""+visibleItemCount);
Log.d("totalItemCount",""+totalItemCount);
Log.d("leng",""+(firstVisibleItem+visibleItemCount-1));
// first的id号+一次最多显示的多少-1才是最后Item的ID号
}
class thread extends Thread{
@Override
public void run() {
Initdate();// 重新加载数据
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.sendEmptyMessage(Data);
}
}
}