Android Studio 实现下拉列表刷新并嵌套轮播图(自动轮播+手动轮播+点击事件)

亲爱的女士们、先生们,还有不请自到的媒体朋友们,大家下午好。 无敌帅气的我回来了,今天给大家带来的是一个灰常灰常见的一个app首页效果,自己动手丰衣足食,不胜感激,感激~~


搞事情啦!先来看一下效果图

Android Studio 实现下拉列表刷新并嵌套轮播图(自动轮播+手动轮播+点击事件)_第1张图片


首先,先送上轮播图要用的依赖包:
http://download.csdn.net/download/qq_35840038/10107248

还有,再送上下拉列表所用的jar包:
http://download.csdn.net/download/qq_35840038/10107259

下来就开始走一波代码了:

1.. 先实现下拉列表的功能。在activity_main.xml中配置下拉刷新的自定义控件

.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        
        .support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        .support.v7.widget.RecyclerView>

    .support.v4.widget.SwipeRefreshLayout>

2.获取控件ID,并实现控件刷新和item的点击事件:

final SwipeRefreshLayout sf = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
        RecyclerView rc = (RecyclerView) findViewById(R.id.recyclerView);

        /**
         * true 表示下拉转的那个圈进行变化
         * 50代表下拉的开始高度
         * 150代表下拉的最终高度
         */
        sf.setProgressViewOffset(true, 50, 150);

        /**
         * 设置圈的颜色
         */
        sf.setColorSchemeResources(R.color.colorAccent);

        /**
         * 刷新事件
         */
        sf.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(MainActivity.this, "暂无消息更新", Toast.LENGTH_SHORT).show();
                        sf.setRefreshing(false);
                        reAdapter.notifyDataSetChanged();
                    }
                }, 3000);
            }
        });


        /**
         * 给adapter传值
         *
         */
        reAdapter = new ReAdapter("天才");

        /**
         * 让item垂直向下排列
         */
        rc.setLayoutManager(new LinearLayoutManager(this));

        /**
         * 绑定数据
         */
        rc.setAdapter(reAdapter);

        /**
         * item点击事件
         */
        reAdapter.setOnItemClick(new ReAdapter.OnItemClick() {
            @Override
            public void succ(View view, int position) {
                Toast.makeText(MainActivity.this, "当前选中的是第" + position + "个", Toast.LENGTH_SHORT).show();
            }
        });

3.书写下拉的adapter,在adapter中获取下拉列表的长度和下拉列表的信息绑定:

public class ReAdapter extends RecyclerView.Adapter<ReAdapter.ViewHolder> {

    String name;

    /**
     * 获取传递的值
     * @param usn
     */
    public ReAdapter(String usn){
        this.name = usn;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        //找到item布局,并传入holder实现
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {
        //绑定数据
        viewHolder.tv.setText(name);
    }

    @Override
    public int getItemCount() {
        //长度
        return 10;
    }

    /**
     * 获取控件并实现点击事件
     */
    public class ViewHolder extends RecyclerView.ViewHolder {

        TextView tv;

        public ViewHolder(View view) {
            super(view);
            tv = view.findViewById(R.id.textView);
            tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClick.succ(v, getPosition());
                }
            });
        }
    }

    /**
     * 单击事件
     */
    private OnItemClick onItemClick;

    public void setOnItemClick(OnItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }

    public interface OnItemClick{
        void succ(View view, int position);
    }



}

4.下拉列表的item.xml配置文件内容:


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

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:text="TextView"
        android:gravity="center"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_below="@+id/textView"
        android:background="#000000"/>

RelativeLayout>
到这里,下拉刷新的效果就已经实现了。接下来开始往下拉组件中加入轮播图效果:

加入轮播图之前,先将轮播图的依赖文件拷贝入项目,并保证能够正确运行;

5.加入轮播图,先修改item.xml布局文件:


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

    <com.ryane.banner_lib.AdPlayBanner
        android:id="@+id/banner"
        android:layout_width="match_parent"
        android:layout_height="200dp">

    com.ryane.banner_lib.AdPlayBanner>

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_below="@+id/banner"
        android:text="TextView"
        android:gravity="center"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_below="@+id/textView"
        android:background="#000000"/>

RelativeLayout>

6.并修改ReAdapter类,实现数据的刷新和绑定

修改之后的ReAdapter类:
public class ReAdapter extends RecyclerView.Adapter {

    String name;

    Activity activity;

    /**
     * 获取传递的值
     * @param usn
     */
    public ReAdapter(String usn, Activity at){
        this.name = usn;
        this.activity = at;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        //找到item布局,并传入holder实现
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {
        //绑定数据
        viewHolder.tv.setText(name);
        Fresco.initialize(activity);
        final List mDatas = new ArrayList<>();
        AdPageInfo info1 = new AdPageInfo(
                "春天",   //标题
                "http://bpic.588ku.com/back_pic/04/28/77/67583e7a57534fb.jpg!ww800",    //图片链接
                "",   //描述
                1     //播放顺序
        );
        AdPageInfo info2 = new AdPageInfo(
                "夏天",
                "http://bpic.588ku.com/back_pic/02/64/73/425785ba66f1346.jpg!ww800",
                "",
                2
        );
        AdPageInfo info3 = new AdPageInfo(
                "秋天",
                "http://bpic.588ku.com/back_pic/00/00/52/00/1dcd7f6338745ccc20cafcf14a56d2ae.jpg!ww800",
                "",
                3
        );
        AdPageInfo info4 = new AdPageInfo(
                "冬天",
                "http://bpic.588ku.com/back_pic/00/12/47/62563dbac37e95c.jpg!ww800",
                "", 
                4
        );
        mDatas.add(info1);
        mDatas.add(info2);
        mDatas.add(info3);
        mDatas.add(info4);

        viewHolder.banner
                .setAutoPlay(true)
                .setIndicatorType(POINT_INDICATOR)
                .setNumberViewColor(0xcc00A600, 0xccea0000, 0xffffffff)
                .setInterval(3500)
                .addTitleView(new TitleView(activity).setPosition(PARENT_BOTTOM).setTitlePadding(165, 5, 155, 5).setTitleMargin(0, 0, 0, 25).setTitleSize(16).setViewBackground(0x00000000).setTitleColor(activity.getResources().getColor(R.color.white)))
                .setBannerBackground(0xff000000)
                .setOnPageClickListener(new AdPlayBanner.OnPageClickListener() {
                    @Override
                    public void onPageClick(AdPageInfo info, int postion) {
                        switch (postion){
                            case 0:
                                Toast.makeText(activity, "我是" + mDatas.get(postion).getTitle(), Toast.LENGTH_SHORT).show();
                                Log.d("图片链接:", "" + mDatas.get(postion).getPicUrl());
                                Log.d("图片标题:", "" + mDatas.get(postion).getTitle());
                                break;
                            case 1:
                                Toast.makeText(activity, "我是" + mDatas.get(postion).getTitle(), Toast.LENGTH_SHORT).show();
                                Log.d("图片链接:", "" + mDatas.get(postion).getPicUrl());
                                Log.d("图片标题:", "" + mDatas.get(postion).getTitle());
                                break;
                            case 2:
                                Toast.makeText(activity, "我是" + mDatas.get(postion).getTitle(), Toast.LENGTH_SHORT).show();
                                Log.d("图片链接:", "" + mDatas.get(postion).getPicUrl());
                                Log.d("图片标题:", "" + mDatas.get(postion).getTitle());
                                break;
                            case 3:
                                Toast.makeText(activity, "我是" + mDatas.get(postion).getTitle(), Toast.LENGTH_SHORT).show();
                                Log.d("图片链接:", "" + mDatas.get(postion).getPicUrl());
                                Log.d("图片标题:", "" + mDatas.get(postion).getTitle());
                                break;
                            default:
                                break;
                        }
                    }
                })
                .setPageTransfromer(new FadeInFadeOutTransformer())
                .setInfoList((ArrayList) mDatas)
                .setUp();
    }

    @Override
    public int getItemCount() {
        //长度
        return 1;
    }

    /**
     * 获取控件并实现点击事件
     */
    public class ViewHolder extends RecyclerView.ViewHolder {

        TextView tv;

        AdPlayBanner banner;

        public ViewHolder(View view) {
            super(view);
            tv = view.findViewById(R.id.textView);
            banner = view.findViewById(R.id.banner);
            tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClick.succ(v, getPosition());
                }
            });
        }
    }

    /**
     * 单击事件
     */
    private OnItemClick onItemClick;

    public void setOnItemClick(OnItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }

    public interface OnItemClick{
        void succ(View view, int position);
    }



}
这里的图片用的是网络图片,所以要记得加入网络权限啊



到这为止,所有的功能都已经添加ok了。

附上demo源码

http://download.csdn.net/download/qq_35840038/10107843

OK了,觉得不错的话记得给我点赞哦

你可能感兴趣的:(android-开发实例,轮播图,下拉刷新)