Android ViewPager加载图片效果
短信预约 -IT技能 免费直播动态提醒
目前项目中需要用到ViewPager加载图片,现在在此记录一下。
首先先看布局文件:activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.fly.viewpagerbanner.MainActivity">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_above="@+id/ll"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<LinearLayout
android:id="@+id/ll"
android:layout_marginTop="10.0dip"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="24.0dip"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
MainActivity
package com.example.fly.viewpagerbanner;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener,View.OnClickListener {
private ViewPager vp;
private ViewPagerAdapter vpAdapter;
private List<Map<String, Object>> data;
private int lastValue = -1; // 是不是最后一张图片
private ImageView[] dots; //底部小点图片
private int currentIndex; //记录当前选中位置
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Intent intent = new Intent();
// intent.putStringArrayListExtra("GalleryPlusActivity",ArrayList<String>); // 怎么传递集合
ArrayList<String> imageLists = getIntent().getStringArrayListExtra("GalleryPlusActivity"); // 动态获取展示图片的集合
data = getData(imageLists);
vp = (ViewPager) findViewById(R.id.viewpager);
vpAdapter = new ViewPagerAdapter(data,this);
vp.setAdapter(vpAdapter);
vp.addOnPageChangeListener(this);
initDots();//初始化底部小点
}
//这个方法长度是动态的,可以改成你从服务器获取的图片,这样数量就不确定啦
public List<Map<String, Object>> getData(ArrayList<String> imageLists) {
List<Map<String, Object>> mData = new ArrayList<>();
// if (imageLists != null && imageLists.size() > 0) {
// for (int i = 0; i < imageLists.size(); i++) {
// Map<String, Object> map = new HashMap<>();
// map.put("url", imageLists.get(i));
// map.put("view", new ImageView(this));
// mData.add(map);
// }
// }
Map<String, Object> map = new HashMap<>();
map.put("url", "http://huoche.7234.cn/images/jb51/va2zpt3i3rq");
map.put("view", new ImageView(this));
mData.add(map);
Map<String, Object> map1 = new HashMap<>();
map1.put("url", "http://huoche.7234.cn/images/jb51/1ck35mgwned");
map1.put("view", new ImageView(this));
mData.add(map1);
Map<String, Object> map2 = new HashMap<>();
map2.put("url", "http://download.pchome.net/wallpaper/pic-5041-8-240x320.jpg");
map2.put("view", new ImageView(this));
mData.add(map2);
Map<String, Object> map3 = new HashMap<>();
map3.put("url", "/file/upload/202206/06/e5e334jtgcu.jpg");
map3.put("view", new ImageView(this));
mData.add(map3);
return mData;
}
private void initDots() {
LinearLayout.LayoutParams mLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
mLayoutParams.setMargins(3, 0, 3, 0);
LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
dots = new ImageView[data.size()];
//循环取得小点图片
for (int i = 0; i < data.size(); i++) {
dots[i] = new ImageView(this);
dots[i].setLayoutParams(mLayoutParams);
dots[i].setBackgroundResource(R.drawable.dot);
dots[i].setEnabled(true);//都设为灰色
dots[i].setOnClickListener(this);
dots[i].setTag(i);//设置位置tag,方便取出与当前位置对应
ll.addView(dots[i]);
}
currentIndex = 0;
dots[currentIndex].setEnabled(false);//设置为白色,即选中状态
}
private void setCurView(int position) {
if (position < 0 || position >= data.size()) {return;}
vp.setCurrentItem(position);
}
private void setCurDot(int position) {
if (position < 0 || position > data.size() - 1 || currentIndex == position) {return;}
dots[position].setEnabled(false);
dots[currentIndex].setEnabled(true);
currentIndex = position;
}
@Override //当前页面被滑动时调用
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
lastValue = position;
}
@Override
public void onPageSelected(int position) { //当新的页面被选中时调用
setCurDot(position); //设置底部小点选中状态
}
@Override
public void onPageScrollStateChanged(int state) { //当滑动状态改变时调用
if(state == 0){
if(lastValue == data.size()-1){
Toast.makeText(this, "已经是最后一张了", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onClick(View v) {
int position = (Integer)v.getTag();
setCurView(position);
setCurDot(position);
}
}
ViewPagerAdapter
package com.example.fly.viewpagerbanner;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import java.util.List;
import java.util.Map;
public class ViewPagerAdapter extends PagerAdapter {
Context context;
List<Map<String, Object>> viewLists;
public ViewPagerAdapter(List<Map<String, Object>> lists, Context context) {
this.viewLists = lists;
this.context = context;
}
@Override
public int getCount() { //获得size
return viewLists.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public void destroyItem(ViewGroup view, int position, Object object) { //销毁Item
ImageView x = (ImageView) viewLists.get(position).get("view");
x.setScaleType(ImageView.ScaleType.FIT_CENTER);
view.removeView(x);
}
@Override
public Object instantiateItem(ViewGroup view, int position){ //实例化Item
ImageView imageView = (ImageView) viewLists.get(position).get("view");
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
Glide.with(context)
.load(viewLists.get(position).get("url").toString())
// .placeholder(R.mipmap.new_default)
.error(R.mipmap.ic_launcher)
.diskCacheStrategy(DiskCacheStrategy.RESULT)
.into(imageView);
view.addView(imageView, 0);
return viewLists.get(position).get("view");
}
}
是不是很简单。
最后附上demo:http://xiazai.jb51.net/201701/yuanma/ViewPager(jb51.net).rar
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341