//权限
implementation ‘com.github.bumptech.glide:glide:4.9.0’
//控件
<com.stx.xhb.xbanner.XBanner
android:id="@+id/xban"
android:layout_width="match_parent"
android:layout_height="200dp"
app:isClipChildrenMode="true">
</com.stx.xhb.xbanner.XBanner>
package com.bawei.lunbo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import com.bawei.lunbo.presenter.BanPresenter;
import com.bawei.lunbo.view.BanView;
import com.bumptech.glide.Glide;
import com.stx.xhb.xbanner.XBanner;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements BanView {
private BanPresenter banPresenter;
private XBanner xBanner;
//创建新的集合把网络请求的图片的字符串传进去
private ArrayList<String> xban_list = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
xBanner = findViewById(R.id.xban);
banPresenter = new BanPresenter(this);
getData();
}
private void getData() {
banPresenter.send();
}
@Override
public void Ban(JSONArray result) {
for (int i=0;i<result.length();i++){
try {
JSONObject jsonObject = result.getJSONObject(i);
String imageUrl = jsonObject.getString("imageUrl");
Log.i("img", "Ban: "+imageUrl);
xban_list.add(imageUrl);
} catch (Exception e) {
e.printStackTrace();
}
}
xBanner.setData(xban_list,null);
xBanner.loadImage(new XBanner.XBannerAdapter() {
@Override
public void loadBanner(XBanner banner, Object model, View view, int position) {
Glide.with(MainActivity.this).load(xban_list.get(position)).into((ImageView) view);
}
});
}
}