activity_main.xml
<?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:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="200dp"></android.support.v4.view.ViewPager> <LinearLayout android:id="@+id/line" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/vp" android:layout_centerHorizontal="true" android:layout_marginBottom="20dp" android:orientation="horizontal"></LinearLayout> <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/vp"></ListView> </RelativeLayout>
MainActivity
import android.os.AsyncTask; import android.os.Handler; import android.os.Message; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import com.google.gson.Gson; import com.nostra13.universalimageloader.core.ImageLoader; import java.io.InputStream; 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 ViewPager vp; private ListView listView; private List<Bean.NewslistBean> list; private List<ImageView> data = new ArrayList<>(); private String path = "https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=6&page=2"; private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (msg.what == 0) { int currentItem = vp.getCurrentItem(); vp.setCurrentItem(currentItem + 1); handler.sendEmptyMessageDelayed(0, 2000); } } }; private LinearLayout line; private List<ImageView> imageViews; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); vp = (ViewPager) findViewById(R.id.vp); listView = (ListView) findViewById(R.id.listview); line = (LinearLayout) findViewById(R.id.line); new My().execute(); } class My extends AsyncTask { @Override protected Object doInBackground(Object[] objects) { String string = ""; try { URL url = new URL(path); HttpURLConnection con = (HttpURLConnection) url.openConnection(); int code = con.getResponseCode(); if (code == 200) { InputStream is = con.getInputStream(); byte[] b = new byte[1024]; int l = 0; while ((l = is.read(b)) != -1) { String str = new String(b, 0, l); string += str; } } } catch (Exception e) { e.printStackTrace(); } return string; } @Override protected void onPostExecute(Object o) { super.onPostExecute(o); Gson gson = new Gson(); list = gson.fromJson((String) o, Bean.class).getNewslist(); for (int i = 0; i < list.size(); i++) { ImageView imageView = new ImageView(MainActivity.this); ImageLoader.getInstance().displayImage(list.get(i).getPicUrl(), imageView); data.add(imageView); } initDoc(); MyAdapter myAdapter = new MyAdapter(); listView.setAdapter(myAdapter); ImgAdapter imgAdapter = new ImgAdapter(); vp.setAdapter(imgAdapter); vp.setCurrentItem(data.size() * 10000); handler.sendEmptyMessageDelayed(0, 2000); vp.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 < imageViews.size(); i++) { if (i == position % imageViews.size()) { imageViews.get(i).setImageResource(R.drawable.doc_select); } else { imageViews.get(i).setImageResource(R.drawable.doc_select_no); } } } @Override public void onPageScrollStateChanged(int state) { } }); } } private void initDoc() { imageViews = new ArrayList<>(); line.removeAllViews(); 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); } imageViews.add(imageView); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); params.setMargins(5, 0, 5, 0); line.addView(imageView, params); } } class ImgAdapter extends PagerAdapter { @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 = data.get(position % data.size()); imageView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { boolean isTZ = true; switch (motionEvent.getAction()) { case MotionEvent.ACTION_DOWN://按下 handler.removeCallbacksAndMessages(null); break; case MotionEvent.ACTION_MOVE://移动 isTZ = false; handler.removeCallbacksAndMessages(null); break; case MotionEvent.ACTION_CANCEL://取消 isTZ = false; handler.sendEmptyMessageDelayed(0, 2000); break; case MotionEvent.ACTION_UP://抬起 handler.sendEmptyMessageDelayed(0, 2000); break; } return true; } }); container.addView(imageView); return imageView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } } class MyAdapter extends BaseAdapter { @Override public int getCount() { return list.size(); } @Override public Object getItem(int i) { return list.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { Hand hand; if (view == null) { view = View.inflate(MainActivity.this, R.layout.item, null); hand = new Hand(); hand.img = view.findViewById(R.id.img); view.setTag(hand); } else { hand = (Hand) view.getTag(); } String url = list.get(i).getPicUrl(); ImageLoader instance = ImageLoader.getInstance(); instance.displayImage(url, hand.img); return view; } } class Hand { ImageView img; } }
drawable/doc_select.xml
<?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>
drawable/doc_select_no.xml
<?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>
Myapp
import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; /** * Created by lenovo on 2017/09/13. */ public class Myapp extends Application { @Override public void onCreate() { super.onCreate(); ImageLoaderConfiguration aDefault = ImageLoaderConfiguration.createDefault(getApplicationContext()); ImageLoader.getInstance().init(aDefault); } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"> <uses-permission android:name="android.permission.INTERNET" /> <application android:name=".Myapp" 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>