本文主要是介绍ViewPager与小圆点的联动效果,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
创建圆点
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"><solid android:color="#800000"/><corners android:radius="8dp"/></shape>
//创建布局
activtity_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:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="com.example.shenxuesong.rikao0913.MainActivity"><android.support.v4.view.ViewPagerandroid:layout_width="match_parent"android:layout_height="300dp"android:id="@+id/vp"android:layout_alignParentTop="true"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/tv"android:layout_marginTop="280dp"android:layout_marginLeft="20dp"android:textColor="#22f"/><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="40dp"android:id="@+id/ll"android:orientation="horizontal"android:layout_marginRight="-10dp"android:layout_marginTop="240dp"android:gravity="center"></LinearLayout>
</RelativeLayout>
或者用线性布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><android.support.v4.view.ViewPagerandroid:layout_width="wrap_content"android:layout_height="200dp"android:id="@+id/vp"android:layout_marginTop="10dp"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/tv"android:layout_marginTop="-40dp"android:layout_marginLeft="20dp"android:textColor="#22f"/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="40dp"android:gravity="center"android:id="@+id/ll"android:orientation="horizontal"android:layout_marginTop="-35dp"></LinearLayout><!-- <me.maxwin.view.XListViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginTop="80dp"android:id="@+id/xlv"></me.maxwin.view.XListView>--></LinearLayout>
item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="match_parent"android:layout_height="match_parent"> <Viewandroid:layout_width="8dp"android:layout_height="8dp"android:layout_margin="10dp"android:id="@+id/view"android:background="@drawable/shape1"/> </LinearLayout>
//universal_Imager_Loader 请求网络图片
public class MyApplicationextendsApplication {
@Override
public void onCreate() {super.onCreate();ImageLoaderConfiguration config=new ImageLoaderConfiguration.Builder(this).build();ImageLoader.getInstance().init(config);} }
//AndroidMianfest.xml中配置权限
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.shenxuesong.rikao0913"> <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:name=".MyApplication" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/Theme.AppCompat.Light"><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>
//具体的方法操作
import android.os.AsyncTask;
import android.os.Bundle;
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.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;public class MainActivity extends AppCompatActivity {private static final String surl="https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=6&page=1";private List<JavaBean.NewslistBean> newslist;private List<View> dolist=new ArrayList<View>();private List<String> str=new ArrayList<String>();private List<ImageView> imagelist=new ArrayList<ImageView>();private ViewPager vp;private LinearLayout ll;private int current=0;private int old_list=0;private TextView tv;private Handler h=new Handler(){@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);if(msg.what==1){int i=msg.arg1;//设置viewpager的位置vp.setCurrentItem(i);// 设置图片的标题tv.setText(str.get(i%imagelist.size()));//设置圆点与图片的联动dolist.get(old_list).setBackgroundResource(R.drawable.shape1);dolist.get(i%imagelist.size()).setBackgroundResource(R.drawable.shape2);old_list=i%imagelist.size();}}};protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//查找IDvp=(ViewPager)findViewById(R.id.vp);ll=(LinearLayout)findViewById(R.id.ll);tv=(TextView)findViewById(R.id.tv);//获取图片的路径及biatigetPricUrLandTitle();}private void getPricUrLandTitle() {//异步任务new AsyncTask<String,Integer,String>(){@Overrideprotected String doInBackground(String... strings) {String str="";try {URL url = new URL(surl);HttpURLConnection http=(HttpURLConnection) url.openConnection();int code = http.getResponseCode();if(code==200){InputStream in = http.getInputStream();byte[] b=new byte[1024];int len=0;while((len=in.read(b))!=-1){str+=new String(b,0,len);}}} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return str;}@Overrideprotected void onPostExecute(String s) {super.onPostExecute(s);Gson gson = new Gson();newslist = gson.fromJson(s, JavaBean.class).getNewslist();for (int i=0;i<newslist.size();i++){JavaBean.NewslistBean newslistBean = newslist.get(i);String picUrl = newslistBean.getPicUrl();String title = newslistBean.getTitle();Log.i("TAG",picUrl);//获取网络请求的图片数据,并把它添加到imagelist中getImageView(picUrl);//得到圆点getYuanDian();//标题str.add(title);}//执行的方法init();}}.execute();}private void getImageView(String picUrl) {ImageView imageView = new ImageView(MainActivity.this);imageView.setScaleType(ImageView.ScaleType.FIT_XY);ImageLoader.getInstance().displayImage(picUrl,imageView);imagelist.add(imageView);}private void getYuanDian() {View view = LayoutInflater.from(this).inflate(R.layout.item, null);View imageview = view.findViewById(R.id.view);//不懂dolist.add(imageview);ll.addView(view);}private void init() {//设置适配器vp.setAdapter(new PagerAdapter() {@Overridepublic int getCount() {return Integer.MAX_VALUE;}@Overridepublic boolean isViewFromObject(View view, Object object) {return view==object;}@Overridepublic Object instantiateItem(ViewGroup container, int position) {View imageView = imagelist.get(position % imagelist.size());container.addView(imageView);return imageView;}@Overridepublic void destroyItem(ViewGroup container, int position, Object object) {container.removeView((View) object);}});//设置圆点的第一个被选中dolist.get(0).setBackgroundResource(R.drawable.shape2);//设置viewpager的当前位置vp.setCurrentItem(50000000);//启动定时器Timer timer = new Timer();timer.schedule(new TimerTask() {@Overridepublic void run() {current=vp.getCurrentItem()+1;Message msg = Message.obtain();msg.arg1=current;msg.what=1;h.sendMessage(msg);}}, 3000, 2000);}}
这篇关于ViewPager与小圆点的联动效果的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!