这篇文章主要为大家详细介绍了ViewPager实现图片切换效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了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'
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程学习网。
沃梦达教程
本文标题为:ViewPager实现图片切换效果
猜你喜欢
- 最好用的ios数据恢复软件:PhoneRescue for Mac 2023-09-14
- 详解flutter engine 那些没被释放的东西 2022-12-04
- iOS 对当前webView进行截屏的方法 2023-03-01
- Android studio实现动态背景页面 2023-05-23
- SurfaceView播放视频发送弹幕并实现滚动歌词 2023-01-02
- Flutter实现底部和顶部导航栏 2022-08-31
- Android实现轮询的三种方式 2023-02-17
- Android实现监听音量的变化 2023-03-30
- Android MaterialButton使用实例详解(告别shape、selector) 2023-06-16
- 作为iOS开发,这道面试题你能答出来,说明你基础很OK! 2023-09-14