实现广告图片循环轮播效果(包括实现手动滑动循环)

package com.hdd.main;



public class FirstActivity extends FragmentActivity implements OnClickListener {
ArrayList list = new ArrayList();
private ListView listview;
private List imgs = new ArrayList();
ViewPager viewpager;

/** 记录轮播到第几张图片了 **/
int position;
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 100:
viewpager.setCurrentItem(position);
position++;
break;


}
};
};


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
init();
for (int i = 20; i < 27; i++) {
getBanner(getResources().getString(R.string.url)
+ getResources().getString(R.string.getFirstPicture), i);
}


}


private void init() {
viewpager.setOnPageChangeListener(new OnPageChangeListener() {


@Override
public void onPageSelected(int arg0) {
position = arg0;
}


@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {


}


@Override
public void onPageScrollStateChanged(int arg0) {


}
});
Timer timer = new Timer();
MyTimerTask task = new MyTimerTask();
timer.schedule(task, 1000, 1000);
}


/** 轮播图片计时器 **/
private class MyTimerTask extends TimerTask {


@Override
public void run() {
if (position == list.size()) {
position = 0;
}
Message msg = new Message();
msg.what = 100;
handler.sendMessage(msg);
}


}


// 获取轮播图片
private void getBanner(String url, final int adSlotId) {
AsyncHttpClient client = MyApplication.getApplicationIntance()
.getHttpClient();
RequestParams params = new RequestParams();
params.put("qm.adSlotId", adSlotId + "");
params.put("page", "1");
params.put("limit", "1");
params.put("start", "0");
client.post(url, params, new AsyncHttpResponseHandler() {
public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
String result = new String(arg2);
Log.i("GUANGGAO", result);
firstDetail fDetail = new Gson().fromJson(result,
firstDetail.class);

switch (adSlotId) {


case 20:
if (fDetail.isSuccess()
&& fDetail.getItems().get(0).getImage() != null) {
list.add(new FirstPictureFragment(FirstActivity.this,
fDetail.getItems().get(0).getImage() + "-raw",
null));
}
break;
case 21:
if (fDetail.isSuccess()
&& fDetail.getItems().get(0).getImage() != null) {
list.add(new SecondPictureFragment(FirstActivity.this,
fDetail.getItems().get(0).getImage() + "-raw",
null));
}
break;
case 22:
if (fDetail.isSuccess()
&& fDetail.getItems().get(0).getImage() != null) {
list.add(new ThirdPictureFragment(FirstActivity.this,
fDetail.getItems().get(0).getImage() + "-raw",
null));
}
break;
MyFragementAdapter adapter = new MyFragementAdapter(
getSupportFragmentManager(), list);
viewpager.setAdapter(adapter);

}


@Override
public void onFailure(int arg0, Header[] arg1, byte[] arg2,
Throwable arg3) {
Toast.makeText(FirstActivity.this, "网络连接异常", Toast.LENGTH_SHORT)
.show();
}
});
}

}






public class MyFragementAdapter extends FragmentPagerAdapter {
ArrayList fragements;


public MyFragementAdapter(FragmentManager fm, ArrayList fragements) {
super(fm);
this.fragements = fragements;
}


@Override
public Fragment getItem(int arg0) {
return fragements.get(arg0);
}


@Override
public int getCount() {
return fragements.size();
 
}

}

你可能感兴趣的:(实现广告图片循环轮播效果(包括实现手动滑动循环))