<--------------------------------布局------------------------------------------------------------>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="200dp">
</android.support.v4.view.ViewPager>
<LinearLayout
android:id="@+id/linear_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/view_pager"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:orientation="horizontal">
</LinearLayout>
</RelativeLayout>
<-----------------------------------------------------------------画小圆点----------------------------------------------------------------->
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ff0000"/>
<corners android:radius="5dp"/>
<size android:height="10dp" android:width="10dp"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#00ff00"/>
<corners android:radius="5dp"/>
<size android:height="10dp" android:width="10dp"/>
</shape>
<-----------------------------------------------------------主页面------------------------------------------------------------------>
package com.example.a13_lunbotu;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ViewPager viewPager;
private List<String> list;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
if (msg.what == 0){
int currentItem = viewPager.getCurrentItem();
viewPager.setCurrentItem(currentItem+1);
handler.sendEmptyMessageDelayed(0,2000);
}
}
};
private LinearLayout linearLayout;
private List<ImageView> images;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.view_pager);
linearLayout = (LinearLayout) findViewById(R.id.linear_layout);
//数据.....装的是path路径
list = new ArrayList<>();
list.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505151721118&di=649c9a43aed72fbc4d99ec1a031510c6&imgtype=0&src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F015c7d574b9f8f6ac72525aee98351.jpg");
list.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505151956771&di=0eb6f306991d24b67a13ceb336f80102&imgtype=0&src=http%3A%2F%2Fi0.hdslb.com%2Fbfs%2Farchive%2F00613def3f1beb7a35ae136341be2b589bc46a2d.jpg_320x200.jpg");
list.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505151847685&di=c7a4b5d08ec43fa629bcb690039a7629&imgtype=0&src=http%3A%2F%2Fattimg.dospy.com%2Fimg%2Fday_080625%2F20080625_2e91a10c444877e88827vri2ZKdGMvQo.jpg");
list.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505151825129&di=70bf74b87d8a15cb91a2d79f15ed0eaf&imgtype=0&src=http%3A%2F%2Fattimg.dospy.com%2Fimg%2Fday_081016%2F20081016_fee215664d5740e56c13E2YB8giERFEX.jpg");
list.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505746504&di=930c4d677a02328a142d6fa85ed14580&imgtype=jpg&er=1&src=http%3A%2F%2Fattimg.dospy.com%2Fimg%2Fday_090113%2F20090113_6ac58b42bea94f0b318e1B6BZb5lPZl5.jpg");
//初始化小圆点
initDoc();
//设置适配器
ImagePager imagePager = new ImagePager(MainActivity.this, list,handler);
//
viewPager.setAdapter(imagePager);
//设置ViewPager初始展示的位置
viewPager.setCurrentItem(list.size()*10000);
//发送延时消息
handler.sendEmptyMessageDelayed(0,2000);
//viewPager页面改变的监听事件
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
//如果选中了这一页,,,当前小圆点红色,,,否则绿色
for (int i = 0;i<images.size();i++){
if (i== position%images.size()){
images.get(i).setImageResource(R.drawable.doc_select);
}else {
images.get(i).setImageResource(R.drawable.doc_select_no);
}
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
/**
* 初始化小圆点
*/
private void initDoc() {
//首先需要一个集合记录这些小圆点的图片,,,,当页面切换的时候,可以从集合中取出imageView进行显示图片的设置
images = new ArrayList<>();
linearLayout.removeAllViews();//清空/移除所有的view
for (int i = 0;i<list.size();i++){
ImageView imageView = new ImageView(MainActivity.this);
if (i==0){//显示第一页,,,红的
imageView.setImageResource(R.drawable.doc_select);
}else {//绿的
imageView.setImageResource(R.drawable.doc_select_no);
}
//添加到集合
images.add(imageView);
//加入到线性布局中显示
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(5,0,5,0);
linearLayout.addView(imageView,params);
}
}
}
<----------------------------------------------------------------------------------适配器---------------------------------------------------------------------------------->
package com.example.a13_lunbotu;
import android.content.Context;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.List;
/**
* @author Dash
* @date 2017/9/12
* @description:
*/
public class ImagePager extends PagerAdapter {
Context context;
List<String> list;
Handler handler;
public ImagePager(Context context, List<String> list, Handler handler) {
this.context = context;
this.list = list;
this.handler = handler;
//初始化
//ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(context));
}
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
//返回当前显示的视图
ImageView imageView = new ImageView(context);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
//加载网络的图片
ImageLoader.getInstance().displayImage(list.get(position%list.size()),imageView);
/*imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i("----","点击事件执行了");
}
});*/
//imageView触摸的监听事件
imageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
boolean isTiaoZhuan = true;
switch (motionEvent.getAction()){//获取动作
case MotionEvent.ACTION_DOWN://按下
Log.i("--","ACTION_DOWN");
handler.removeCallbacksAndMessages(null);
break;
case MotionEvent.ACTION_MOVE://移动
Log.i("--","ACTION_MOVE");
isTiaoZhuan = false;
handler.removeCallbacksAndMessages(null);
break;
case MotionEvent.ACTION_CANCEL://取消
Log.i("--","ACTION_CANCEL");
isTiaoZhuan = false;
handler.sendEmptyMessageDelayed(0,2000);
break;
case MotionEvent.ACTION_UP://抬起
Log.i("--","ACTION_UP");
if (isTiaoZhuan){
Toast.makeText(context,"跳转页面",Toast.LENGTH_SHORT).show();
/*Intent intent = new Intent();
context.startActivity();*/
}
handler.sendEmptyMessageDelayed(0,2000);
break;
}
//自己处理触摸事件....如果当前位置返回了true,,,点击事件将不会执行,,,表示动作自己处理,不会传递
return true;
}
});
//添加到容器
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
<--------------------------------------------------------------------------imageLoader设置-------------------------------------------------------------------------------->
public class BaseApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
//初始化
ImageloaderUtil.initConfig(this);
}
}
package com.example.a13_lunbotu;
import android.content.Context;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.decode.BaseImageDecoder;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.nostra13.universalimageloader.utils.StorageUtils;
import java.io.File;
/**
* @author Dash
* @date 2017/9/12
* @description:
*/
public class ImageloaderUtil {
public static void initConfig(Context context) {
//配置
File cacheDir = StorageUtils.getCacheDirectory(context); //缓存文件夹路径
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
.threadPoolSize(3) // default 线程池内加载的数量
.threadPriority(Thread.NORM_PRIORITY - 2) // default 设置当前线程的优先级
.tasksProcessingOrder(QueueProcessingType.FIFO) // default
.denyCacheImageMultipleSizesInMemory()
.memoryCache(new LruMemoryCache(2 * 1024 * 1024)) //可以通过自己的内存缓存实现
.memoryCacheSize(2 * 1024 * 1024) // 内存缓存的最大值
.memoryCacheSizePercentage(13) // default
.diskCache(new UnlimitedDiskCache(cacheDir)) // default 可以自定义缓存路径
.diskCacheSize(50 * 1024 * 1024) // 50 Mb sd卡(本地)缓存的最大值
.diskCacheFileCount(100) // 可以缓存的文件数量
// default为使用HASHCODE对UIL进行加密命名, 还可以用MD5(new Md5FileNameGenerator())加密
.diskCacheFileNameGenerator(new HashCodeFileNameGenerator())
.imageDownloader(new BaseImageDownloader(context)) // default
.imageDecoder(new BaseImageDecoder(true)) // default
.defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
.writeDebugLogs() // 打印debug log
.build(); //开始构建
ImageLoader.getInstance().init(config);
}
}
<----------------------------------------------------------清单列表配置------------------------------------------------------->
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.a13_lunbotu">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:name=".BaseApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
package com.example.adminjs.zuoye;
import android.os.AsyncTask;
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.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.example.adminjs.zuoye.Bean.DataDataBean;
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<DataDataBean.DataBean.EssayBean> list = new ArrayList<>();
private ViewPager pager;
private List<DataDataBean.DataBean.EssayBean> essay;
private LinearLayout linearLayout;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
if (msg.what == 0){
int currentItem = pager.getCurrentItem();
pager.setCurrentItem(currentItem+1);
handler.sendEmptyMessageDelayed(0,2000);
}
}
};
private List<ImageView> images;
private DataDataBean.DataBean data;
private HoAdapter hoAdapter;
private GridView gridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pager = (ViewPager) findViewById(R.id.pager);
linearLayout = (LinearLayout) findViewById(R.id.line);
gridView = (GridView)findViewById(R.id.gridview);
getData();
}
public void getData() {
AsyncTask<Void, Void, String> asyncTask = new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... voids) {
String path = "http://v3.wufazhuce.com:8000/api/reading/index/?version=3.5.0&platform=android";
try {
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
int responseCode = connection.getResponseCode();
if (responseCode == 200) {
InputStream inputStream = connection.getInputStream();
String json = StreamToString(inputStream, "utf-8");
return json;
}
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
@Override
protected void onPostExecute(String json) {
Gson gson = new Gson();
DataDataBean dataDataBean = gson.fromJson(json, DataDataBean.class);
essay = dataDataBean.getData().getEssay();
initDa();
pager.setCurrentItem(essay.size() * 10000);
MyAdapter myAdapter = new MyAdapter(MainActivity.this, handler, essay);
pager.setAdapter(myAdapter);
JianTing();
handler.sendEmptyMessageDelayed(0,2000);
list.addAll(dataDataBean.getData().getEssay());
setAdapter();
}
};
asyncTask.execute();
}
private void setAdapter() {
if (hoAdapter == null){
hoAdapter = new HoAdapter(MainActivity.this, list);
gridView.setAdapter(hoAdapter);
}else{
hoAdapter.notifyDataSetChanged();
}
}
public String StreamToString(InputStream inputstream, String charset){
try {
InputStreamReader inputStreamReader = new InputStreamReader(inputstream, charset);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder stringBuilder = new StringBuilder();
String string;
while((string=bufferedReader.readLine())!=null){
stringBuilder.append(string);
}
bufferedReader.close();
return stringBuilder.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void initDa() {
images = new ArrayList<>();
linearLayout.removeAllViews();
for (int i=0;i<essay.size();i++) {
ImageView imageView = new ImageView(MainActivity.this);
if (i == 0) {
imageView.setImageResource(R.drawable.doc_select);
} else {
imageView.setImageResource(R.drawable.doc_select_no);
}
images.add(imageView);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(5, 0, 5, 0);
linearLayout.addView(imageView, params);
}
}
//监听方法
public void JianTing(){
pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
for (int i = 0;i<essay.size();i++) {
if (i == position % essay.size()) {
images.get(i).setImageResource(R.drawable.doc_select);
} else {
images.get(i).setImageResource(R.drawable.doc_select_no);
}
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
}