本文实例为大家分享了ViewPager实现图片切换效果的具体代码,供大家参考,具体内容如下
xml布局:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="260dp">
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:background="#000"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="@+id/layout_point"
android:background="#00000000"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="20dp"
android:orientation="horizontal"
android:layout_marginBottom="40dp"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
activity代码:
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.example.uoso.myapplicationdemo.R;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* ViewPager显示图片
*/
public class Fragment2 extends Fragment{
private ViewPager viewPager;
private LinearLayout layoutPoint;
private String[] imgs = {"1图片url"
,"2图片url",
"3图片url"};
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment2, null);
viewPager = (ViewPager) view.findViewById(R.id.view_pager);
layoutPoint = (LinearLayout) view.findViewById(R.id.layout_point);
initView();
return view;
}
private void initView(){
//ViewPager相关
ViewPagerAdater myAdater = new ViewPagerAdater(getContext());
viewPager.setAdapter(myAdater);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
//在滑动完成后向布局中添加小圆点
setLayoutPoint(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
setLayoutPoint(0);
}
/**
* 设置小圆点布局
* @param position
*/
private void setLayoutPoint(int position){
layoutPoint.removeAllViews();
for (int i = 0; i <3 ; i++) {
ImageView imageView = new ImageView(getContext());
//设置ImageView
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(20, 20);
params.setMargins(10,0,10,0);
imageView.setLayoutParams(params);
//设置小圆点样式
if (position==i){
imageView.setImageDrawable(getResources().getDrawable(R.drawable.point_white));
}else {
imageView.setImageDrawable(getResources().getDrawable(R.drawable.point_gray));
}
layoutPoint.addView(imageView);
}
}
public class ViewPagerAdater extends PagerAdapter {
private Context context;
public ViewPagerAdater(Context context) {
this.context = context;
}
@Override
public int getCount() {
return imgs.length;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
Glide.with(context).load(imgs[position]).into(imageView);
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((ImageView) object);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
}
小圆点样式:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#fddc04"/>
<corners android:radius="5dp"/>
</shape>
Glide依赖:
compile 'com.github.bumptech.glide:glide:3.7.0'