banner輪播、
匯入依賴 dependencies{
implementation ‘com.youth.banner:banner:1.4.10’
}
main_activity
<?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=“
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=".MainActivity">
<com.youth.banner.Banner android:id="@+id/banner" android:layout_width="0dp" android:layout_height="200dp" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:indicator_width="8dp" app:indicator_height="8dp" app:indicator_drawable_selected="@drawable/shape_sel_dot" app:indicator_drawable_unselected="@drawable/shape_normal_dot" /> <me.maxwin.view.XListView android:id="@+id/xListView" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@id/banner" app:layout_constraintBottom_toBottomOf="parent" />
</android.support.constraint.ConstraintLayout>
image_item
<?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”
android:padding=“10dp”
xmlns:app=“
<ImageView
android:id="@+id/image"
android:layout_width="160dp"
android:layout_height="100dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
<TextView
android:id="@+id/news_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="@id/image"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="@id/news_summary"/>
<TextView
android:id="@+id/news_summary"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@id/image"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/news_title"
android:layout_marginTop="30dp"/>
</android.support.constraint.ConstraintLayout>
text_item
<?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”
android:padding=“10dp”
xmlns:app=“http://schemas.android.com/apk/res-auto”>
<TextView
android:id="@+id/news_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="@id/news_summary"/>
<TextView
android:id="@+id/news_summary"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/news_title"
android:layout_marginTop="30dp"/>
</android.support.constraint.ConstraintLayout>
MainActivity
package com.example.text_one;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.loader.ImageLoader;
import com.youth.banner.loader.ImageLoaderInterface;
import java.util.ArrayList;
import java.util.List;
import me.maxwin.view.XListView;
public class MainActivity extends AppCompatActivity {
private Banner banner;
private XListView xListView;
private int mPage;
private RecommendBaseAdapter baseAdapter;
private String urlStr="http://api.expoon.com/AppNews/getNewsList/type/1/p/%d";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
banner=findViewById(R.id.banner);
xListView=findViewById(R.id.xListView);
mPage=1;
baseAdapter=new RecommendBaseAdapter(MainActivity.this);
xListView.setAdapter(baseAdapter);
xListView.setPullLoadEnable(true);
xListView.setPullRefreshEnable(true);
xListView.setXListViewListener(new XListView.IXListViewListener() {
@Override
public void onRefresh() {
mPage=1;
loadData();
}
@Override
public void onLoadMore() {
mPage++;
loadData();
}
});
//設定banner樣式
banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);
//設定圖片載入器
banner.setImageLoader(new ImageLoaderInterface<ImageView>() {
@Override
public void displayImage(Context context, Object path, ImageView imageView) {
BannerBean.Banner banner= (BannerBean.Banner) path;
com.nostra13.universalimageloader.core.ImageLoader.getInstance()
.displayImage(banner.getPicUrl(),imageView);
}
@Override
public ImageView createImageView(Context context) {
ImageView imageView=new ImageView(context);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
return null;
}
});
initData();
loadData();
}
private void loadData() {
NetUtils.getInsanner().Requery(String.format(urlStr, mPage), UserBean.class, new NetUtils.CallBack<UserBean>() {
@Override
public void indatas(UserBean o) {
if (mPage==1){
baseAdapter.setdata(o.getData());
}else{
baseAdapter.adddata(o.getData());
}
xListView.stopRefresh();
xListView.stopLoadMore();
}
});
}
private void initData() {
NetUtils.getInsanner().Requery("https://api.tianapi.com/wxnew/?key=c4aa776e0a51d57d6750511e2baa46b6&num=6&page=1", BannerBean.class, new NetUtils.CallBack<BannerBean>() {
@Override
public void indatas(BannerBean o) {
//設定圖片集合
banner.setImages(o.getNewslist());
banner.setBannerTitles(getTitles(o));
//banner設定方法全部呼叫完畢時最後呼叫
banner.start();
}
});
}
private List<String> getTitles(BannerBean o) {
List<String> result=new ArrayList<>();
for (BannerBean.Banner banner:o.getNewslist()) {
result.add(banner.getTitle());
}
return result;
}
}
NetUtils
package com.example.text_one;
import android.annotation.SuppressLint;
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.URL;
public class NetUtils {
private static NetUtils insanner;
private Gson gson;
public NetUtils() {
gson=new Gson();
}
public static NetUtils getInsanner() {
if(insanner==null){
insanner=new NetUtils();
}
return insanner;
}
public interface CallBack<T>{
void indatas(T t);
}
@SuppressLint("StaticFieldLeak")
public void Requery(String urldata, final Class clazz, final CallBack callBack){
new AsyncTask<String,Void,Object>(){
@Override
protected Object doInBackground(String... strings) {
return Requery(strings[0],clazz);
}
@Override
protected void onPostExecute(Object o) {
callBack.indatas(o);
}
}.execute(urldata);
}
public <E> E Requery(String uridata,Class clazz){
return (E) gson.fromJson(Requery(uridata),clazz);
}
public String Requery(String uridata){
String getstram="";
try {
URL url = new URL(uridata);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setConnectTimeout(5000);
urlConnection.setReadTimeout(5000);
int responseCode = urlConnection.getResponseCode();
if (responseCode==200){
getstram = getstram(urlConnection.getInputStream());
}
} catch (Exception e) {
e.printStackTrace();
}
return getstram;
}
private String getstram(InputStream inputStream) throws IOException {
StringBuilder builder = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
for (String tmp =br.readLine();tmp!=null;tmp=br.readLine()){
builder.append(tmp);
}
return builder.toString();
}
}
介面卡
package com.example.text_one;
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.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.ArrayList;
import java.util.List;
public class RecommendBaseAdapter extends BaseAdapter {
private static final int ITEM_COUNT = 2;
private static final int TEAT_ITEM = 0;
private static final int DATA_ITEM = 1;
private Context context;
private List<UserBean.DataBean> list;
public RecommendBaseAdapter(Context context){
this.context=context;
list=new ArrayList<>();
}
public void setdata(List<UserBean.DataBean> data) {
list.clear();
if (data!=null){
list.addAll(data);
}
notifyDataSetChanged();
}
public void adddata(List<UserBean.DataBean> data){
if (data!=null){
list.addAll(data);
}
notifyDataSetChanged();
}
@Override
public int getViewTypeCount() {
return ITEM_COUNT;
}
@Override
public int getItemViewType(int position) {
return position%2==0?TEAT_ITEM:DATA_ITEM;
}
@Override
public int getCount() {
return list.size();
}
@Override
public UserBean.DataBean 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) {
ViewHolder holder;
if (convertView==null){
convertView=LayoutInflater.from(context).inflate(
getItemViewType(position)==TEAT_ITEM?R.layout.text_item:R.layout.image_item,parent,false
);
holder=new ViewHolder(convertView);
}else {
holder= (ViewHolder) convertView.getTag();
}
holder.bindData(getItem(position));
return convertView;
}
class ViewHolder{
TextView news_title;
TextView news_summary;
ImageView imageView;
public ViewHolder(View convertView) {
news_title=convertView.findViewById(R.id.news_title);
news_summary=convertView.findViewById(R.id.news_summary);
imageView=convertView.findViewById(R.id.image);
convertView.setTag(this);
}
public void bindData(UserBean.DataBean item) {
news_title.setText(item.getNews_title());
news_summary.setText(item.getNews_summary());
if (imageView!=null){
DisplayImageOptions options=new DisplayImageOptions.Builder()
.cacheOnDisk(true)
.cacheInMemory(true)
.showImageForEmptyUri(R.mipmap.ic_launcher)
.showImageOnFail(R.mipmap.ic_launcher)
.showImageOnLoading(R.mipmap.ic_launcher)
.build();
ImageLoader.getInstance().displayImage(item.getPic_url(),imageView,options);
}
}
}
}
全域性App
package com.example.shaopenglai;
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;
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
ImageLoader.getInstance().init(
new ImageLoaderConfiguration.Builder(this)
.diskCacheSize(5010241024)
.memoryCacheSizePercentage(13)
.defaultDisplayImageOptions(
new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.showImageForEmptyUri(R.mipmap.ic_launcher)
.showImageOnFail(R.mipmap.ic_launcher)
.showImageOnLoading(R.mipmap.ic_launcher)
.displayer(new RoundedBitmapDisplayer(16))
.build()
)
.build()
);
}
}
BannerBean
package com.example.text_one;
import java.util.List;
public class BannerBean {
private int code;
private String msg;
private List newslist;
private final int SUCCESS_CODE = 200;
public boolean isSuccess(){
return code==SUCCESS_CODE;
}
public int getCode() {
return code;
}
public String getMsg() {
return msg;
}
public List<Banner> getNewslist() {
return newslist;
}
public class Banner{
private String title;
private String picUrl;
public String getTitle() {
return title;
}
public String getPicUrl() {
return picUrl;
}
}
}
UserBean
package com.example.text_one;
import java.util.List;
public class UserBean {
private int status;
private String info;
private List<DataBean> data;
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public List<DataBean> getData() {
return data;
}
public void setData(List<DataBean> data) {
this.data = data;
}
public static class DataBean {
private String news_id;
private String news_title;
private String news_summary;
private String pic_url;
public String getNews_id() {
return news_id;
}
public void setNews_id(String news_id) {
this.news_id = news_id;
}
public String getNews_title() {
return news_title;
}
public void setNews_title(String news_title) {
this.news_title = news_title;
}
public String getNews_summary() {
return news_summary;
}
public void setNews_summary(String news_summary) {
this.news_summary = news_summary;
}
public String getPic_url() {
return pic_url;
}
public void setPic_url(String pic_url) {
this.pic_url = pic_url;
}
}
}