XListView 的使用:
XML代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f00"
android:orientation="horizontal" >
<com.qy.xlistview.XListView
android:id="@+id/xlistview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.qy.xlistview.XListView>
</LinearLayout>
两个条目:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:src="@mipmap/ic_launcher"
/>
<TextView
android:id="@+id/text"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toLeftOf="@id/image"
/>
</android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ImageView
android:id="@+id/image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
/>
<TextView
android:id="@+id/text2"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toRightOf="@+id/image2"
app:layout_constraintRight_toRightOf="parent"
/>
</android.support.constraint.ConstraintLayout>
java代码:
package com.guo.xiangmu.frag;
import com.guo.xiangmu.HttpUtils;
import com.guo.xiangmu.R;
import com.guo.xiangmu.adapter.MyAdapter;
import com.guo.xiangmu.json.JsonBean;
import com.guo.xiangmu.sql.Dao;
import com.google.gson.Gson;
import com.qy.xlistview.XListView;
import android.content.ContentValues;
import android.database.Cursor;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class Frag_01 extends BaseFramt{
private XListView xListView;
private ImageView imageView;
private TextView textView;
private Dao dao;
private List<JsonBean.DataBean> data;
private JsonBean jsonBean;
private MyAdapter myAdapter;
//布局
@Override
protected int bindLayout() {
return R.layout.frag_01;
}
//控件
@Override
protected void initView() {
xListView = bindView(R.id.xlistview);
imageView = bindView(R.id.image);
textView = bindView(R.id.text);
dao = new Dao(getActivity());
xListView.setPullLoadEnable(true);
xListView.setXListViewListener(new XListView.IXListViewListener() {
//刷新
@Override
public void onRefresh() {
HttpUtils.isNetWord(getActivity());
onLoad();
}
//加载
@Override
public void onLoadMore() {
if (HttpUtils.isNetWord(getActivity())){
HttpUtils.MyAsyncTask myAsyncTask = new HttpUtils.MyAsyncTask();
myAsyncTask.setBacks(new HttpUtils.MyAsyncTask.CallBacks() {
@Override
public void getData(String s) {
Gson gson = new Gson();
jsonBean = gson.fromJson(s, JsonBean.class);
List<JsonBean.DataBean> list= jsonBean.getData();
data.addAll(list);
//刷新适配器
myAdapter.notifyDataSetChanged();
Toast.makeText(getActivity(), "获取数据成功!", Toast.LENGTH_SHORT).show();
}
});
myAsyncTask.execute("http://api.expoon.com/AppNews/getNewsList/type/1/p/1");
}else{
Toast.makeText(getActivity(), "网络连接失败!", Toast.LENGTH_SHORT).show();
}
onLoad();
}
});
}
//操作
@Override
protected void initData() {
if (HttpUtils.isNetWord(getActivity())){
HttpUtils.MyAsyncTask myAsyncTask = new HttpUtils.MyAsyncTask();
myAsyncTask.setBacks(new HttpUtils.MyAsyncTask.CallBacks() {
@Override
public void getData(String s) {
Gson gson = new Gson();
jsonBean = gson.fromJson(s, JsonBean.class);
data = jsonBean.getData();
Toast.makeText(getActivity(), "获取数据成功!", Toast.LENGTH_SHORT).show();
myAdapter = new MyAdapter(getActivity(), data);
xListView.setAdapter(myAdapter);
if (dao.select("bw",null,null,null,null,null,null ).moveToFirst()){
Toast.makeText(getActivity(), "数据库中没有数据!", Toast.LENGTH_SHORT).show();
}else {
for (int i = 0;i< data.size();i++){
ContentValues contentValues = new ContentValues();
contentValues.put("news_tiele",data.get(i).getNews_title());
contentValues.put("news_url",data.get(i).getPic_url());
dao.add("bw",null,contentValues);
}
}
}
});
myAsyncTask.execute("http://api.expoon.com/AppNews/getNewsList/type/1/p/1");
}else {
Cursor bw = dao.select("bw", null, null, null, null, null, null);
data = new ArrayList<>();
if (bw.moveToFirst()){
do {
String news_tiele = bw.getString(bw.getColumnIndex("news_tiele"));
String pic_url = bw.getString(bw.getColumnIndex("pic_url"));
data.add(new JsonBean.DataBean(news_tiele,pic_url));
}while (bw.moveToNext());
}
myAdapter = new MyAdapter(getActivity(), data);
xListView.setAdapter(myAdapter);
}
}
//设置监听
@Override
protected void bindEvent() {
}
//加载监听
public void onLoad() {
//停止加载,重置头视图
xListView.stopLoadMore();
//停止加载,重置脚视图
xListView.stopRefresh();
//设置刷新时间
xListView.setRefreshTime("刚刚刷新了!");
}
}