shape里面的书写
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/point_red" android:state_enabled="true"></item><item android:drawable="@drawable/point_green" android:state_enabled="false"></item>
</selector>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
>
<solid android:color="@android:color/holo_green_light"></solid>
</shape>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
>
<solid android:color="@android:color/holo_red_light"></solid>
</shape>
上面是小圆点的布局
下面在代码中的实现
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
import cz.msebera.android.httpclient.Header;
public class MainActivity extends AppCompatActivity {
private ViewPager vp;
private LinearLayout ll_point;
private TextView tv_info;
private List<String> infos = new ArrayList<>();
private List<ImageView> images = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找控件
initView();
//获取网络数据
getServerData();
}
private void initView() {
vp = (ViewPager) findViewById(R.id.vp);
ll_point = (LinearLayout) findViewById(R.id.ll_point);
tv_info = (TextView) findViewById(R.id.tv_info);
}
private void getServerData() {
String url = "http://mock.eolinker.com/success/aDpzG1ZiKPbEI6JdXjqasb958Q1rBg9j";
AsyncHttpClient client = new AsyncHttpClient();
client.get(this, url, new TextHttpResponseHandler() {
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
}
@Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
try {
JSONObject jsonObject = new JSONObject(responseString);
JSONArray data = jsonObject.getJSONArray("data");
for (int i = 0; i < data.length(); i++) {
City city = new City();
JSONObject jo = data.getJSONObject(i);
String content = jo.getString("content");
infos.add(content);
String image_url = jo.getString("image_url");
ImageView imageView = new ImageView(MainActivity.this);
Picasso.with(MainActivity.this).load(image_url).error(R.mipmap.ic_launcher).placeholder(R.mipmap.ic_launcher).into(imageView);
images.add(imageView);
}
initViewPager();
Toast.makeText(MainActivity.this, "图片个数:" + images.size(), Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
private void initViewPager() {
MyAdapter adapter = new MyAdapter();
vp.setAdapter(adapter);
initPoint();
vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
//获取小圆点数据
int count = ll_point.getChildCount();
for (int i = 0; i < count; i++) {
View view = ll_point.getChildAt(i);
//页面改变时选中小圆点
view.setEnabled(i == position ? false : true);
}
//页面改变时选中文字
tv_info.setText(infos.get(position));
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
private void initPoint() {
// ll_point.removeAllViews();
for (int i = 0; i < images.size(); i++) {
// Toast.makeText(MainActivity.this, "图片个数:"+images.size(), Toast.LENGTH_SHORT).show();
//创建小圆点控件
View view = new View(this);
//设置背景选择器
view.setBackgroundResource(R.drawable.point_bg);
//创建小圆点大小
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(20, 20);
//创建小圆点间距
params.rightMargin = 10;
//把小圆点添加容器
ll_point.addView(view, params);
view.setTag(i);
}
//设置第一个为默认选择
View view = ll_point.getChildAt(0);
view.setEnabled(false);
//设置第一个文字为默认选择
tv_info.setText(infos.get(0));
}
//定义handler
private Handler handler = new Handler();
//开启
public void startSwitch() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
int item = vp.getCurrentItem();
if (item == vp.getAdapter().getCount() - 1) {
item = 0;
} else {
item++;
}
vp.setCurrentItem(item);
handler.postDelayed(this, 2000);
}
}, 2000);
}
//停止
public void stopSwitch() {
handler.removeCallbacksAndMessages(null);
}
//创建适配器
private class MyAdapter extends PagerAdapter {
@Override
public int getCount() {
return images.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = images.get(position);
// vp.addView(imageView);
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(images.get(position));
}
}
@Override
protected void onResume() {
super.onResume();
startSwitch();
}
@Override
protected void onPause() {
super.onPause();
stopSwitch();
}
}