viewpager+fragment联动加载网络数据展示XRecycview

Mainctivity

public class yuekao2 extends AppCompatActivity {
    private XRecyclerView xRecyclerView;
    private Context context;
    private int curr;
    private MyAdapter adapter;
    private List<Bean> beanList=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yuekao2);
//标题
        setTitle("首页");
        context=this;
        xRecyclerView=findViewById(R.id.recyclerview);
//布局管理器
        LinearLayoutManager layoutManager=new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        xRecyclerView.setLayoutManager(layoutManager);
//上拉刷新 下拉加载
        xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                curr=0;
                beanList.clear();
                getData(curr);
                xRecyclerView.refreshComplete();
            }


            @Override
            public void onLoadMore() {
                curr=curr+10;
                getData(curr);
                xRecyclerView.refreshComplete();
            }
        });
        curr=0;
        getData(curr);


    }
//获取数据
    private void getData(int number) {
        for (int i=number;i<number+10;i++){
            RequestParams requestParams=new RequestParams("https://zhuanlan.zhihu.com//api/columns/growthhacker/posts?limit=10&offset="+i);
            x.http().get(requestParams, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {


                    Gson gson=new Gson();
                    List<Bean> beans=gson.fromJson(result,new TypeToken<List<Bean>>(){}.getType());


                    beanList.addAll(beans);


                    adapter=new MyAdapter(yuekao2.this,beanList);
                    xRecyclerView.setAdapter(adapter);
                    adapter.notifyDataSetChanged();


                    showNormalDialog();
//                    Bean bean=gson.fromJson(result,Bean.class);
//                    beanList.add(bean);
//
//                    MyAdapter myAdapter=new MyAdapter(getContext)


                }




                @Override
                public void onError(Throwable ex, boolean isOnCallback) {


                }


                @Override
                public void onCancelled(CancelledException cex) {


                }


                @Override
                public void onFinished() {


                }
            });
        }
    }
//弹框
    private void showNormalDialog() {
        final AlertDialog.Builder nor=new AlertDialog.Builder(yuekao2.this);
        nor.setTitle("请求数据成功");
        nor.setMessage("请求数据成功");
        nor.setPositiveButton("en", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {


            }
        });
        nor.setNegativeButton("en", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {


            }
        });


        nor.show();
    }
//菜单
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
//        getMenuInflater().inflate(R.menu.yue2menu,menu);
        menu.add(1,1,1,"关于");
        menu.add(1,2,2,"分享");
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case 1:


                Intent intent=new Intent(this,MainActivity2.class);
                startActivity(intent);
                break;
            case 2:
                break;
        }


        return true;
    }
}





MyAdapter 

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.Holder> {
    Context context;
    private List<Bean> beans;


    public MyAdapter(Context context, List<Bean> beans) {
        this.context = context;
        this.beans = beans;
    }


    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.item,parent,false);
        Holder holder=new Holder(view);
        return holder;
    }


    @Override
    public void onBindViewHolder(Holder holder, int position) {
        Bean bean=beans.get(position);
        holder.text_bt.setText(bean.getTitle());
        Log.e("www",bean.getTitle());
//        holder.text_id.setText(bean.getLikesCount());
    }


    @Override
    public int getItemCount() {
        Log.e("www",""+beans.size());
        return beans.size();
    }


    public class Holder extends RecyclerView.ViewHolder {


        private TextView text_bt;
        private TextView text_id;
        public Holder(View itemView) {
            super(itemView);


            text_bt=itemView.findViewById(R.id.text_bt);
            text_id=itemView.findViewById(R.id.text_id);
        }
    }
}



activity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context="com.example.myapplication.yuekao2">

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


    </com.jcodecraeer.xrecyclerview.XRecyclerView>
</LinearLayout>



item

猜你喜欢

转载自blog.csdn.net/qq_41326326/article/details/79955726
今日推荐