import java.util.ArrayList;
import java.util.List;
import com.bawei.bean.FenLei;
import com.bawei.fragment.*;
import com.bawei.httpclient.MyHttpClient;
import com.google.gson.Gson;
import android.app.Activity;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
public class MainActivity3 extends FragmentActivity {
private ViewPager vp3;
private ViewPager vp4;
private TextView tv1;
private TextView tv2;
private TextView tv3;
private TextView tv4;
private FenLei fl;
private int[] id={R.drawable.ggg,R.drawable.hhh,R.drawable.jjj,R.drawable.kkk,R.drawable.lll,R.drawable.mmm};
private String path1="http://japi.juhe.cn/comic/category?key=b31825c5cabbde4223d5b5f027219fe8";
private List<Fragment> list;
private List<ImageView> list2;
private LinearLayout ll;
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
if(msg.what==1){
int position=vp3.getCurrentItem();
position++;
vp3.setCurrentItem(position);
wuxianlunbo();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main3);
//获取控件
vp3 = (ViewPager) findViewById(R.id.vp3_main3);
vp4 = (ViewPager) findViewById(R.id.vp3_main4);
tv1 = (TextView) findViewById(R.id.tv1);
tv2 = (TextView) findViewById(R.id.tv2);
tv3 = (TextView) findViewById(R.id.tv3);
tv4 = (TextView) findViewById(R.id.tv4);
ll = (LinearLayout) findViewById(R.id.ll_don);
vp3Adapter();
getData();
getFragemt();
vpAdapter();
tvListener();
vpListener();
}
private void vp3Adapter() {
vp3.setAdapter(new PagerAdapter() {
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0==arg1;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return Integer.MAX_VALUE;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
container.removeView((View) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View view=View.inflate(MainActivity3.this, R.layout.vp_vp, null);
ImageView iv=(ImageView) view.findViewById(R.id.vp_iv);
iv.setImageResource(id[position%id.length]);
container.addView(view);
return view;
}
});
inDon();
vp3.setCurrentItem(20000);
wuxianlunbo();
vp3Listener();
}
private void vp3Listener() {
vp3.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
for(int i=0;i<id.length;i++){
if(arg0%list2.size()==i){
list2.get(i).setImageResource(R.drawable.don_focuse);
}else{
list2.get(i).setImageResource(R.drawable.don_normal);
}
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
public void wuxianlunbo(){
handler.sendEmptyMessageDelayed(1, 3000);
}
public void inDon(){
list2 = new ArrayList<ImageView>();
if(list2!=null){
list2.clear();
}
for(int i=0;i<id.length;i++){
ImageView iv1=new ImageView(this);
if(i==0){
iv1.setImageResource(R.drawable.don_focuse);
}else{
iv1.setImageResource(R.drawable.don_normal);
}
LayoutParams params=new LayoutParams(20,20);
params.setMargins(15, 0, 15, 0);
list2.add(iv1);
ll.addView(iv1,params);
}
}
private void vpListener() {
vp4.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
switch (arg0) {
case 0:
tv1.setTextColor(Color.RED);
tv2.setTextColor(Color.WHITE);
tv3.setTextColor(Color.WHITE);
tv4.setTextColor(Color.WHITE);
break;
case 1:
tv2.setTextColor(Color.RED);
tv1.setTextColor(Color.WHITE);
tv3.setTextColor(Color.WHITE);
tv4.setTextColor(Color.WHITE);
break;
case 2:
tv3.setTextColor(Color.RED);
tv2.setTextColor(Color.WHITE);
tv1.setTextColor(Color.WHITE);
tv4.setTextColor(Color.WHITE);
break;
case 3:
tv4.setTextColor(Color.RED);
tv2.setTextColor(Color.WHITE);
tv3.setTextColor(Color.WHITE);
tv1.setTextColor(Color.WHITE);
break;
default:
break;
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
private void tvListener() {
tv1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
tv1.setTextColor(Color.RED);
tv2.setTextColor(Color.WHITE);
tv3.setTextColor(Color.WHITE);
tv4.setTextColor(Color.WHITE);
vp4.setCurrentItem(0);
}
});
tv2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
tv2.setTextColor(Color.RED);
tv1.setTextColor(Color.WHITE);
tv3.setTextColor(Color.WHITE);
tv4.setTextColor(Color.WHITE);
vp4.setCurrentItem(1);
}
});
tv3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
tv3.setTextColor(Color.RED);
tv1.setTextColor(Color.WHITE);
tv1.setTextColor(Color.WHITE);
tv4.setTextColor(Color.WHITE);
vp4.setCurrentItem(2);
}
});
tv4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
tv4.setTextColor(Color.RED);
tv1.setTextColor(Color.WHITE);
tv3.setTextColor(Color.WHITE);
tv1.setTextColor(Color.WHITE);
vp4.setCurrentItem(3);
}
});
}
private void getFragemt() {
list = new ArrayList<Fragment>();
list.add(new Fragment1());
list.add(new Fragment2());
list.add(new Fragment3());
list.add(new Fragment4());
}
private void vpAdapter() {
vp4.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@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);
}
});
}
private void getData() {
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... arg0) {
String result=new MyHttpClient().getData(path1);
return result;
}
protected void onPostExecute(String result) {
Gson gson=new Gson();
System.out.println(result);
fl = gson.fromJson(result, FenLei.class);
tv1.setText(fl.result.get(0));
tv2.setText(fl.result.get(1));
tv3.setText(fl.result.get(2));
tv4.setText(fl.result.get(3));
};
}.execute();
}
}
画小圆点 创建drawble文件夹 创建xml文件
选中
<?xml version="1.0" encoding="UTF-8"?>
-<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="12dp"/>
<solid android:color="#f00"/>
</shape>
未选中
<?xml version="1.0" encoding="UTF-8"?>
-<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="12dp"/>
<solid android:color="#fff"/>
</shape>
以上是项目中的 部分代码
以下 是 小Demo 一例
public class MainActivity extends Activity {
private ViewPager vp;
private RelativeLayout rl;
private LinearLayout ll;
private int[] id={
R.drawable.a,R.drawable.b,R.drawable.c,
R.drawable.g,R.drawable.l,R.drawable.n,R.drawable.r,
};
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
if(msg.what==1){
int position=vp.getCurrentItem();
position++;
vp.setCurrentItem(position);
zidong();
}
}
};
private List<ImageView> list=new ArrayList<ImageView>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取控件
vp = (ViewPager) findViewById(R.id.vp);
ll = (LinearLayout) findViewById(R.id.ll);
//调用添加适配器的方法
vpAdapter();
//小圆点初始化
indon();
//设置当前的页数
vp.setCurrentItem(20000);
//调用使其自动 轮播的方法
zidong();
//调用viewpage滑动监听的方法
vpListener();
}
//创建viewpage滑动监听的方法
private void vpListener() {
vp.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
//循环判断 循环的次数是图片的页数
for(int i=0;i<id.length;i++){
//如果 当前的页数/图片的总数 ==arg0 则是选中的小圆点 设置 指定小圆点的图片 否则 为 无颜色的小圆点
if(arg0%id.length==i){
list.get(i).setImageResource(R.drawable.don_foruc);
}else{
list.get(i).setImageResource(R.drawable.don_monron);
}
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
//创建自定义适配器
private void vpAdapter() {
vp.setAdapter(new PagerAdapter() {
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0==arg1;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
//无限轮播
return Integer.MAX_VALUE;
}
@Override
public void destroyItem(ViewGroup container, int position,
Object object) {
container.removeView((View) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView iv=new ImageView(MainActivity.this);
iv.setImageResource(id[position%id.length]);
container.addView(iv);
return iv;
}
});
}
//初始化小圆点
private void indon(){
//判断集合中是否有数据 有的话清空
if(list!=null){
list.clear();
}
//循环判断 然后添加 数据
for(int i=0;i<id.length;i++){
//创建自定义的远点
ImageView av=new ImageView(this);
//判断让第一个小圆点 变色 其他的为无色
if(i==0){
av.setImageResource(R.drawable.don_foruc);
}else{
av.setImageResource(R.drawable.don_monron);
}
//创建
LayoutParams params=new LayoutParams(20,20);
//控制两点之间的距离
params.setMargins(15, 0, 15, 10);
//向 布局中 添加图像
ll.addView(av,params);
//将图像 添加到集合中
list.add(av);
}
}
//使其自动轮播的方法
public void zidong(){
handler.sendEmptyMessageDelayed(1, 3000);
}
}