XRecycleView的一系列事件

在Gradle中,导入依赖:

compile 'com.jcodecraeer:xrecyclerview:1.2.0'

在布局文件中:

    <com.jcodecraeer.xrecyclerview.XRecyclerView
                android:id="@+id/rc_list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    </com.jcodecraeer.xrecyclerview.XRecyclerView>

在main方法中的代码:(我这是在Fragment中,是Activity的子类,改一改上下文就可以)(getActivity())—(MainActivity.this)

public class List_Fragment extends Fragment implements PersonInterface{
    private View view;
    private ArrayList<RecycleBean> list_recyle = new ArrayList<>();
    private XRecyclerView rc_list;
    private Adapter adapter;
    // 加载页数
    private int page = 1;
    // 是否是刷新
    private boolean isFresh = true;
    private Person person;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    //实例化布局
        view = inflater.inflate(R.layout.list_fragment, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    private void initView() {
    //找到控件
        rc_list = (XRecyclerView)view.findViewById(R.id.rc_list);
        //设置支持上拉加载,(默认支持下拉加载)
        rc_list.setLoadingMoreEnabled(true);
    //设置风格        rc_list.setLaodingMoreProgressStyle(ProgressStyle.SquareSpin);
    //定义XRecycleView的监听事件
        rc_list.setLoadingListener(new XRecyclerView.LoadingListener() {
        //下拉刷新
            @Override
            public void onRefresh() {
                isFresh = true;
                page = 1;
                //重新请求网络的方法
                Load();
            }
        //上拉加载
            @Override
            public void onLoadMore() {
                isFresh = false;
                page++;
                Load();
            }
        });
    //设置适配器
        adapter = new Adapter(getActivity(), list_recyle);
        //P层的方法,(我自己封装了一个OkHttp的请求方式)
        person = new Person(this);
        Load();
        //定义视图的布局方式
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        //设置布局管理者
        rc_list.setLayoutManager(linearLayoutManager);
        //设置适配器
        rc_list.setAdapter(adapter);
        //长按事件
        initLongClick();
        //点击事件
        initClick();
    }
//点击事件
 private void initClick() {
 //适配器点击事件的接口
        adapter.setPageRecycleclickInterfaceListen(new PageRecycleclickInterface() {
            @Override
            public void pageclick(int position) {
            //点击跳转
                Intent intent = new Intent(getActivity(), TiaoActivity.class);
                startActivity(intent);
            }
        });
    }

    //设置长按监听
    private void initLongClick() {
    //适配器中的长按接口
        adapter.setPageRecyceviewInterfaceListen(new PageRecyceviewInterface() {
            @Override
            public void asd(int a) {
            //长按删除
                list_recyle.remove(a);
                //刷新适配器
                adapter.notifyDataSetChanged();
            }
        });
    }
    //设置请求网络
    private void Load() {
    //在这里做网络请求就可以。。        person.get("http://tingapi.ting.baidu.com/v1/restserver/ting","baidu.ting.billboard.billList",page+"","10","0");
    }

    /*
    * pic_small 图片
    * si_proxycompany 简介
    * author 歌手
    * title 歌名
    * */
    //P层实现接口重写的方法(成功)
    @Override
    public void success(HttpBean bean) {
    //将上拉加载,下拉刷新收回去
            if(isFresh){
            //收回下拉刷新
                rc_list.refreshComplete();
            }else{
            //收回上拉加载
                rc_list.loadMoreComplete();
            }
            if(null != bean){
            //如果是下拉刷新,就清空集合,重新请求
                if(isFresh){
                    list_recyle.clear();
                }
                //添加集合数据
                List<HttpBean.SongListBean> songList = bean.getSongList();
                for (int i = 0; i < songList.size(); i++) {
                    HttpBean.SongListBean songListBean = songList.get(i);
                    String picSmall = songListBean.getPicSmall();
                    String siProxycompany = songListBean.getSiProxycompany();
                    String author = songListBean.getAuthor();
                    String title = songListBean.getTitle();
                    list_recyle.add(new RecycleBean(picSmall,siProxycompany,author,title));
                    //刷新适配器
                    adapter.notifyDataSetChanged();
                }
            }
    }
    //失败的方法
    @Override
    public void failed(String message) {
        Log.e("----",message);
    }
    //解除绑定的放法
    @Override
    public void onDestroy() {
        super.onDestroy();
        if(person==null){
            person.unBind();
        }
    }

适配器的代码:

public class Adapter extends RecyclerView.Adapter<Adapter.Viewhoder>{
//定义的接口方法
    private PageRecyceviewInterface pageRecyceviewInterface;
    private PageRecycleclickInterface pageRecycleclickInterface;
    //传一个上下文,一个集合
    private Context context;
    private ArrayList<RecycleBean> list;
    //调用接口的方法
    public void setPageRecyceviewInterfaceListen(PageRecyceviewInterface pageRecyceviewInterface){
        this.pageRecyceviewInterface = pageRecyceviewInterface;
    }
    public void setPageRecycleclickInterfaceListen(PageRecycleclickInterface pageRecycleclickInterface){
        this.pageRecycleclickInterface = pageRecycleclickInterface;
    }
    public Adapter(Context context, ArrayList<RecycleBean> list) {
        this.context = context;
        this.list = list;
    }
    //重写的方法
    @Override
    public Viewhoder onCreateViewHolder(ViewGroup parent, int viewType) {
    //实例化视图
        View inflate = View.inflate(context, R.layout.recycle_page, null);
        //返回一个viewhoder
        Viewhoder viewhoder = new Viewhoder(inflate);
        return viewhoder;
    }

    @Override
    public void onBindViewHolder(Viewhoder holder, final int position) {
    //给hoder.itemView的点击事件
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            //将当前下标放进接口中
                pageRecycleclickInterface.pageclick(position);
            }
        });
        //给hoder.itemView的长按事件
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                pageRecyceviewInterface.asd(position);
                return true;
            }
        });
    //将集合中的数据放进控件中        Glide.with(context).load(list.get(position).getPicSmall()).into(holder.iv_title);
        holder.tv_song_name.setText(list.get(position).getTitle());
        holder.tv_author.setText(list.get(position).getAuthor()+"   "+list.get(position).getSiProxycompany());
    }
    //返回集合的长度
    @Override
    public int getItemCount() {
        if(list != null){
            return list.size();
        }
        return 0;
    }
    //创建自己的viewhoder继承RecyceView的Viewhoder
    class Viewhoder extends RecyclerView.ViewHolder{

        private final ImageView iv_title;
        private final TextView tv_song_name;
        private final TextView tv_author;

        public Viewhoder(View itemView) {
            super(itemView);
            //找控件`这里写代码片
            iv_title = (ImageView)itemView.findViewById(R.id.iv_img);
            tv_song_name = (TextView)itemView.findViewById(R.id.tv_song_name);
            tv_author = (TextView)itemView.findViewById(R.id.tv_author);

        }
    }
}

自定义的接口:(自己创建两个类::)

//点击的接口
public interface PageRecyceviewInterface {
    void asd(int a);
}
//长按的接口
public interface PageRecycleclickInterface {
    void pageclick(int position);
}

猜你喜欢

转载自blog.csdn.net/dealpoor/article/details/78514965