Android实现图片轮播列表

 更新时间:2019年06月25日 11:39:11   作者:free5156  
这篇文章主要为大家详细介绍了Android实现图片轮播列表,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

这个效果在交友app中比较常见,一般作为首页使用,顶部是一个自动轮播的ViewPager,下面放一个横向LinearLayout,最下面要放一个ListView,但是注意这三个部分都支持滑动,应该和固定在顶部的标题栏和底部的操作栏分开,也就是把ViewPager和LinearLayout以及ListView都放在父控件ScrollView里面

布局文件代码:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">

  <include layout="@layout/title_bar"/>

<ScrollView
  android:layout_width="match_parent"
  android:id="@+id/sv"
  android:layout_height="0dp"
  android:layout_weight="1">

<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical">

  <android.support.v4.view.ViewPager
    android:id="@+id/vp"
    android:layout_width="match_parent"
    android:layout_height="200dp"/>

  <LinearLayout
    android:id="@+id/ivs"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_margin="10dp"
    android:orientation="horizontal">

    <ImageView
      android:id="@+id/iv1"
      android:layout_weight="1"
      android:layout_marginStart="30dp"
      android:layout_width="60dp"
      android:layout_height="60dp"
      android:src="@drawable/index_icon"/>

    <ImageView
      android:id="@+id/iv2"
      android:layout_weight="1"
      android:layout_width="60dp"
      android:layout_height="60dp"
      android:src="@drawable/index_icon"/>

    <ImageView
      android:id="@+id/iv3"
      android:layout_weight="1"
      android:layout_width="60dp"
      android:layout_height="60dp"
      android:src="@drawable/index_icon"/>

    <ImageView
      android:id="@+id/iv4"
      android:layout_weight="1"
      android:layout_marginEnd="30dp"
      android:layout_width="60dp"
      android:layout_height="60dp"
      android:src="@drawable/index_icon"/>
  </LinearLayout>

  <com.oridway.www.uiframe.utils.ListViewForScrollView
    android:id="@+id/lvfsv"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
</LinearLayout>
</ScrollView>

  <LinearLayout
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="horizontal">

    <TextView
      android:id="@+id/index"
      android:layout_width="60dp"
      android:layout_height="wrap_content"
      android:drawableTop="@drawable/ic_home_black_24dp"
      android:gravity="center"
      android:text="首页"
      android:textColor="@color/black" />

    <TextView
      android:id="@+id/message"
      android:layout_width="60dp"
      android:layout_height="wrap_content"
      android:drawableTop="@drawable/ic_message_black_24dp"
      android:gravity="center"
      android:text="消息"
      android:textColor="@color/black" />

    <TextView
      android:id="@+id/community"
      android:layout_width="60dp"
      android:layout_height="wrap_content"
      android:drawableTop="@drawable/ic_people_black_24dp"
      android:gravity="center"
      android:text="社区"
      android:textColor="@color/black" />

    <TextView
      android:id="@+id/self"
      android:layout_width="60dp"
      android:layout_height="wrap_content"
      android:drawableTop="@drawable/ic_person_black_24dp"
      android:gravity="center"
      android:text="我"
      android:textColor="@color/black" />
  </LinearLayout>
</LinearLayout>

主窗口代码:

public class IndexActivity extends AppCompatActivity implements View.OnClickListener{

  private Context mContext;
  private List<Integer> mImageList;
  private List<Candidate> mCandidateList;
  private ViewPagerAdapter mPagerAdapter;
  private CandidateListAdapter mListAdapter;

  @SuppressLint("HandlerLeak")
  private Handler handler = new Handler() {

    @Override
    public void handleMessage(Message msg) {
      //每次将当前的位置加1,也就是向右滑动一次
      vp.setCurrentItem(vp.getCurrentItem() + 1);
      //递归无限循环调用
      handler.sendEmptyMessageDelayed(0x123, 2000);
    }
  };

  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_index);
    ButterKnife.bind(this);

    initData();
    initView();
    intListener();
  }

  //初始化数据源,固定写法 1.实例化容器 2.实例化适配器 3.设置适配器
  private void initData() {
    mContext = this;
    mImageList = new ArrayList<>();
    mCandidateList = new ArrayList<>();
    mListAdapter = new CandidateListAdapter(mCandidateList);
    mPagerAdapter = new ViewPagerAdapter(mImageList);
    lvfsv.setAdapter(mListAdapter);
    vp.setAdapter(mPagerAdapter);

    getListData(10);
    getPagerData();

    //间隔2秒发送一次信息
    handler.sendEmptyMessageDelayed(0x123, 2000);
  }

  //生成ViewPager数据源
  private void getPagerData() {
    mImageList.add(R.drawable.bm1);
    mImageList.add(R.drawable.bm2);
    mImageList.add(R.drawable.bm3);
    mImageList.add(R.drawable.bm4);
    mImageList.add(R.drawable.bm5);
    mImageList.add(R.drawable.bm6);

    mPagerAdapter.notifyDataSetChanged();
    //初始的位置在正中间
    vp.setCurrentItem(mPagerAdapter.getCount() / 2);
  }

  //生成ListView数据源
  private void getListData(int num) {
    for (int i = 0; i < num; i++) {
      Candidate candidate = new Candidate();
      candidate.setName("姓名:尼尔斯·亨利克·戴维·玻尔");
      candidate.setInfo("职业:学者,物理学家,足球运动员");
      candidate.setTrait("成就:哥本哈根学派的创始人,1922年获得诺贝尔物理学奖");
      mCandidateList.add(candidate);
    }

    mListAdapter.notifyDataSetChanged();
  }

  private void initView() {
    tvTitleMiddle.setText("轮播列表");
    //手动设置ScrollView的位置
    scrollView.smoothScrollTo(0, 0);
  }

  //初始化监听
  private void intListener() {
    mPagerAdapter.setmCallback((v, position) -> {
      Toast.makeText(mContext, "position: " + position, Toast.LENGTH_SHORT).show();
    });

    lvfsv.setOnItemClickListener((parent, view, position, id) -> {
      Toast.makeText(mContext, "position: " + position, Toast.LENGTH_SHORT).show();
    });

    for (int i = 0; i < 4; i++) {
      ivs.getChildAt(i).setOnClickListener(this);
    }
  }

  @Override
  public void onClick(View v) {

    switch (v.getId()){
      case R.id.iv1:
      case R.id.iv2:
      case R.id.iv3:
      case R.id.iv4:
        Toast.makeText(mContext, "此处跳转", Toast.LENGTH_SHORT).show();
    }
  }
}

ListView需要覆盖onMeasure方法,代码如下:

public class ListViewForScrollView extends ListView {
  public ListViewForScrollView(Context context) {
    super(context);
  }
  public ListViewForScrollView(Context context, AttributeSet attrs) {
    super(context, attrs);
  }
  public ListViewForScrollView(Context context, AttributeSet attrs,
    int defStyle) {
    super(context, attrs, defStyle);
  }
  @Override
  /**
   * 重写该方法,达到使ListView适应ScrollView的效果
   */
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
    MeasureSpec.AT_MOST);
    super.onMeasure(widthMeasureSpec, expandSpec);
  }
}

ViewPager适配器代码:

public class ViewPagerAdapter extends PagerAdapter implements View.OnClickListener {

  //图片的资源id列表
  private List<Integer> mList;
  private Callback mCallback;

  public ViewPagerAdapter(List<Integer> mList) {
    this.mList = mList;
  }

  public void setmCallback(Callback mCallback) {
    this.mCallback = mCallback;
  }

  public interface Callback {
    void onClick(View v, int position);
  }

  @Override
  //将适配器中的数据设为无穷大
  public int getCount() {
    return Integer.MAX_VALUE;
  }

  @Override
  //固定写法,不覆盖会报错
  public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
    container.removeView((View) object);
  }

  @Override
  //固定写法
  public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
    return view == object;
  }

  @NonNull
  @Override
  public Object instantiateItem(@NonNull ViewGroup container, int position) {
    LayoutInflater inflater = LayoutInflater.from(container.getContext());
    ImageView imageView = (ImageView) inflater.inflate(R.layout.item_image_pager, null);

    //将position转换成余数
    int realPosition = position % mList.size();
    imageView.setImageResource(mList.get(realPosition));
    imageView.setOnClickListener(this);
    //tag放跳转需要的数据
    imageView.setTag(realPosition);
    //将实例加入父控件
    container.addView(imageView);
    return imageView;
  }

  @Override
  //使用接口将position回传
  public void onClick(View v) {
    mCallback.onClick(v, (int) v.getTag());
  }
}

大功告成,实现效果如下:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Android zxing如何识别反转二维码详解

    Android zxing如何识别反转二维码详解

    这篇文章主要给大家介绍了关于Android zxing如何识别反转二维码的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-09-09
  • Android实现倒计时30分钟功能

    Android实现倒计时30分钟功能

    这篇文章主要为大家详细介绍了Android实现倒计时30分钟功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-05-05
  • Android编程实现播放视频时切换全屏并隐藏状态栏的方法

    Android编程实现播放视频时切换全屏并隐藏状态栏的方法

    这篇文章主要介绍了Android编程实现播放视频时切换全屏并隐藏状态栏的方法,结合实例形式分析了Android视频播放事件响应及相关属性设置操作技巧,需要的朋友可以参考下
    2017-08-08
  • Flutter应用集成极光推送的实现示例

    Flutter应用集成极光推送的实现示例

    这篇文章主要介绍了Flutter应用集成极光推送的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-02-02
  • Android实现点击WebView界面中图片滑动浏览与保存图片功能

    Android实现点击WebView界面中图片滑动浏览与保存图片功能

    大家在日常使用spp流量文章的时候经常会遇到这样的一个功能,点击文章的图片进入图片的浏览模式,可以左右滑动图片浏览,并且可以实现保存图片的功能,所以本文主要就介绍了在Android如何实现点击WebView界面中图片滑动浏览与保存图片功能,需要的朋友可以参考下。
    2017-04-04
  • 快速了解Android Room使用细则进阶

    快速了解Android Room使用细则进阶

    这篇文章主要为大家介绍了快速了解Android Room使用细则进阶,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-03-03
  • Android WebView 缓存详解

    Android WebView 缓存详解

    这篇文章主要介绍了 Android WebView 缓存详解的相关资料,需要的朋友可以参考下
    2017-06-06
  • Android应用读取Excel文件的方法

    Android应用读取Excel文件的方法

    这篇文章主要介绍了Android应用读取Excel文件的方法,涉及Android针对Excel文件的读写保存等相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-12-12
  • Android开发之BottomSheetDialog组件的使用

    Android开发之BottomSheetDialog组件的使用

    BottomSheetDialog是底部操作控件,可在屏幕底部创建一个支持滑动关闭视图。本文将通过示例详细讲解它的使用,感兴趣的小伙伴可以了解一下
    2023-01-01
  • Android中使用Toast.cancel()方法优化toast内容显示的解决方法

    Android中使用Toast.cancel()方法优化toast内容显示的解决方法

    做程序员的,基本一看api就知道,用这个可以取消上一个toast的显示,然后显示下一个,这样就能解决出现的问题。可是在测试的过程中,发现却没有想象中的那么简单,不信可以百度一下,很多很多人发现toast的cancel()方法不起作用
    2013-05-05

最新评论