package com.example.horzatalscallview; import java.util.ArrayList; import java.util.List; import java.util.Timer; import java.util.TimerTask; import android.os.Bundle; import android.os.Handler; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.view.LayoutInflater; import android.view.View; import android.widget.LinearLayout; import com.example.adapter.MyPagerAdapter; import com.example.fragment.Fragment01; import com.example.fragment.Fragment02; import com.example.fragment.Fragment03; public class MainActivity extends FragmentActivity implements OnPageChangeListener{ private ViewPager mypager; private List<Fragment> list; private Fragment01 f1; private Fragment02 f2; private Fragment03 f3; private FragmentManager fm; private List<View>dots; private View view; private LinearLayout yuandian; private int olddotindex=0; int i=0; private Handler h=new Handler(){ public void handleMessage(android.os.Message msg) { if(msg.what==1){ mypager.setCurrentItem(i); dots.get(olddotindex).setBackgroundResource(R.drawable.dot_nomal); dots.get(i).setBackgroundResource(R.drawable.dot_focus); olddotindex=i; } }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); new Timer().schedule(new TimerTask() { @Override public void run() { // TODO Auto-generated method stub i++; if(i==3){ i=0; } h.sendEmptyMessage(1); } }, 1000, 1000); getDotsList(); dots.get(0).setBackgroundResource(R.drawable.dot_focus); } private void getDotsList() { // TODO Auto-generated method stub dots = new ArrayList<View>(); //循环图片数组,创建对应数量的dot for (int i = 0; i <3 ; i++) { view=LayoutInflater.from(getApplicationContext()).inflate(R.layout.dots, null); //得到布局中的dot点组件 View dot=view.findViewById(R.id.dotview); //收集dot; dots.add(dot); //把布局添加到线性布局 yuandian.addView(view); } } private void init() { // TODO Auto-generated method stub mypager=(ViewPager) findViewById(R.id.mypager); yuandian=(LinearLayout) findViewById(R.id.yuandian); initdata(); mypager.setOnPageChangeListener(this); fm=getSupportFragmentManager(); mypager.setAdapter(new MyPagerAdapter(fm, list)); } private void initdata() { // TODO Auto-generated method stub list=new ArrayList<Fragment>(); f1=new Fragment01(); f2=new Fragment02(); f3=new Fragment03(); list.add(f1); list.add(f2); list.add(f3); } @Override public void onPageScrollStateChanged(int arg0) { // TODO Auto-generated method stub } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { // TODO Auto-generated method stub } @Override public void onPageSelected(int position) { // TODO Auto-generated method stub setSelected(); } private void setSelected() { // TODO Auto-generated method stub } }
package com.example.adapter; import java.util.List; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.view.View; public class MyPagerAdapter extends FragmentPagerAdapter { private List<Fragment>list; public MyPagerAdapter(FragmentManager fm,List<Fragment>list) { super(fm); // TODO Auto-generated constructor stub this.list=list; } @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } @Override public Fragment getItem(int arg0) { // TODO Auto-generated method stub return list.get(arg0); } }
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <solid android:color="#f00"/> <corners android:radius="8dip"/> </shape>
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#313037"/> <corners android:radius="8dip"/> </shape>
<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" tools:context=".MainActivity" > <android.support.v4.view.ViewPager android:id="@+id/mypager" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/hello_world" /> <LinearLayout android:id="@+id/yuandian" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:layout_alignParentBottom="true" /> </RelativeLayout>
<?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" > <View android:id="@+id/dotview" android:layout_width="8dp" android:layout_height="8dp" android:layout_margin="10dp" android:background="@drawable/dot_nomal" /> </LinearLayout>