Android中文API
安卓中文官网(android中文官网)-Android Studio Android sdk Android sdk下载 Android api
当前位置: 主页 > Android开发文档 >

Android 下拉刷新 PullToRefreshListView

时间:2017-08-03 11:44来源:未知 作者:卓一哥 点击:
主要用到 Android-PullToRefresh 地址: https://github.com/chrisbanes/Android-PullToRefresh 一、 MainActivity package com.fengshupo.mypackage; import android.os.Bundle; import android.app.Activity; import android.content.Intent; impo
主要用到 Android-PullToRefresh 
地址:https://github.com/chrisbanes/Android-PullToRefresh
 
一、MainActivity 
package com.fengshupo.mypackage;
 
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
 
public class MainActivity extends Activity {
 
private Button btnListView;
private Intent intent;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        btnListView=(Button)findViewById(R.id.btnListView);
        btnListView.setOnClickListener(new OnClickListener(){
 
@Override
public void onClick(View view) {
intent=new Intent(MainActivity.this,PullToRefreshListActivity.class);
startActivity(intent);
}});
    }
 
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}
 
二、PullToRefreshListActivity 
 
package com.fengshupo.mypackage;
 
import java.util.Arrays;
import java.util.LinkedList;
 
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
 
import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
 
public class PullToRefreshListActivity extends ListActivity {
 
private LinkedList<String> items;
private ArrayAdapter<String> adapter;
private String[] names={"周星星","刘诗诗"};
private PullToRefreshListView mPullRefreshListView;
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ptr_list);
 
mPullRefreshListView=(PullToRefreshListView)findViewById(R.id.pull_refresh_list);
mPullRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>(){
 
@Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
String msg=DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(), 
DateUtils.FORMAT_SHOW_YEAR|DateUtils.FORMAT_SHOW_DATE|DateUtils.FORMAT_SHOW_TIME|DateUtils.FORMAT_SHOW_DATE);
refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(msg);
new GetDataTask().execute();
}});
 
mPullRefreshListView.setOnLastItemVisibleListener(new OnLastItemVisibleListener(){
 
@Override
public void onLastItemVisible() {
Toast.makeText(PullToRefreshListActivity.this, "已经是末尾", Toast.LENGTH_SHORT).show();
}});
 
 
ListView actualListView = mPullRefreshListView.getRefreshableView();
items=new LinkedList<String>();
items.addAll(Arrays.asList(names));
adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,items);
actualListView.setAdapter(adapter);
}
 
private class GetDataTask extends AsyncTask<Void, Void, String[]> {
 
@Override
protected String[] doInBackground(Void... params) {
// Simulates a background job.
try {
items.addFirst(System.currentTimeMillis()+"");
Thread.sleep(4000);
} catch (InterruptedException e) {
}
return names;
}
 
@Override
protected void onPostExecute(String[] result) {
items.addFirst("-------这是更新的内容-------");
adapter.notifyDataSetChanged();
 
// Call onRefreshComplete when the list has been refreshed.
mPullRefreshListView.onRefreshComplete();
 
super.onPostExecute(result);
}
}
 
}
 
三、效果图

 

(责任编辑:卓一哥)
织梦二维码生成器
顶一下
(1)
100%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
推荐内容