图片轮播组合控件如:
图片可以轮播,现在我们可以一点一点开发
首先明确大框架是ViewPager然后整个布局是RelativeLayout,其中ViewPager布满整个布局,之下是一个线性布局线性布局中有文字和小点两种View,CiewPager是V4包下的,布局文件如下:
<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="160dp" tools:context="com.hty.ad.MainActivity">
<android.support.v4.view.ViewPager android:id="@+id/view_pager" android:layout_width="match_parent" android:layout_height="match_parent"/>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_alignParentBottom="true" android:background="#25000000">
<TextView android:paddingBottom="5dp" android:id="@+id/text_view" android:text="调试完毕" android:gravity="center" android:textSize="20sp" android:textColor="#ffffff" android:layout_width="match_parent" android:layout_height="wrap_content"/>
<LinearLayout android:id="@+id/dot_layout" android:layout_width="match_parent" android:orientation="horizontal" android:layout_height="20dp" android:gravity="center">
LinearLayout>
LinearLayout>
RelativeLayout>
定义一个MyAdapter继承PagerAdapter然后实现getcount()方法和isviewFromObject()方法
代码如下:
class MyAdapter extends PagerAdapter {
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
/** * true: 表示不去创建,使用缓存 false:去重新创建 * view: 当前滑动的view * object:将要进入的新创建的view,由instantiateItem方法创建 */
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
package com.hty.ad;
/** * 作者:司马啸尘 * 创建日期:2016/4/12 * 描述: */
public class Ad {
private int icon;
private String into;
public Ad(int icon, String into) {
this.icon = icon;
this.into = into;
}
public int getIcon() {
return icon;
}
public void setIcon(int icon) {
this.icon = icon;
}
public String getInto() {
return into;
}
public void setInto(String into) {
this.into = into;
}
}
private void initData() {
mAds = new ArrayList();
mAds.add(new Ad(R.drawable.c, "哈哈哈哈h"));
mAds.add(new Ad(R.drawable.c, "ddddddh"));
mAds.add(new Ad(R.drawable.c, "vvvvvv哈h"));
mViewPager.setAdapter(new MyAdapter());
Log.d("setAdapter", "------------");
}
在MyAdapter中重写instantianteItem方法
public Object instantiateItem(ViewGroup container, int position) {
View view = View.inflate(MainActivity.this, R.layout.dapter_ad, null);
ImageView imageView = (ImageView) view.findViewById(R.id.iv_body);
imageView.setImageResource(mAds.get(position % mAds.size()).getIcon());
container.addView(view);
return view;
}
现在就可以实现滑动但是在边界不能滑动,而且小点和文字不同步
对mViewAdapter设置监听,api变化成addOnPagerChangeLiatener,初始化监听器
private void initListener() {
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
update();
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
设置事件同步
private void update() {
int currentPosition = mViewPager.getCurrentItem();
mTextView.setText(mAds.get(currentPosition % mAds.size()).getInto());
for (int i = 0; i < mLinearLayout.getChildCount(); i++) {
mLinearLayout.getChildAt(i).setEnabled(i == currentPosition % mAds.size());
}
}
就是把所有位置变成position % mAds.size()而不是position
然后再添加周期改变:
android.os.Handler mHandler = new android.os.Handler() {
@Override
public void handleMessage(Message msg) {
mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 1);
mHandler.sendEmptyMessageDelayed(0, 1000);
}
};
package com.hty.ad;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ViewPager mViewPager;
private ArrayList mAds;
private TextView mTextView;
private LinearLayout mLinearLayout;
android.os.Handler mHandler = new android.os.Handler() {
@Override
public void handleMessage(Message msg) {
mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 1);
mHandler.sendEmptyMessageDelayed(0, 1000);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initView();
initData();
initListener();
}
private void initListener() {
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
update();
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
private void update() {
int currentPosition = mViewPager.getCurrentItem();
mTextView.setText(mAds.get(currentPosition % mAds.size()).getInto());
for (int i = 0; i < mLinearLayout.getChildCount(); i++) {
mLinearLayout.getChildAt(i).setEnabled(i == currentPosition % mAds.size());
}
}
private void initView() {
setContentView(R.layout.activity_main);
mViewPager = (ViewPager) findViewById(R.id.view_pager);
mTextView = (TextView) findViewById(R.id.text_view);
mLinearLayout = (LinearLayout) findViewById(R.id.dot_layout);
}
private void initData() {
mAds = new ArrayList();
mAds.add(new Ad(R.drawable.c, "哈哈哈哈h"));
mAds.add(new Ad(R.drawable.c, "ddddddh"));
mAds.add(new Ad(R.drawable.c, "vvvvvv哈h"));
mViewPager.setAdapter(new MyAdapter());
Log.d("setAdapter", "------------");
int center = Integer.MAX_VALUE / 2;
mViewPager.setCurrentItem(center);
initDot();
mHandler.sendEmptyMessageDelayed(0, 1000);
update();
}
private void initDot() {
for (int i = 0; i < mAds.size(); i++) {
View view = new View(MainActivity.this);
view.setBackgroundResource(R.drawable.selector);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(20, 20);
layoutParams.setMargins(10, 0, 0, 0);
view.setLayoutParams(layoutParams);
mLinearLayout.addView(view);
}
}
class MyAdapter extends PagerAdapter {
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// super.destroyItem(container, position, object);
container.removeView((View) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View view = View.inflate(MainActivity.this, R.layout.dapter_ad, null);
ImageView imageView = (ImageView) view.findViewById(R.id.iv_body);
imageView.setImageResource(mAds.get(position % mAds.size()).getIcon());
container.addView(view);
return view;
}
}
}
PS:有几天没有更新博客了,四月好美加油