androidのXlist框架上拉下拉刷新案例 (不含缓存的)
1. 今天看了项目中使用XListView, 方便以后使用,做个笔记记录。
案例源码可以加扣扣群:191974931 ,方便你的学习。
点击查看更多,即可刷新操作。
案例中使用listview 为
<me.maxwin.view.XListView android:id="@+id/xListView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:cacheColorHint="#00000000" android:dividerHeight="1px" > </me.maxwin.view.XListView>首先要导入几个跟XlistView 相关的java代码。
上拉时候注意设置分页page =0. 再去查询。
下拉的时候page要实现page++, 进行分页查询。
这里数据是从服务器上获取的,详细代码如下:
public class MainActivity3 extends Activity implements IXListViewListener { static final int MENU_MANUAL_REFRESH = 0; static final int MENU_DISABLE_SCROLL = 1; static final int MENU_SET_MODE = 2; static final int MENU_DEMO = 3; // private List<Yilin> mListItems; private LinkedList<Comment> mListItems; private PullToRefreshListView mPullRefreshListView; private AIContentAdapter mAdapter; private static final int LOADING = 1; private static final int LOADING_COMPLETED = 2; private static final int LOADING_FAILED = 3; private static final int NORMAL = 4; private XListView mListView; public static final int FRIST_GET_DATE = 111; public static final int REFRESH_GET_DATE = 112; public static final int LOADMORE_GET_DATE = 113; private static int page = 0; private TextView textview; private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == 0) { geneItems((Integer)msg.obj); } } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_ptr_list3); textview=(TextView)findViewById(R.id.textview); mListItems = new LinkedList<Comment>(); mListView = (XListView) findViewById(R.id.xListView); mListView.setPullLoadEnable(true); loaddata(); mAdapter = new AIContentAdapter(MainActivity3.this, mListItems); mListView.setAdapter(mAdapter); mListView.setXListViewListener(MainActivity3.this); } private void loaddata() { new GetDataTask().execute(FRIST_GET_DATE); } private class GetDataTask extends AsyncTask<Integer, Void, LinkedList<Comment>> { @Override protected LinkedList<Comment> doInBackground(Integer... params) { // 处理完成后给handler发送消息 Message msg = new Message(); msg.what = 0; msg.obj=params[0]; handler.sendMessage(msg); return mListItems; } @Override protected void onPostExecute(LinkedList<Comment> result) { mAdapter.notifyDataSetChanged(); super.onPostExecute(result); } } // 加载,从网络端获取数据 public LinkedList<Comment> fetchData(final int Action) { if(!NetworkUtils.isNetworkAvailable(MainActivity3.this)){ mListView.setVisibility(View.GONE); textview.setVisibility(View.VISIBLE); return null; } // setState(LOADING); Log.e("blueberry", "fetchData begin page=" + page); BmobQuery<Comment> query = new BmobQuery<Comment>(); // 根据createdAt字段降序显示数据 query.order("-createdAt"); // 限制最多10条显示一页 query.setLimit(10); // 用于控制查询显示次数,去除会刷新重复显示个数。 query.setSkip(10 * (page++)); query.findObjects(MainActivity3.this, new FindListener<Comment>() { @Override public void onSuccess(List<Comment> list) { Log.i("blueberry", "查询成功 success.list个数=" + list.size()); if (list.size() != 0 && list.get(list.size() - 1) != null) { if (Action == REFRESH_GET_DATE) { Log.i("blueberry", "上刷新ListItems个数=" + mListItems.size()); mListItems.clear(); Log.i("blueberry", "上刷新清除后mListItems个数=" + mListItems.size()); } // 将网络段获取的数据list集合,加入创建的list对象中 Log.i("blueberry", "添加listview个数前=" + mListItems.size()); mListItems.addAll(list); Log.i("blueberry", "添加listview个数后=" + mListItems.size()); // setState(LOADING_COMPLETED); mAdapter.notifyDataSetChanged(); } else { Log.i("blueberry", "当前查询list数目为0 page=" + page); page--; // setState(LOADING_FAILED); } } @Override public void onError(int arg0, String arg1) { page--; // setState(LOADING_FAILED); } }); return mListItems; } // 设置获取直当前状态 public void setState(int state) { switch (state) { case LOADING: if (mListItems.size() == 0) { mListView.setVisibility(View.GONE); } break; case LOADING_COMPLETED: mPullRefreshListView.setVisibility(View.VISIBLE); break; case LOADING_FAILED: if (mListItems.size() == 0) { mPullRefreshListView.setVisibility(View.VISIBLE); } break; case NORMAL: break; default: break; } } class AIContentAdapter extends BaseAdapter { public static final String TAG = "AIContentAdapter"; private LinkedList<Comment> dataList; public AIContentAdapter(Context context, LinkedList<Comment> list) { super(); Log.i("blueberry", "AIContentAdapter 加载"); dataList = list; } @Override public int getCount() { return dataList.size(); } @Override public Object getItem(int arg0) { return arg0; } @Override public long getItemId(int arg1) { return arg1; } @Override public View getView(int position, View convertView, ViewGroup parent) { final ViewHolder viewHolder; if (convertView == null) { viewHolder = new ViewHolder(); convertView = LayoutInflater.from(MainActivity3.this).inflate( R.layout.yilin_list_item, null); viewHolder.item_title = (TextView) convertView .findViewById(R.id.tv_article_title); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } System.out.println("blueberry_datalist.size=" + dataList.size()); final Comment entity = dataList.get(position); // 显示单个title viewHolder.item_title.setText((position + 1) + " " + entity.getCommentContent()); return convertView; } } public class ViewHolder { public TextView item_title; } @Override public void onRefresh() { new GetDataTask().execute(REFRESH_GET_DATE); } @Override public void onLoadMore() { new GetDataTask().execute(LOADMORE_GET_DATE); } @SuppressLint("SimpleDateFormat") private void onLoad() { mListView.stopRefresh(); mListView.stopLoadMore(); SimpleDateFormat formatter = new SimpleDateFormat( "yyyy年MM月dd日 HH:mm:ss"); Date curDate = new Date(System.currentTimeMillis());// 获取当前时间 String str = formatter.format(curDate); mListView.setRefreshTime(str); } private void geneItems(final int ACTION) { if (ACTION == FRIST_GET_DATE) {// 第一次加载 fetchData(ACTION); mListView.setAdapter(mAdapter); } else if (ACTION == REFRESH_GET_DATE) {// 刷新数据 page = 0; mListItems = fetchData(ACTION); mAdapter.notifyDataSetChanged(); onLoad(); } else if (ACTION == LOADMORE_GET_DATE) {// 加载更多 mListItems = fetchData(ACTION); mAdapter.notifyDataSetChanged(); onLoad(); } } @Override protected void onDestroy() { super.onDestroy(); } }