Xml
<me.maxwin.view.XListView
android:id="@+id/xListView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
适配器的布局item
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/imageView"
app:layout_constraintTop_toTopOf="@id/imageView"
app:layout_constraintBottom_toBottomOf="@id/imageView"
/>
<ImageView
android:id="@+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
app:layout_constraintRight_toRightOf="parent"
/>
MainActivity
private XListView xListView;
private Button button;
private int mPager;
private String mUrl="http://i.jandan.net/?oxwlxojflwblxbsapi=get_recent_posts&include=url,date,tags,author,title,excerpt,comment_count,comment_status,custom_fields&page=1&custom_fields=thumb_c,views&dev=%d";
private IPresenterImpl iPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
xListView=findViewById(R.id.xListView);
mPager=1;
iPresenter=new IPresenterImpl(this);
initView();
}
private void initView() {
checkPermissions();
xListView.setPullLoadEnable(true);
xListView.setPullRefreshEnable(true);
xListView.setXListViewListener(new XListView.IXListViewListener() {
@Override
public void onRefresh() {
mPager=1;
startRequest();
}
@Override
public void onLoadMore() {
startRequest();
}
});
startRequest();
}
@Override
protected void onDestroy() {
super.onDestroy();
if(iPresenter!=null){
iPresenter.onDetach();
}
}
private void checkPermissions() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.INTERNET}, 100);
} else {
startRequest();
}
} else {
startRequest();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//如果requestCode匹配,切权限申请通过
if (requestCode == 100 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
startRequest();
}
}
private void startRequest() {
iPresenter.startRequest(String.format(mUrl,mPager), UserBean.class);
}
@Override
public void showResponseData(Object data) {
UserBean userBean = (UserBean) data;
MineAdapter adapter = new MineAdapter(this);
xListView.setAdapter(adapter);
if(mPager==1){
adapter.setDatas(userBean.getPosts());
}else{
adapter.addDatas(userBean.getPosts());
}
mPager++;
xListView.stopRefresh();
xListView.stopLoadMore();
//Tmoast.makeText(this, String.valueOf(userBean.getPosts()), Toast.LENGTH_SHORT).show();
}
Adapter
public class MineAdapter extends BaseAdapter {
private Context mContext;
private List<UserBean.PostsBean> userBeans;
public MineAdapter(Context mContext) {
this.mContext = mContext;
userBeans=new ArrayList<>();
}
public void setDatas(List<UserBean.PostsBean> muserBeans) {
// this.userBeans = userBeans;
userBeans.clear();
if(muserBeans!=null){
userBeans=muserBeans;
}
notifyDataSetChanged();
}
public void addDatas(List<UserBean.PostsBean> muserBeans) {
// this.userBeans = userBeans;
//userBeans.clear();
if(muserBeans!=null){
userBeans=muserBeans;
}
notifyDataSetChanged();
}
@Override
public int getCount() {
return userBeans.size();
}
@Override
public UserBean.PostsBean getItem(int position) {
return userBeans.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vh;
if(convertView==null){
convertView=LayoutInflater.from(mContext).inflate(R.layout.item,parent,false);
vh=new ViewHolder();
vh.textView=convertView.findViewById(R.id.textView);
vh.imageView=convertView.findViewById(R.id.imageView);
convertView.setTag(vh);
}else{
vh= (ViewHolder) convertView.getTag();
}
vh.textView.setText(getItem(position).getTitle());
ImageLoader.getInstance().displayImage(getItem(position).getCustom_fields().getThumb_c().get(0),vh.imageView);
return convertView;
}
class ViewHolder{
TextView textView;
ImageView imageView;
}
接口 CallBack
public interface MyCallBack {
void setData(T data);
}
Model层
public interface IModel {
void startRequest(String url, Class clazz, MyCallBack myCallBack);
}
model层的实现类
public class IModelImpl implements IModel {
private MyCallBack callBack;
public <T> T reQuest(String url,Class clazz){
return (T) new Gson().fromJson(HttpUtils.reQuest(url),clazz);
}
@Override
public void startRequest(String url, final Class clazz, final MyCallBack myCallBack) {
new AsyncTask<String,Void,Object>(){
@Override
protected Object doInBackground(String... strings) {
return reQuest(strings[0],clazz);
}
@Override
protected void onPostExecute(Object o) {
myCallBack.setData(o);
}
}.execute(url);
}
}
Iview 接口
public interface IView {
void showResponseData(T data);
}
Persenter层
public interface IPresenter {
void startRequest(String url,Class clazz);
}
persenter层的实现类
public class IPresenterImpl implements IPresenter {
private IView iView;
private IModelImpl model;
public IPresenterImpl(IView iView){
this.iView=iView;
model=new IModelImpl();
}
@Override
public void startRequest(String url, Class clazz) {
model.startRequest(url, clazz, new MyCallBack() {
@Override
public void setData(Object data) {
iView.showResponseData(data);
}
});
}
public void onDetach(){
if(model!=null){
model=null;
}
if(iView!=null){
iView=null;
}
}
工具类 Util
public static String reQuest(String url){
String result="";
try {
URL url1=new URL(url);
HttpURLConnection urlConnection = (HttpURLConnection) url1.openConnection();
urlConnection.setReadTimeout(3000);
urlConnection.setConnectTimeout(3000);
int responseCode = urlConnection.getResponseCode();
if(responseCode==200){
result=Stream2String(urlConnection.getInputStream());
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
private static String Stream2String(InputStream inputStream) throws IOException {
StringBuilder stringBuilder = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
for (String tmp=bufferedReader.readLine();tmp!=null;tmp=bufferedReader.readLine()){
stringBuilder.append(tmp);
}
return stringBuilder.toString();
}
UserBean
public class UserBean<T> {
private String status;
private int count;
private int count_total;
private int pages;
private List<PostsBean> posts;
private T data;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getCount_total() {
return count_total;
}
public void setCount_total(int count_total) {
this.count_total = count_total;
}
public int getPages() {
return pages;
}
public void setPages(int pages) {
this.pages = pages;
}
public List<PostsBean> getPosts() {
return posts;
}
public void setPosts(List<PostsBean> posts) {
this.posts = posts;
}
public static class PostsBean {
/**
* id : 100258
* url : http://i.jandan.net/2018/12/04/old-water.html
* title : 放了一天的水能喝吗?
* excerpt : 此外,味道会变吗?
* date : 2018-12-04 15:00:18
* tags : [{"id":831,"slug":"%e5%81%a5%e5%ba%b7","title":"健康","description":"","post_count":1449}]
* author : {"id":716,"slug":"110","name":"HTT110","first_name":"","last_name":"","nickname":"HTT110","url":"","description":""}
* comment_count : 9
* comment_status : open
* custom_fields : {"thumb_c":["http://img.jandan.net/news/2018/12/164c5c03a467946503ff25757f5bbdd8.jpg"]}
*/
private int id;
private String url;
private String title;
private String excerpt;
private String date;
private AuthorBean author;
private int comment_count;
private String comment_status;
private CustomFieldsBean custom_fields;
private List<TagsBean> tags;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getExcerpt() {
return excerpt;
}
public void setExcerpt(String excerpt) {
this.excerpt = excerpt;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public AuthorBean getAuthor() {
return author;
}
public void setAuthor(AuthorBean author) {
this.author = author;
}
public int getComment_count() {
return comment_count;
}
public void setComment_count(int comment_count) {
this.comment_count = comment_count;
}
public String getComment_status() {
return comment_status;
}
public void setComment_status(String comment_status) {
this.comment_status = comment_status;
}
public CustomFieldsBean getCustom_fields() {
return custom_fields;
}
public void setCustom_fields(CustomFieldsBean custom_fields) {
this.custom_fields = custom_fields;
}
public List<TagsBean> getTags() {
return tags;
}
public void setTags(List<TagsBean> tags) {
this.tags = tags;
}
public static class AuthorBean {
/**
* id : 716
* slug : 110
* name : HTT110
* first_name :
* last_name :
* nickname : HTT110
* url :
* description :
*/
private int id;
private String slug;
private String name;
private String first_name;
private String last_name;
private String nickname;
private String url;
private String description;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getSlug() {
return slug;
}
public void setSlug(String slug) {
this.slug = slug;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
public static class CustomFieldsBean {
private List<String> thumb_c;
public List<String> getThumb_c() {
return thumb_c;
}
public void setThumb_c(List<String> thumb_c) {
this.thumb_c = thumb_c;
}
}
public static class TagsBean {
/**
* id : 831
* slug : %e5%81%a5%e5%ba%b7
* title : 健康
* description :
* post_count : 1449
*/
private int id;
private String slug;
private String title;
private String description;
private int post_count;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getSlug() {
return slug;
}
public void setSlug(String slug) {
this.slug = slug;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getPost_count() {
return post_count;
}
public void setPost_count(int post_count) {
this.post_count = post_count;
}
}
}
App的获取图片
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
ImageLoader.getInstance().init(
new ImageLoaderConfiguration.Builder(this)
.memoryCacheSizePercentage(50)
.diskCacheSize(10*1024*1024)
.defaultDisplayImageOptions(
new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.displayer(new RoundedBitmapDisplayer(8))
.build()
).build()
);
}
}