compile 'com.xhb:xbanner:1.2.2' compile 'com.nineoldandroids:library:2.4.0' compile 'com.github.bumptech.glide:glide:3.7.0' compile 'jp.wasabeef:glide-transformations:1.0.6'
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
//布局
<com.stx.xhb.xbanner.XBanner android:layout_width="match_parent" android:layout_height="150dp" android:layout_below="@id/button" android:id="@+id/banner" ></com.stx.xhb.xbanner.XBanner>
//JAVA代码
public class Frament6 extends Fragment{ private String path="http://www.wanandroid.com/tools/mockapi/6523/article_channel_list"; private int page=1; private XBanner banner; private HttpUtils httpUtils; private List<String> xlist; public static Fragment getinstace(String title){ Frament6 fragment6 = new Frament6(); Bundle bundle = new Bundle(); bundle.putString("title",title); fragment6.setArguments(bundle); return fragment6; } @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.framen6, container, false); banner = view.findViewById(R.id.banner);
Bundle arguments = getArguments(); String title = arguments.getString("title"); if (arguments.get("title").equals("头条")){ httpUtils = HttpUtils.getInstance(); httpUtils.getdata(path); xlist = new ArrayList<>(); httpUtils.getHttpListener(new HttpUtils.HttpListener() { @Override public void getdata(String json) { Gson gson = new Gson(); UsersBean usersBean = gson.fromJson(json, UsersBean.class); List<UsersBean.DataBean.FocusListBean> focus_list = usersBean.getData().getFocus_list(); for (int i = 0; i < focus_list.size(); i++){ String image_url = focus_list.get(i).getImage_url(); xlist.add(image_url); } banner.setData(xlist,null); banner.setmAdapter(new XBanner.XBannerAdapter() { @Override public void loadBanner(XBanner banner, View view, int position) { Glide.with(getActivity()).load(xlist.get(position)).into((ImageView) view); } }); } }); } return view; } }