基类
public abstract class BaseActivity extends AppCompatActivity {
protected HttpUtils httpUtils;
private boolean isFullScreen;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}
/**
* 初始化
*/
private void init() {
isFullScreen = isFullScreen();
if (isFullScreen) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
setContentView(initLayout());
findView();
httpUtils = HttpUtils.getInstance();
setListener();
initFinish();
}
protected abstract boolean isFullScreen();
/**
* 初始化完成
*/
protected abstract void initFinish();
/**
* 设置事件
*/
protected abstract void setListener();
/**
* 查找控件
*/
protected abstract void findView();
/**
* 填充布局
*
* @return
*/
protected abstract int initLayout();
}
上面是基类
/展示页面//
public class MainActivity extends BaseActivity implements HttpCallback, AdapterView.OnItemClickListener {
private PullToRefreshListView pullToRefreshListView;
private String api = "http://172.17.8.100/mobile/exam/findNewList";
private boolean isPull = false;
private MainAdapter adapter;
// 定义数据集
private List<MainBean.ResultBean.DataBean> dataBeanList = new ArrayList<>();
@Override
protected boolean isFullScreen() {
return false;
}
@Override
protected void initFinish() {
// 设置支持上拉 下拉
pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
// 设置监听
pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
isPull = true;
loadData();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
loadData();
}
});
adapter = new MainAdapter(this, dataBeanList);
pullToRefreshListView.setAdapter(adapter);
loadData();
}
/**
* 加载数据
*/
private void loadData() {
httpUtils.getDataFromServer(api, this);
}
@Override
protected void setListener() {
pullToRefreshListView.setOnItemClickListener(this);
}
@Override
protected void findView() {
pullToRefreshListView = findViewById(R.id.pull_to_refresh_list);
}
@Override
protected int initLayout() {
return R.layout.activity_main;
}
@Override
public void onSuccess(String result) {
// 解析数据
Gson gson = new Gson();
MainBean mainBean = gson.fromJson(result, MainBean.class);
// 下拉
if (isPull) {
isPull = !isPull;
dataBeanList.clear();
}
// 添加数据
dataBeanList.addAll(mainBean.getResult().getData());
// 更新数据
adapter.notifyDataSetChanged();
// 停止刷新
pullToRefreshListView.onRefreshComplete();
}
@Override
public void onError(String errorMsg) {
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(this, DetailActivity.class);
MainBean.ResultBean.DataBean item = adapter.getItem(position);
intent.putExtra("url", item.getUrl());
startActivity(intent);
}
}
/上面是展示页面//
========webview页面==============
public class DetailActivity extends BaseActivity implements HttpCallback {
private static final int CODE_BANNER = 1;
private static final long DELAY = 2000;
private String bannerApi = "https://api.yunxuekeji.cn/yunxue_app_api/content/getIndexBanner/1";
private String url;
private ViewPager banner;
private WebView webView;
private List<ImageView> bannerImages = new ArrayList<>();
@Override
protected boolean isFullScreen() {
return false;
}
@Override
protected void initFinish() {
webView.setWebViewClient(new WebViewClient());
webView.setWebChromeClient(new WebChromeClient());
webView.loadUrl(url);
// 设置banner
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() {
mHandler.removeCallbacksAndMessages(null);
}
/**
* 加载轮播数据
*/
private void loadBannerData() {
httpUtils.getDataFromServer(bannerApi, this);
}
@Override
protected void setListener() {
}
@Override
protected void findView() {
url = getIntent().getStringExtra("url");
webView = findViewById(R.id.web_view);
banner = findViewById(R.id.banner);
}
@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);
}
// 设置Adapter
banner.setAdapter(new BannerAdapter(bannerImages));
// 轮播
startBanner();
}
private void startBanner() {
mHandler.sendEmptyMessageDelayed(CODE_BANNER, DELAY);
}
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case CODE_BANNER:
int currentItem = banner.getCurrentItem();
currentItem++;
banner.setCurrentItem(currentItem);
sendEmptyMessageDelayed(CODE_BANNER, DELAY);
break;
}
}
};
@Override
public void onError(String errorMsg) {
}
}
========webview页面==============
==================欢迎页面============================
public class WelcomeActivity extends BaseActivity {
private static final int CODE_TO_MAIN = 1;
private static final long DEAYL = 2000;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case CODE_TO_MAIN:
toMain();
break;
}
}
};
private void toMain() {
startActivity(new Intent(this, MainActivity.class));
finish();
}
@Override
protected void onDestroy() {
super.onDestroy();
mHandler.removeCallbacksAndMessages(null);
}
@Override
protected boolean isFullScreen() {
return true;
}
@Override
protected void initFinish() {
mHandler.sendEmptyMessageDelayed(CODE_TO_MAIN, DEAYL);
}
@Override
protected void setListener() {
}
@Override
protected void findView() {
}
@Override
protected int initLayout() {
return R.layout.activity_welcome;
}
}
==================欢迎页面============================
///图片了适配器///
public class BannerAdapter extends PagerAdapter {
private List<ImageView> imageViewList;
public BannerAdapter(List<ImageView> 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 parent = (ViewGroup) imageView.getParent();
if (parent != null) {
parent.removeView(imageView);
}
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
}
///图片的适配器///
///数据的适配器/
public class MainAdapter extends BaseAdapter {
private Context context;
private List<MainBean.ResultBean.DataBean> dataBeanList;
public MainAdapter(Context context, List<MainBean.ResultBean.DataBean> 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;
}
}
//上面是数据的适配器/
//application// //
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
initImageLoader();
}
private void initImageLoader() {
ImageLoaderConfiguration configuration = new ImageLoaderConfiguration
.Builder(getApplicationContext())
.build();
ImageLoader.getInstance().init(configuration);
}
}
//application/ /
//Httputils/
public class HttpUtils {
private static final HttpUtils ourInstance = new HttpUtils();
public static HttpUtils getInstance() {
return ourInstance;
}
private HttpUtils() {
}
/**
* 封装Http get请求获取数据
*/
public void getDataFromServer(String url, HttpCallback callback) {
new LoadDataTask(callback).execute(url);
}
class LoadDataTask extends AsyncTask<String, Void, String> {
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("没有请求到数据");
}
}
}
}
}
//Httputils/
//httpCallBack
public interface HttpCallback {
void onSuccess(String result);
void onError(String errorMsg);
}