輪播圖+ListView多條目展示(輪播圖暫無)
阿新 • • 發佈:2018-12-20
MainActivity_UI
package com.example.demo.ui.activity; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import com.example.demo.R; import com.example.demo.model.BannerResponse; import com.example.demo.model.NewsResponse; import com.example.demo.ui.adapter.ContentsAdapter; import com.example.demo.utils.NetUtils; import me.maxwin.view.XListView; public class MainActivity extends AppCompatActivity { private XListView contents; private ContentsAdapter mAdapter; private int mPage; private final int LOAD_COUNT = 10; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mPage = 1; contents = findViewById(R.id.contents); mAdapter = new ContentsAdapter(this); contents.setAdapter(mAdapter); contents.setPullRefreshEnable(true); contents.setPullLoadEnable(true); contents.setXListViewListener(new XListView.IXListViewListener() { @Override public void onRefresh() { mPage = 1; loadData(); } @Override public void onLoadMore() { mPage++; loadData(); } }); loadData(); } private void loadData() { if(mPage == 1) { loadBannerData(); loadNesData(); } else { loadNesData(); } } private void loadBannerData() { NetUtils.getInstance().getRequest(bannerUrl, BannerResponse.class, new NetUtils.Callback<BannerResponse>() { @Override public void onSuccess(BannerResponse o) { mAdapter.setBanner(o.getData()); } }); } private void loadNesData() { NetUtils.getInstance().getRequest(String.format(newsUrl, LOAD_COUNT, mPage), NewsResponse.class, new NetUtils.Callback<NewsResponse>() { @Override public void onSuccess(NewsResponse o) { if(mPage == 1) { mAdapter.setDatas(o.getResult()); } else { mAdapter.addDatas(o.getResult()); } //停止重新整理載入 contents.stopRefresh(); contents.stopLoadMore(); if(o.getResult().size() < LOAD_COUNT) { contents.setPullLoadEnable(false); } } }); } //banner url private String bannerUrl = "http://www.zhaoapi.cn/quarter/getAd"; //news url private String newsUrl = "http://172.17.8.100/movieApi/movie/v1/findHotMovieList?count=%d&page=%d"; }
ViewAdapter_介面卡
package com.example.demo.ui.adapter; import android.content.Context; import android.support.v4.view.ViewPager; 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.example.demo.R; import com.example.demo.model.BannerResponse; import com.example.demo.model.NewsResponse; import com.nostra13.universalimageloader.core.ImageLoader; import java.util.ArrayList; import java.util.List; public class ContentsAdapter extends BaseAdapter { //banner資料 private List<BannerResponse.DataBean> mBanner; private List<NewsResponse.ResultBean> mDatas; private Context mContext; public ContentsAdapter(Context context) { this.mContext = context; mBanner = new ArrayList<>(); mDatas = new ArrayList<>(); } //更新banner public void setBanner(List<BannerResponse.DataBean> banner) { mBanner.clear(); if (banner != null) { mBanner.addAll(banner); } notifyDataSetChanged(); } //更新新聞資料 public void setDatas(List<NewsResponse.ResultBean> datas) { mDatas.clear(); if(datas != null) { mDatas.addAll(datas); } notifyDataSetChanged(); } //追加新聞資料 public void addDatas(List<NewsResponse.ResultBean> datas) { if(datas != null) { mDatas.addAll(datas); } notifyDataSetChanged(); } @Override public int getCount() { //return mDatas.size() + 1; return hasBanner() ? mDatas.size() + 1 : mDatas.size(); } //是否有banner private boolean hasBanner() { return mBanner.size() > 0; } @Override public NewsResponse.ResultBean getItem(int position) { return mDatas.get(hasBanner() ? position - 1 : position); } @Override public long getItemId(int position) { return position; } private final int ITEM_COUNT = 2; private final int BANNER_TYPE = 0; private final int NEWS_TYPE = 1; @Override public int getViewTypeCount() { //banner + news return ITEM_COUNT; } @Override public int getItemViewType(int position) { //return super.getItemViewType(position); return position == 0 && hasBanner() ? BANNER_TYPE : NEWS_TYPE; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (getItemViewType(position) == BANNER_TYPE) { BannerViewHolder viewHolder; if (convertView == null) { convertView = LayoutInflater.from(mContext).inflate(R.layout.item_banner, parent, false); viewHolder = new BannerViewHolder(convertView); } else { viewHolder = (BannerViewHolder) convertView.getTag(); } viewHolder.bindData(mBanner); } else { NewsViewHolder viewHolder; if (convertView == null) { convertView = LayoutInflater.from(mContext).inflate(R.layout.item_news, parent, false); viewHolder = new NewsViewHolder(convertView); } else { viewHolder = (NewsViewHolder) convertView.getTag(); } viewHolder.bindData(getItem(position)); } return convertView; } class BannerViewHolder { ViewPager viewPager; public BannerViewHolder(View itemView) { viewPager = itemView.findViewById(R.id.banner); itemView.setTag(this); } public void bindData(List<BannerResponse.DataBean> datas) { //TODO } } class NewsViewHolder { ImageView icon; TextView title; TextView date; public NewsViewHolder(View itemView) { icon = itemView.findViewById(R.id.icon); title = itemView.findViewById(R.id.title); date = itemView.findViewById(R.id.date); itemView.setTag(this); } public void bindData(NewsResponse.ResultBean data) { title.setText(data.getName()); date.setText(data.getSummary()); ImageLoader.getInstance().displayImage(data.getImageUrl(), icon); } } }
NetUtil_工具類
package com.example.demo.utils; import android.content.Context; 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 { private static NetUtils instance; private Gson gson; private NetUtils() { gson = new Gson(); } public static NetUtils getInstance() { if(instance == null) { instance = new NetUtils(); } return instance; } public interface Callback<T> { void onSuccess(T t); } public void getRequest(final String urlStr, final Class clazz, final Callback callback) { new AsyncTask<String, Void, Object>(){ @Override protected Object doInBackground(String... strings) { return getRequest(urlStr, clazz); } @Override protected void onPostExecute(Object o) { callback.onSuccess(o); } }.execute(urlStr); } //是否有網路 public static boolean hasNetwork(Context context) { return true; } public <T> T getRequest(String urlStr, Class clazz) { return (T) gson.fromJson(getRequest(urlStr), clazz); } public String getRequest(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 is) throws IOException { StringBuilder sb = new StringBuilder(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); for (String tmp = br.readLine(); tmp != null ; tmp = br.readLine()) { sb.append(tmp); } return sb.toString(); } }
App類(ImageLoader初始化)
package com.example.demo;
import android.app.Application;
import android.graphics.Bitmap;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import java.io.InputStream;
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(
new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.bitmapConfig(Bitmap.Config.ARGB_4444)
.showImageOnLoading(R.mipmap.ic_launcher)
.showImageOnFail(R.mipmap.ic_launcher)
.showImageForEmptyUri(R.mipmap.ic_launcher)
.displayer(new RoundedBitmapDisplayer(16))
.build()
)
.build()
);
}
}
item_news.xml(新聞)
<?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="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ImageView
android:id="@+id/icon"
android:layout_width="140dp"
android:layout_height="100dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/title"
android:text="text"
app:layout_constraintTop_toTopOf="@id/icon"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/icon"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/date"
android:text="text"
android:maxLines="3"
app:layout_constraintBottom_toBottomOf="@id/icon"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/icon"
/>
</android.support.constraint.ConstraintLayout>