1.先写图片的全局配置,例如:
public class MyAppliction extends Application {
@Override
public void onCreate() {
super.onCreate();
initD();
}
private void initD() {
ImageLoaderConfiguration configuration = new ImageLoaderConfiguration
.Builder(getApplicationContext())
.build();
ImageLoader.getInstance().init(configuration);
}
}
2.先写一个外部的数据返回接口,例如:
public interface HttpCallback {
void onSuccess(String result);
void onError(String errorMsg);
}
3.网络工具类,例如:
public class HttpUtils {
private static final HttpUtils ourInstance = new HttpUtils();
public static HttpUtils getInstance() {
return ourInstance;
}
private HttpUtils() {
}
/* 封装get Http 请求数据*/
public void getDataFromService(String url,HttpCallback callback){
new LoadDataTask(callback).execute(url);
}
class LoadDataTask extends AsyncTask{
//有参函数
private HttpCallback callback;
public LoadDataTask(HttpCallback callback) {
this.callback = callback;
}
@Override
protected String doInBackground(String... strings) {
try {
URL url = new URL(strings[0]);
HttpURLConnection connection= (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
if (connection.getResponseCode()==HttpURLConnection.HTTP_OK){
return CharStreams.toString(new InputStreamReader(connection.getInputStream(),"UTF-8"));
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
if (callback!=null){
if (!TextUtils.isEmpty(s)){
callback.onSuccess(s);
}else{
callback.onError("没有数据");
}
}
}
}
}
4.多条j目的adapter不需要继承基类所以写在上面,例如:
public class MainAdapter extends BaseAdapter {
private Context context;
private List dataBeanList;
public MainAdapter(Context context, List dataBeanList) {
this.context = context;
this.dataBeanList = dataBeanList;
}
@Override
public int getCount() {
return dataBeanList.size();
}
@Override
public MainBean.ResultBean.DataBean getItem(int position) {
return dataBeanList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
if (!TextUtils.isEmpty(dataBeanList.get(position).getThumbnail_pic_s03())) {
return VIEW_TYPE_TWO;
} else {
return VIEW_TYPE_ONE;
}
}
@Override
public int getViewTypeCount() {
return super.getViewTypeCount() + 3;
}
private final int VIEW_TYPE_ONE = 1;
private final int VIEW_TYPE_TWO = 2;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int viewType = getItemViewType(position);
switch (viewType) {
case VIEW_TYPE_ONE:
ViewHodler hodler = null;
if (convertView == null) {
hodler = new ViewHodler();
convertView = LayoutInflater.from(context).inflate(R.layout.main_list_item_one, null, false);
hodler.tvTitle = convertView.findViewById(R.id.tv_title);
hodler.ivImage = convertView.findViewById(R.id.iv_image);
convertView.setTag(hodler);
} else {
hodler = (ViewHodler) convertView.getTag();
}
hodler.tvTitle.setText(dataBeanList.get(position).getTitle());
ImageLoader.getInstance().displayImage(dataBeanList.get(position).getThumbnail_pic_s(), hodler.ivImage);
break;
case VIEW_TYPE_TWO:
ViewHolderTwo holderTwo = null;
if (convertView == null) {
holderTwo = new ViewHolderTwo();
convertView = LayoutInflater.from(context).inflate(R.layout.main_list_item_two, null, false);
holderTwo.tvTitle = convertView.findViewById(R.id.tv_title);
holderTwo.ivImage = convertView.findViewById(R.id.iv_image);
holderTwo.ivImage2 = convertView.findViewById(R.id.iv_image2);
holderTwo.ivImage3 = convertView.findViewById(R.id.iv_image3);
convertView.setTag(holderTwo);
} else {
holderTwo = (ViewHolderTwo) convertView.getTag();
}
holderTwo.tvTitle.setText(dataBeanList.get(position).getTitle());
ImageLoader.getInstance().displayImage(dataBeanList.get(position).getThumbnail_pic_s(), holderTwo.ivImage);
ImageLoader.getInstance().displayImage(dataBeanList.get(position).getThumbnail_pic_s02(), holderTwo.ivImage2);
ImageLoader.getInstance().displayImage(dataBeanList.get(position).getThumbnail_pic_s03(), holderTwo.ivImage3);
break;
}
return convertView;
}
class ViewHodler {
TextView tvTitle;
ImageView ivImage;
}
class ViewHolderTwo {
TextView tvTitle;
ImageView ivImage;
ImageView ivImage2;
ImageView ivImage3;
}
}
1.抽取基类,例如:
public abstract class BaseActivity extends AppCompatActivity {
protected HttpUtils httpUtils;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}
/*初始化*/
private void init() {
setContentView(initLayout());
findView();
httpUtils = HttpUtils.getInstance();
setListener();
initFinish();
}
/*初始化完成*/
protected abstract void initFinish();
/*设置事件*/
protected abstract void setListener();
/*查找控件*/
protected abstract void findView();
/*填充布局*/
protected abstract int initLayout();
}
2.抽取过基类后其他的Activity都需要继承基类BaseActivity ,例如:
public class MainActivity extends BaseActivity implements HttpCallback{
private String api = "http://172.17.8.100/mobile/exam/findNewList";
//上拉
private boolean isPall = false;
private PullToRefreshListView pull;
private ArrayList dataBeanslist = new ArrayList<>();//数据的集合
private MainAdapter mainAdapter;
/*初始化完成*/
@Override
protected void initFinish() {
//设置PullToRefreshListView的上拉下拉
pull.setMode(PullToRefreshBase.Mode.BOTH);
//监听事件
pull.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {
@Override
public void onPullDownToRefresh(PullToRefreshBase pullToRefreshBase) {
//下拉
isPall=true;
lodData();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase pullToRefreshBase) {
lodData();
}
});
/*设置适配器*/
mainAdapter = new MainAdapter(MainActivity.this, dataBeanslist);
pull.setAdapter(mainAdapter);
/*显示数据的方法*/
lodData();
}
/*显示数据的方法*/
private void lodData() {
/*显示数据*/
httpUtils.getDataFromService(api,this);
}
/*设置事件*/
@Override
protected void setListener() {
}
/*查找控件*/
@Override
protected void findView() {
pull = findViewById(R.id.pull);
}
/*填充布局*/
@Override
protected int initLayout() {
return R.layout.activity_main;
}
/*这两个方法是HttpCallback生成*/
@Override
public void onSuccess(String result) {
//解析数据
Gson gson = new Gson();
MainBean mainBean = gson.fromJson(result, MainBean.class);
if (isPall){//如果是上拉
isPall=!isPall;//改成下拉
dataBeanslist.clear();//只显示第一页数据
}
//添加数据
dataBeanslist.addAll(mainBean.getResult().getData());
//更新数据
mainAdapter.notifyDataSetChanged();
//停止刷新
pull.onRefreshComplete();
}
@Override
public void onError(String errorMsg) {
}
}
即可实现抽取基类并且上拉加载下拉刷新的多条目展示
接口,例如:http://172.17.8.100/mobile/exam/findNewList
1.在MainActivity中的设置事件里写,例如:
/*设置事件*/
@Override
protected void setListener() {
pull.setOnItemClickListener(this);
}
/*点击条目跳转*/
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
//意图对象
Intent intent = new Intent(this, DetailActivity.class);
//得到条目
MainBean.ResultBean.DataBean item = mainAdapter.getItem(position);
//传值
intent.putExtra("url",item.getUrl());
startActivity(intent);
}
2.跳转到DetailActivity页面后先展示Webview再加载图片,例如:
public class DetailActivity extends BaseActivity implements HttpCallback {
private String apilb = "https://api.yunxuekeji.cn/yunxue_app_api/content/getIndexBanner/1";
private String url;
private ViewPager banner;
private WebView web_view;
private List bannerImages = new ArrayList<>();
/*初始化完成*/
@Override
protected void initFinish() {
webView.setWebViewClient(new WebViewClient());
webView.setWebChromeClient(new WebChromeClient());
//WebView显示
web_view.loadUrl(url);
//设置轮播
loadBannerData();
//轮播点击停止--可写可不写
banner.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN://按下
stopBanner();
break;
case MotionEvent.ACTION_UP://抬起
startBanner();
break;
}
return false;
}
});
}
//停止轮播
private void stopBanner() {
handler.removeCallbacksAndMessages(null);
}
//加载轮播数据---开始轮播
private void loadBannerData() {
httpUtils.getDataFromService(apilb,this);
}
/*设置事件*/
@Override
protected void setListener() {
}
/*查找控件*/
@Override
protected void findView() {
//接收跳转时带过来的值
url = getIntent().getStringExtra("url");
//轮播
banner = findViewById(R.id.banner);
//WebView
web_view = findViewById(R.id.web_view);
}
/*填充布局*/
@Override
protected int initLayout() {
return R.layout.activity_detail;
}
@Override
public void onSuccess(String result) {
//解析数据
Gson gson = new Gson();
BannerBean bannerBean = gson.fromJson(result, BannerBean.class);
//遍历图片
for (BannerBean.BodyBean.ResultBean resultBean : bannerBean.getBody().getResult()) {
ImageView imageView = new ImageView(this);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
ImageLoader.getInstance().displayImage(resultBean.getBarnerPic(),imageView);
bannerImages.add(imageView);
}
//轮播适配器
banner.setAdapter(new BannerAdapter(bannerImages));
//开始轮播
startBanner();
}
//开始轮播
private void startBanner() {
handler.sendEmptyMessageDelayed(1,2000);
}
//Handler发送消息开始轮播
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case 1:
int currentItem = banner.getCurrentItem();
currentItem++;
banner.setCurrentItem(currentItem);
sendEmptyMessageDelayed(1,2000);
break;
}
}
};
@Override
public void onError(String errorMsg) {
}
}
展示轮播的时候需要加载网络数据设置adapter,例如:
public class BannerAdapter extends PagerAdapter {
private List imageViewList;
public BannerAdapter(List imageViewList) {
this.imageViewList = imageViewList;
}
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view==object;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
ImageView imageView = imageViewList.get(position % imageViewList.size());
ViewGroup par = (ViewGroup) imageView.getParent();
if (par!=null){
par.removeView(imageView);
}else{
container.addView(imageView);
}
return imageView;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
}