依赖:
implementation 'com.google.guava:guava:16.0.1'
implementation 'com.google.code.gson:gson:2.2.4'
包:
implementation project(':imageloaderlibrary')
implementation project(':pullToRefresh')
刷新接口:
http://www.xieast.com/api/news/news.php?page=1
NetUtils:
package com.example.lasttimt.net;
import android.os.AsyncTask;
import com.google.common.io.CharStreams;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class NetUtils {
private static final NetUtils ourInstance = new NetUtils();
public static NetUtils getInstance() {
return ourInstance;
}
private NetUtils() {
}
public void GetData(String url){
new HttpUtils().execute(url);
}
private Callback callback;
public void setCallback(Callback callback) {
this.callback = callback;
}
public interface Callback{
void Call(String result);
}
public class HttpUtils extends AsyncTask<String,Void,String>{
@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) {
super.onPostExecute(s);
if (callback != null){
callback.Call(s);
}
}
}
}
App:
package com.example.lasttimt.app;
import android.app.Application;
import com.bwei.imageloaderlibrary.utils.ImageLoaderUtils;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
ImageLoaderConfiguration configuration = ImageLoaderUtils.getConfiguration(this);
ImageLoader.getInstance().init(configuration);
}
}
轮播接口:
http://www.xieast.com/api/banner.php
,首页布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="欢迎光临"
android:textColor="#F00"
android:textSize="180dp"
android:gravity="center"/>
</LinearLayout>
初始化控件,以及其他操作:
package com.example.lasttimt;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView text_view;
private AlphaAnimation alphaAnimation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
text_view = findViewById(R.id.text_view);
alphaAnimation = new AlphaAnimation(0, 1);
alphaAnimation.setDuration(3000);
text_view.setAnimation(alphaAnimation);
alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
startActivity(new Intent(MainActivity.this,ShowActivity.class));
finish();
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
}
}
侧滑布局以及刷新:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/pull_to_refresh_image_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.handmark.pulltorefresh.library.PullToRefreshListView>
</LinearLayout>
<LinearLayout
android:layout_gravity="start"
android:layout_width="280dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ccc">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/a"/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
控件以及点击事件:
package com.example.lasttimt;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import com.example.lasttimt.adapter.Myadapter;
import com.example.lasttimt.bean.Bean;
import com.example.lasttimt.net.NetUtils;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import java.util.List;
public class ShowActivity extends AppCompatActivity {
private PullToRefreshListView pull_to_refresh_image_list_view;
private NetUtils instance;
private String url="http://www.xieast.com/api/news/news.php?page=";
private int page = 1;
private List<Bean.DataBean> data;
private Bean bean;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
pull_to_refresh_image_list_view = findViewById(R.id.pull_to_refresh_image_list_view);
instance = NetUtils.getInstance();
instance.GetData(url);
instance.setCallback(new NetUtils.Callback() {
@Override
public void Call(String result) {
Gson gson = new Gson();
bean = gson.fromJson(result, Bean.class);
data = bean.getData();
Myadapter myadapter = new Myadapter(data, ShowActivity.this);
pull_to_refresh_image_list_view.setAdapter(myadapter);
}
});
pull_to_refresh_image_list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(ShowActivity.this,WebAcyivity.class);
intent.putExtra("url",data.get(i).getUrl());
startActivity(intent);
}
});
pull_to_refresh_image_list_view.setMode(PullToRefreshListView.Mode.BOTH);
pull_to_refresh_image_list_view.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
page = 1;
instance.GetData(url + page);
pull_to_refresh_image_list_view.postDelayed(new Runnable() {
@Override
public void run() {
pull_to_refresh_image_list_view.onRefreshComplete();
}
},1000);
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
page ++;
instance.GetData(url + page);
pull_to_refresh_image_list_view.postDelayed(new Runnable() {
@Override
public void run() {
pull_to_refresh_image_list_view.onRefreshComplete();
}
},1000);
}
});
}
}
上面轮播和刷新各需要适配器一个各需要一个类:
轮播类:
刷新类:
轮播适配器:
package com.example.lasttimt.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.example.lasttimt.bean.Beanlist;
import java.util.List;
public class LunAdapter extends PagerAdapter {
private List<ImageView> list ;
private Context context;
public LunAdapter(List<ImageView> list, Context context) {
this.list = list;
this.context = context;
}
@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 = list.get(position % list.size());
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View)object);
}
}
刷新适配器:
package com.example.lasttimt.adapter;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.lasttimt.R;
import com.example.lasttimt.bean.Bean;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.List;
public class Myadapter extends BaseAdapter {
private List<Bean.DataBean> data;
private Context context;
public Myadapter(List<Bean.DataBean> data, Context context) {
this.data = data;
this.context = context;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int i) {
return data.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View convertview, ViewGroup viewGroup) {
ViewHolderB holderB = null;
if (convertview == null){
convertview =View.inflate(context, R.layout.layout_b,null);
holderB = new ViewHolderB();
holderB.text1 = convertview.findViewById(R.id.text_datab);
holderB.text2 = convertview.findViewById(R.id.text_nameb);
holderB.text3 = convertview.findViewById(R.id.text_titleb);
holderB.ima1 = convertview.findViewById(R.id.ima1);
holderB.ima2 = convertview.findViewById(R.id.ima2);
holderB.ima3 = convertview.findViewById(R.id.ima3);
convertview.setTag(holderB);
}else{
holderB = (ViewHolderB) convertview.getTag();
}
holderB.text1.setText(data.get(i).getDate());
holderB.text2.setText(data.get(i).getAuthor_name());
holderB.text3.setText(data.get(i).getTitle());
ImageLoader.getInstance().displayImage(data.get(i).getThumbnail_pic_s(),holderB.ima1);
ImageLoader.getInstance().displayImage(data.get(i).getThumbnail_pic_s02(),holderB.ima2);
ImageLoader.getInstance().displayImage(data.get(i).getThumbnail_pic_s03(),holderB.ima3);
return convertview;
}
class ViewHolderB{
TextView text1,text2,text3;
ImageView ima1,ima2,ima3;
}
}
刷新适配器里面的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/text_datab"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="XXXX"
android:gravity="center"
/>
<TextView
android:id="@+id/text_nameb"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="XXXX"
android:gravity="center"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/ima1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@drawable/ic_launcher_background"/>
<ImageView
android:id="@+id/ima2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@drawable/ic_launcher_background"/>
<ImageView
android:id="@+id/ima3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@drawable/ic_launcher_background"/>
</LinearLayout>
<TextView
android:id="@+id/text_titleb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="XXXX"
android:gravity="center"
/>
</LinearLayout>
WenView布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android" >
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="200dp"/>
<WebView
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
控件:
package com.example.lasttimt;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import com.example.lasttimt.adapter.LunAdapter;
import com.example.lasttimt.bean.Beanlist;
import com.example.lasttimt.net.NetUtils;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.ArrayList;
import java.util.List;
public class WebAcyivity extends AppCompatActivity {
private WebView web_view;
private Intent intent;
private ViewPager vp;
private NetUtils instance;
private String url = "http://www.xieast.com/api/banner.php";
private List<Beanlist.DataBean> data;
private Beanlist beanlist;
private List<ImageView> list = new ArrayList<>();
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 0){
int currentItem = vp.getCurrentItem();
currentItem++;
vp.setCurrentItem(currentItem);
handler.sendEmptyMessageDelayed(0,2000);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_acyivity);
web_view = findViewById(R.id.web_view);
vp = findViewById(R.id.vp);
intent = getIntent();
web_view.loadUrl(intent.getStringExtra("url"));
instance = NetUtils.getInstance();
instance.GetData(url);
instance.setCallback(new NetUtils.Callback() {
private LunAdapter lunAdapter;
@Override
public void Call(String result) {
Gson gson = new Gson();
beanlist = gson.fromJson(result, Beanlist.class);
data = beanlist.getData();
for (int i = 0 ; i < beanlist.getData().size() ; i ++ ){
ImageView imageView = new ImageView(WebAcyivity.this);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
ImageLoader.getInstance().displayImage(data.get(i).getImg(),imageView);
list.add(imageView);
}
lunAdapter = new LunAdapter(list, WebAcyivity.this);
vp.setAdapter(lunAdapter);
handler.sendEmptyMessageDelayed(0,2000);
}
});
}
}