(Fragment)XRecyclerView上下拉刷新加载+GreenDao(缓存,查看)

//FragmentActivity

public class HomeFragment extends Fragment {
    private static final String TAG = "HomeFragment";
    private OkUtils okUtils;
    private int page = 3;
    private XRecyclerView xRecyclerView;
    private DaoSession daoSession;
    private BeanDao beanDao;
    private int cc = 1;
    private List datas = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.home_fragment,container,false);
        xRecyclerView = view.findViewById(R.id.home_xrecycler);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        daoSession = DaoManger.instance(getContext()).getDaoSession();
        beanDao = daoSession.getBeanDao();
        int page;
        initnews();
    }

    private void initnews( ) {
        okUtils = OkUtils.getokClient();
        String urls="https://www.zhaoapi.cn/quarter/getJokes?source=android&appVersion=100&page="+page;
        Log.d(TAG,urls+"-------------------");
        okUtils.doGet(urls, new OkUtils.IOkutils() {

            private JavaAdaper javaAdaper;

            @Override
            public void onFailure(IOException e) {
                Log.d(TAG,e+"");
            }

            @Override
            public void onResponse(String json) {

                Gson gson = new Gson();
                JavaBean javaBean = gson.fromJson(json, JavaBean.class);
                List data = javaBean.getData();
                final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
                linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                xRecyclerView.setLayoutManager(linearLayoutManager);
                javaAdaper = new JavaAdaper(datas);
                xRecyclerView.setAdapter(javaAdaper);
                javaAdaper.setOnClickJavaAdaper(new JavaAdaper.OnClickJavaAdaper() {
                    @Override
                    public void OnJavaAdaperClick(Bean bean) {
                        beanDao.insert(bean);
                        List list = beanDao.queryBuilder().list();
                        for (int i = 0; i < list.size(); i++) {
                            Log.d(TAG,list.get(i).toString());
                        }
                    }
                });
                if (cc==1){
                    datas.clear();
                    xRecyclerView.refreshComplete();
                }else {
                    xRecyclerView.loadMoreComplete();
                }
                datas.addAll(data);

                xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
                    @Override
                    public void onRefresh() {
                        cc=1;
                        page = 3;
                        initnews();
                    }

                    @Override
                    public void onLoadMore() {
                        cc = 2;
                        ++page;
                        Log.d(TAG,page+"");
                        initnews();
                    }
                });
            }
        });
    }
}



//适配器

public class JavaAdaper extends XRecyclerView.Adapter{
    private List data;
    private JavaAdaper.javaViewHolder javaViewHolder;

    public JavaAdaper(List data) {
        this.data = data;
    }

    @Override
    public javaViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.java_adaper,parent,false);
        javaViewHolder = new javaViewHolder(view);
        return javaViewHolder;
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public void onBindViewHolder(javaViewHolder holder, final int position) {
        holder.textView.setText(data.get(position).getContent());
        Uri uri = Uri.parse(data.get(position).getUser().getIcon()+"");
        holder.simpleDraweeView1.setImageURI(uri);
        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bean bean = new Bean(null,data.get(position).getContent(),data.get(position).getUser().getIcon()+"");
                onClickJavaAdaper.OnJavaAdaperClick(bean);
            }
        });
    }

    class javaViewHolder extends RecyclerView.ViewHolder{

        private final TextView textView;
        private final SimpleDraweeView simpleDraweeView1;
        private final ImageView imageView;

        public javaViewHolder(View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.java_adaper_tv);
            simpleDraweeView1 = itemView.findViewById(R.id.java_adaper_image);
            imageView = itemView.findViewById(R.id.java_adaper_image1);
        }
    }
    OnClickJavaAdaper onClickJavaAdaper;
    public interface OnClickJavaAdaper{
        void OnJavaAdaperClick(Bean bean);
    }

    public void setOnClickJavaAdaper(OnClickJavaAdaper onClickJavaAdaper) {
        this.onClickJavaAdaper = onClickJavaAdaper;
    }
}



你可能感兴趣的:((Fragment)XRecyclerView上下拉刷新加载+GreenDao(缓存,查看))