//页面布局
<?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.handmark.pulltorefresh.library.PullToRefreshListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/pullListview"
></com.handmark.pulltorefresh.library.PullToRefreshListView>
</android.support.constraint.ConstraintLayout>
//第一个子条目的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="wrap_content"
android:orientation="vertical“>
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:id="@+id/Icon"
/>
</LinearLayout>
//第二个子条目
<?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="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="df"
android:textSize="25dp"
android:textColor="@color/colorAccent"
android:id="@+id/news_title"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="df"
android:textSize="25dp"
android:textColor="@color/colorPrimary"
android:id="@+id/news_ctime"
/>
</LinearLayout>
//App
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
//初始化
ImageLoader.getInstance().init(
new ImageLoaderConfiguration.Builder(this)
//图片缓存后的大小
.diskCacheSize(50*1024*1024)
//内存缓存的最大值 百分数
.memoryCacheSizePercentage(13)
.defaultDisplayImageOptions(
// 通过DisplayImageOptions这个类 设置图片的各种格式
new DisplayImageOptions.Builder()
.cacheInMemory(true)//设置下载的图片是否缓存在内存中
.cacheOnDisc(true)//设置下载的图片是否缓存在SD卡中
.bitmapConfig(Bitmap.Config.ARGB_4444)//设置色彩模式
//设置下载过程中图片显示
.showImageOnLoading(R.mipmap.ic_launcher)
//设置图片加载或解码过程中发生错误显示的图片
.showImageOnFail(R.mipmap.ic_launcher)
//设置图片Uri为空或是错误的时候显示的图片
.showImageForEmptyUri(R.mipmap.ic_launcher)
//设置圆角
.displayer(new RoundedBitmapDisplayer(16))
.build()
)
.build()
);
}
//多条目的适配器
public class NewsAdapter extends BaseAdapter {
private List<Bean.NewslistBean> mlist;
private Context context;
private final int ITEM_COUNT = 2;
private final int ITEM_TYPE1 = 0;
private final int ITEN_TYPE2 = 1;
public NewsAdapter(Context context) {
this.context = context;
mlist = new ArrayList<>();
}
public void setMlist(List<Bean.NewslistBean> list) {
mlist.clear();
if (list != null){
mlist.addAll(list);
}
notifyDataSetChanged();
}
public void addMlist(List<Bean.NewslistBean> list) {
if (list != null){
mlist.addAll(list);
}
notifyDataSetChanged();
}
@Override
public int getViewTypeCount() {
return ITEM_COUNT;
}
@Override
public int getItemViewType(int position) {
return position % 2;
}
@Override
public int getCount() {
return mlist.size();
}
@Override
public Bean.NewslistBean getItem(int position) {
return mlist.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
NewViewHolder holder;
if (convertView == null){
convertView = View.inflate(context,
getItemViewType(position)==ITEM_TYPE1?
R.layout.item:R.layout.itme
,null);
holder = new NewViewHolder(convertView);
}else {
holder = (NewViewHolder) convertView.getTag();
}
if (getItemViewType(position)== ITEM_TYPE1){
holder.bindData2(getItem(position));
}else{
holder.bindData1(getItem(position));
}
return convertView;
}
class NewViewHolder{
ImageView Icon;
TextView news_title,news_ctime;
public NewViewHolder(View item){
Icon = item.findViewById(R.id.Icon);
news_title = item.findViewById(R.id.news_title);
news_ctime = item.findViewById(R.id.news_ctime);
item.setTag(this);
}
public void bindData1(Bean.NewslistBean data){
news_ctime.setText(data.getCtime());
news_title.setText(data.getTitle());
}
public void bindData2(Bean.NewslistBean data){
ImageLoader.getInstance().displayImage(data.getPicUrl(),Icon);
}
}
}
//主页
package com.example.lianxi2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshWebView;
public class MainActivity extends AppCompatActivity {
private PullToRefreshListView pullListView;
private int pager;
private NewsAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pullListView = findViewById(R.id.pullListview);
adapter = new NewsAdapter(this);
pullListView.setAdapter(adapter);
pager = 1;
pullListView.setMode(PullToRefreshBase.Mode.BOTH);
pullListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
pager = 1;
initData();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
initData();
}
});
initData();
}
private String urlStr= "http://api.tianapi.com/meinv/?key=605fa0a929c8d8b116424df0510c6b2a&num=95";
private void initData(){
NetUtils.getInstance().getRequset(String.format(urlStr), Bean.class, new NetUtils.Callbak<Bean>() {
@Override
public void onSuccess(Bean bean) {
if (pager == 1){
adapter.setMlist(bean.getNewslist());
}else {
adapter.addMlist(bean.getNewslist());
}
pager++;
pullListView.onRefreshComplete();
}
});
}
}
//工具类
package com.example.lianxi2;
import android.os.AsyncTask;
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class NetUtils {
//初始化一个GSon
private Gson gson;
//单例
private static NetUtils instance;
//创建私有构造
private NetUtils(){
//实例化一个GSON
gson = new Gson();
}
//提供静态方法让外面获取实例对象
public static NetUtils getInstance(){
if (instance == null){
instance = new NetUtils();
}
return instance;
}
//定义一个接口
public interface Callbak<T>{
void onSuccess(T t);
}
//接口回调
public void getRequset(final String urlStr,final Class clazz,final Callbak callbak){
new AsyncTask<String,Void,Object>(){
@Override
protected Object doInBackground(String... strings) {
return getRequset(urlStr,clazz);
}
@Override
protected void onPostExecute(Object o) {
callbak.onSuccess(o);
}
}.execute(urlStr);
}
//GSON解析的方法
public <T> T getRequset(String urlStr,Class clazz){
//gson 解析数据 解析后的数据传给clazz
return (T) gson.fromJson(getRequset(urlStr),clazz);
}
//创建网络请求方法
public String getRequset(String urlStr){
String result = "";
try {
URL url = new URL(urlStr);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setConnectTimeout(5000);
urlConnection.setReadTimeout(5000);
int responseCode = urlConnection.getResponseCode();
if (responseCode == 200){
result = stream2String(urlConnection.getInputStream());
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
private String stream2String(InputStream inputStream) throws IOException {
InputStreamReader is = new InputStreamReader(inputStream);
BufferedReader br = new BufferedReader(is);
StringBuilder sb = new StringBuilder();
for (String temp = br.readLine() ; temp != null; temp = br.readLine()){
sb.append(temp);
}
return sb.toString();
}
}