package com.bwei.ViewPager;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import com.example.webservice.R;
public class ShapeActivity extends Activity {
private ViewPager viewPager;
private List<View> list;
private LinearLayout pointGroup;
// 上一个指示点的下标
private int lastPointIdex = 0;
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
int currentItem = viewPager.getCurrentItem();
viewPager.setCurrentItem(currentItem += 1);
handler.sendEmptyMessageDelayed(1, 2000);
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_viewpager);
viewPager = (ViewPager) findViewById(R.id.viewpager);
pointGroup = (LinearLayout) findViewById(R.id.ll_point_group);
viewPager.setAdapter(new MyPagerAdapter(setData()));
handler.sendEmptyMessageDelayed(1,2000);
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
int myIndex = arg0 % list.size();
// 改变指示点的状态。
// 将当前指示点的图片enable设为 true
pointGroup.getChildAt(myIndex).setEnabled(true);
// 将上一个指示点的设置enable 设为false;
pointGroup.getChildAt(lastPointIdex).setEnabled(false);
// 更新lastPointIdex的值
lastPointIdex = myIndex;
}
@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 List<View> setData() {
// TODO Auto-generated method stub
list = new ArrayList<View>();
list.add(View.inflate(getApplicationContext(),
R.layout.viewpager_show01, null));
list.add(View.inflate(getApplicationContext(),
R.layout.viewpager_show02, null));
list.add(View.inflate(getApplicationContext(),
R.layout.viewpager_show03, null));
list.add(View.inflate(getApplicationContext(),
R.layout.viewpager_show04, null));
for (int i = 0; i < list.size(); i++) {
// 添加指示点
ImageView point = new ImageView(this);
/*
* 子view的LayoutParams参数,要看,当前父view是谁,
* 由于现在point的父view是LinearLayout,所以为point 添加
* LinearLayout.LayoutParams
*
*/
LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, -2);
params.leftMargin = 15;
point.setLayoutParams(params);
point.setBackgroundResource(R.drawable.point_bg);
pointGroup.addView(point);
// 默认情况下,第一个小点enable为true
if (i == 0) {
point.setEnabled(true);
} else {
point.setEnabled(false);
}
}
return list;
}
}
package com.bwei.main;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.exception.DbException;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
private ViewPager vp;
private List<View> list;
private SharedPreferences sp;
private boolean boolean1;
private DbUtils dbUtils;
private List<Data> data;
private static final String URL="http://101.200.142.201:8080/tqyb/newsList.json";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vp=(ViewPager) findViewById(R.id.vp);
list=new ArrayList<View>();
BitmapUtils bitmapUtils=new BitmapUtils(getApplicationContext());
View view01 = View.inflate(getApplicationContext(), R.layout.viewpager_show01, null);
View view02 = View.inflate(getApplicationContext(), R.layout.viewpager_show02, null);
list.add(view01);
list.add(view02);
ImageView image01=(ImageView) view01.findViewById(R.id.imageView1);
bitmapUtils.configDefaultLoadingImage(R.drawable.image_b);
bitmapUtils.display(image01, "http://101.200.142.201:8080/tqyb/images/top1.png");
ImageView image02=(ImageView) view02.findViewById(R.id.imageView2);
bitmapUtils.display(image02, "http://101.200.142.201:8080/tqyb/images/top2.png");
dbUtils = DbUtils.create(getApplicationContext(), "1407C");
try {
dbUtils.createTableIfNotExist(Data.class);
} catch (DbException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sp=getSharedPreferences("main", MODE_PRIVATE);
boolean1 = sp.getBoolean("image", true);
if (boolean1) {
XUtils();
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 list.size();
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
container.addView(list.get(position));
if (list.size() - 1 == position) {
TextView tv = (TextView) list.get(position)
.findViewById(R.id.vp02_tv);
tv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,
LoginActivity.class);
startActivity(intent);
boolean1 = sp.edit().putBoolean("image", false).commit();
finish();
}
});
}
return list.get(position);
}
@Override
public void destroyItem(ViewGroup container, int position,
Object object) {
// TODO Auto-generated method stub
container.removeView(list.get(position));
}
});
}else{
Intent intent = new Intent(MainActivity.this,
LoginActivity.class);
startActivity(intent);
finish();
}
}
private void XUtils() {
// TODO Auto-generated method stub
HttpUtils httpUtils=new HttpUtils();
httpUtils.send(HttpMethod.GET, URL, new RequestCallBack<String>() {
@Override
public void onFailure(HttpException arg0, String arg1) {
// TODO Auto-generated method stub
}
@Override
public void onSuccess(ResponseInfo<String> arg0) {
// TODO Auto-generated method stub
Gson gson=new Gson();
All all = gson.fromJson(arg0.result, All.class);
data = all.getList().getArticles();
for (Data str : data) {
AddSQL(str);
}
}
});
}
private void AddSQL(Data str) {
// TODO Auto-generated method stub
try {
dbUtils.save(str);
} catch (DbException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}