清单文件配置
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.duotiaomu">
<uses-permission android:name="android.permission.INTERNET"/> (网络请求)
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:name=".App" // 获取.App的类
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
package com.example.duotiaomu;
import android.app.Application;
import com.nostra13.universalimageloader.core.ImageLoader;
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
ImageLoader.getInstance().init(ImageConfs.getcon(this));
}
}
package com.example.duotiaomu;
import android.content.Context;
import android.graphics.Bitmap;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.CircleBitmapDisplayer;
public class ImageConfs {
public static ImageLoaderConfiguration getcon(Context context){
ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(context)
.memoryCacheSizePercentage(10)
.diskCacheSize(5010241024)
.defaultDisplayImageOptions(DisplayImageOptions.createSimple())
.writeDebugLogs()
.build();
return configuration;
}
public static DisplayImageOptions getpon(){
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheOnDisk(true)
.cacheInMemory(true)
.showImageOnLoading(R.mipmap.ic_launcher)
.showImageOnFail(R.drawable.ic_launcher_background)
.bitmapConfig(Bitmap.Config.RGB_565)
.displayer(new CircleBitmapDisplayer())
.build();
return options;
}
}
package com.example.duotiaomu;
import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import com.example.xlist.me.maxwin.view.XListView;
import com.google.gson.Gson;
import java.util.Date;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private XListView xlv;
private Myadapter ma;
private int page =1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
xlv = findViewById(R.id.xlv);
ma = new Myadapter(this);
xlv.setAdapter(ma);
xlv.setPullLoadEnable(true);
xlv.setXListViewListener(new XListView.IXListViewListener() {
@Override
public void onRefresh() {
page = 1;
LoadDate(page);
}
@Override
public void onLoadMore() {
LoadDate(page);
}
});
canloadmore(true);
LoadDate(page);
}
private void canloadmore(boolean b) {
xlv.setPullLoadEnable(b);
}
@SuppressLint("StaticFieldLeak")
private void LoadDate(int page) {
final String path = "http://172.17.8.100/movieApi/movie/v1/findHotMovieList?page="+page+"&count=3";
String Urlwith = path+page;
new AsyncTask<String,String,List<Person.ResultDate>>(){
@Override
protected List<Person.ResultDate> doInBackground(String... strings) {
Person dian = null;
String httpcon = new Util().httpcon(strings[0]);
dian = new Gson().fromJson(httpcon,Person.class);
return dian == null ? null:dian.getResult();
}
@Override
protected void onPostExecute(List<Person.ResultDate> resultDates) {
super.onPostExecute(resultDates);
if(resultDates == null){
Toast.makeText(MainActivity.this,"请求数据失败",Toast.LENGTH_SHORT).show();
return;
}
updateData(resultDates);
loade();
canloadmore(resultDates.size() == 3);
}
}.execute(Urlwith);
}
private void loade() {
page++;
xlv.stopRefresh();
xlv.stopLoadMore();
}
private void updateData(List<Person.ResultDate> resultDates) {
if(page == 1){
ma.setDats(resultDates);
Date date = new Date();
xlv.setRefreshTime(date.toLocaleString());
}else{
ma.addDats(resultDates);
}
}
}
适配器要重写两个方法
package com.example.duotiaomu;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.ArrayList;
import java.util.List;
public class Myadapter extends BaseAdapter {
List<Person.ResultDate> list;
Context context;
public void addDats(List<Person.ResultDate> dates){
if(dates !=null){
list.addAll(dates);
notifyDataSetChanged();
}
}
public void setDats(List<Person.ResultDate> dates){
list = dates;
notifyDataSetChanged();
}
public Myadapter(Context context) {
this.context = context;
list = new ArrayList<>();
}
public void setList(List<Person.ResultDate> list){
this.list = list;
notifyDataSetChanged();
}
(方法1)
@Override
public int getItemViewType(int position) {
if(position % 2 == 0){
return 0;
}else{
return 1;
}
}
(方法2)
@Override
public int getViewTypeCount() {
return 2; //这个是你要返回多少条目
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(getItemViewType(position) == 0){
ViewHolde vh;
if(convertView == null){
convertView = LayoutInflater.from(context).inflate(R.layout.list_text1,null);
vh = new ViewHolde(convertView);
}else{
vh = (ViewHolde) convertView.getTag();
}
Person.ResultDate data = (Person.ResultDate) getItem(position);
vh.BindDate(data);
}else{
ViewHolde1 vh;
if(convertView == null){
convertView = LayoutInflater.from(context).inflate(R.layout.list_text2,null);
vh = new ViewHolde1(convertView);
}else{
vh = (ViewHolde1) convertView.getTag();
}
Person.ResultDate data = (Person.ResultDate) getItem(position);
vh.BindDate(data);
}
return convertView;
}
class ViewHolde{
TextView t1;
ImageView image;
public ViewHolde(View view) {
t1 = view.findViewById(R.id.t1);
image = view.findViewById(R.id.image);
view.setTag(this);
}
public void BindDate(Person.ResultDate data){
t1.setText(data.getName());
ImageLoader.getInstance().displayImage(data.getImageUrl(),image,ImageConfs.getpon());
}
}
class ViewHolde1{
TextView t1;
ImageView image1;
ImageView image2;
public ViewHolde1(View view) {
t1 = view.findViewById(R.id.t1);
image1 = view.findViewById(R.id.image1);
image2 = view.findViewById(R.id.image2);
view.setTag(this);
}
public void BindDate(Person.ResultDate data){
t1.setText(data.getName());
ImageLoader.getInstance().displayImage(data.getImageUrl(),image1,ImageConfs.getpon());
ImageLoader.getInstance().displayImage(data.getImageUrl(),image2,ImageConfs.getpon());
}
}
}
//这是json解析数据写成一个类
package com.example.duotiaomu;
import java.util.List;
public class Person {
public List result;
public String message;
public int status;
public List<ResultDate> getResult() {
return result;
}
public void setResult(List<ResultDate> result) {
this.result = result;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public class ResultDate{
public String name;
public String summary;
public String imageUrl;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
}
}
提取工具类
package com.example.duotiaomu;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class Util {
public String httpcon(String urls){
try {
URL url = new URL(urls);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setReadTimeout(5000);
httpURLConnection.setConnectTimeout(5000);
httpURLConnection.setRequestMethod("GET");
int request = httpURLConnection.getResponseCode();
if(request == 200){
String str = String2(httpURLConnection.getInputStream());
return str;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private String String2(InputStream inputStream) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
String text = null;
while ((text = bufferedReader.readLine()) != null){
stringBuilder.append(text);
}
bufferedReader.close();
return stringBuilder.toString();
}
}
剩下的 是布局文件
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app=“http://schemas.android.com/apk/res-auto”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=".MainActivity">
<com.example.xlist.me.maxwin.view.XListView
android:id="@+id/xlv"
android:layout_width="match_parent"
android:layout_height="match_parent"></com.example.xlist.me.maxwin.view.XListView>
</android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?><TextView
android:id="@+id/t1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="30dp"
android:text="你好"
/>
<ImageView
android:id="@+id/image"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/ic_launcher_background"
android:layout_marginLeft="40dp"
/>
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@+id/t1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="发福"
android:layout_marginTop="20dp"
/>
<ImageView
android:id="@+id/image1"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/ic_launcher_background"
android:layout_marginTop="50dp"
/>
<ImageView
android:id="@+id/image2"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/ic_launcher_background"
android:layout_marginTop="50dp"
android:layout_marginLeft="120dp"
/>