public class MainActivity extends Activity implements Pulltoload_listener{
private List<String>list;
private int Max=80;
private PulltoRefreshView pulltoRefreshView;
private ArrayAdapter<String> adapter;
private MeasuredListview listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initdata();
initview();
}
private void initview() {
pulltoRefreshView = (PulltoRefreshView)findViewById(R.id.my_sc);
pulltoRefreshView.setPull(this);
pulltoRefreshView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
View centerView=View.inflate(this, R.layout.centerview, null);
listview = (MeasuredListview)centerView.findViewById(R.id.my_listview);
// listview.setSelection(ListView.FOCUS_DOWN);
listview.setFocusable(false);
pulltoRefreshView.setConterView(centerView);
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);
listview.setAdapter(adapter);
}
Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
if (msg.what==1) {
pulltoRefreshView.complate();
if (adapter.getCount()==Max) {
Toast.makeText(MainActivity.this, "已经滑到底部,暂无更新", 0).show();
listview.setSelection(Max);
pulltoRefreshView.complate();
}
adapter.notifyDataSetChanged();
}else if (msg.what==2) {
pulltoRefreshView.complate();
adapter.notifyDataSetChanged();
}
};
};
private void initdata() {
// TODO Auto-generated method stub
list=new ArrayList<String>();
for (int i = 0; i <50; i++) {
list.add("item"+i);
}
}
//上拉加载
@Override
public void Load() {
// TODO Auto-generated method stub
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
Thread.sleep(1500);
int count=adapter.getCount();
if ((count+5)<Max) {
for (int i =count; i < count+5; i++) {
list.add("上拉新数据"+i);
}
}else{
for (int i =count; i <Max; i++) {
list.add("上拉新数据"+i);
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.sendEmptyMessage(1);
}
}).start();
}
//刷新
@Override
public void Refrash() {
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
Thread.sleep(1500);
for (int i = 5; i>0; i--) {
list.add(0,i+"");
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
handler.sendEmptyMessage(2);
}
}).start();
}
}
网友评论