Android RecyclerView 列表

zcck_main.xml页面中包含了表头 列表 翻页

	<include
        android:id="@+id/header"
        layout="@layout/zcck_one_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout3" />
	<androidx.recyclerview.widget.RecyclerView
        android:id="@+id/zc_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/linearLayout4"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/header"
        app:layout_constraintVertical_bias="1.0">

    </androidx.recyclerview.widget.RecyclerView>
     <LinearLayout
        android:id="@+id/linearLayout4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <Button
            android:id="@+id/ruku"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/transparent"
            android:text="@string/prev" />

        <TextView
            android:id="@+id/pageNum"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/page_num" />

        <Button
            android:id="@+id/cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/transparent"
            android:text="@string/next"
            android:textAlignment="center"
            android:textSize="18sp" />

    </LinearLayout>

在这里插入图片描述
zcck_one_title.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="35dp"
    android:background="@drawable/login_button_selector"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/YQBH_CK"
        style="@style/RePage2Header"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginEnd="@dimen/re_2_half_gap"
        android:layout_weight="1"
        android:background="@color/blue_user"
        android:gravity="center"
        android:text="@string/YQBH"
        android:textColor="#FFFFFF"
        android:textSize="12sp" />

    <TextView
        android:id="@+id/ZCMC_CK"
        style="@style/RePage2Header"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginStart="@dimen/re_2_half_gap"
        android:layout_marginEnd="@dimen/re_2_half_gap"
        android:layout_weight="1"
        android:background="@color/blue_user"
        android:gravity="center"
        android:text="@string/ZCMC2"
        android:textColor="#FFFFFF"
        android:textSize="12sp" />

    <TextView
        android:id="@+id/XH_CK"
        style="@style/RePage2Header"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginStart="@dimen/re_2_half_gap"
        android:layout_marginEnd="@dimen/re_2_half_gap"
        android:layout_weight="1"
        android:background="@color/blue_user"
        android:gravity="center"
        android:text="@string/XH2"
        android:textColor="#FFFFFF"
        android:textSize="12sp" />

    <TextView
        android:id="@+id/HJH_CK"
        style="@style/RePage2Header"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginStart="@dimen/re_2_half_gap"
        android:layout_marginEnd="@dimen/re_2_half_gap"
        android:layout_weight="1"
        android:background="@color/blue_user"
        android:gravity="center"
        android:text="@string/HJH"
        android:textColor="#FFFFFF"
        android:textSize="12sp" />

    <TextView
        android:id="@+id/JYZT_CK"
        style="@style/RePage2Header"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginStart="@dimen/re_2_half_gap"
        android:layout_weight="1"
        android:background="@color/blue_user"
        android:gravity="center"
        android:text="@string/JYZT2"
        android:textColor="#FFFFFF"
        android:textSize="12sp" />

</LinearLayout>

zcck_one.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/YQBH_CK"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_marginEnd="@dimen/re_2_half_gap"
        android:layout_weight="1"
        android:gravity="center"
        android:text="@string/YQBH"
        android:textSize="14sp" />

    <TextView
        android:id="@+id/ZCMC_CK"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_marginStart="@dimen/re_2_half_gap"
        android:layout_marginEnd="@dimen/re_2_half_gap"
        android:layout_weight="1"
        android:gravity="center"
        android:text="@string/ZCMC2" />

    <TextView
        android:id="@+id/XH_CK"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_marginStart="@dimen/re_2_half_gap"
        android:layout_marginEnd="@dimen/re_2_half_gap"
        android:layout_weight="1"
        android:gravity="center"
        android:text="@string/XH2" />

    <TextView
        android:id="@+id/HJH_CK"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_marginStart="@dimen/re_2_half_gap"
        android:layout_marginEnd="@dimen/re_2_half_gap"
        android:layout_weight="1"
        android:gravity="center"
        android:text="@string/HJH" />

    <TextView
        android:id="@+id/JYZT_CK"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_marginStart="@dimen/re_2_half_gap"
        android:layout_weight="1"
        android:gravity="center"
        android:text="@string/JYZT2" />

</LinearLayout>

Acticity.java

public class ZcckMainActivity extends BaseActivity implements View.OnClickListener {

    EditText zcbh;
    Context mContext;
    private BroadcastReceiver mReceiver;
    private Dialog mDialog;
    private Dialog mDialog2;

    TextView pageNum;
    Button prev;
    Button next;

    private ZcAdapter zcAdapter = new ZcAdapter();
    private String ckdbh;
    private int pages;
    private int page;
    
    private boolean dialogClose=true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zcck_main);
        mContext = getApplicationContext();
        initView();
        RecyclerView zcView = findViewById(R.id.zc_view);
        zcView.setAdapter(zcAdapter);
        zcView.setLayoutManager(new LinearLayoutManager(ZcckMainActivity.this, RecyclerView.VERTICAL, false));
        zcView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.top = getResources().getDimensionPixelOffset(R.dimen.re_2_gap);
            }
        });

        //查询数据
        getData("1");

    }

    private void initView(){
        prev=(Button) findViewById(R.id.ruku);
        next=(Button) findViewById(R.id.cancel);
        pageNum=(TextView) findViewById(R.id.pageNum);

        prev.setOnClickListener(this);
        next.setOnClickListener(this);

    }

    /**
     * 查询出库单中的相关数据
     * @param currentPage
     */
    private void getData(String currentPage){
        PageData param=new PageData();
        param.put("limit","10");
        param.put("page",currentPage);
        mDialog = com.windoer.warehouse.utils.loadDialogUtils.createLoadingDialog(ZcckMainActivity.this, "loading...");
        addSubscription(httpApi.getZcByCkd(param),new SubscriberCallBack(new ApiCallBack<ResponseWrapper>() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onSuccess(ResponseWrapper resp) {
                if(resp.getCode()!=0){
                    showMsg("请求失败");
                }else{
                    //列表数据
                    zcAdapter.setDataList(resp.getLists());
                    //判断页数
                    pages=resp.getPages();
                    page=resp.getPage();
                    pageNum.setText(page+"/"+pages);
                }
            }

            @Override
            public void onFailure(int code, String msg) {
                showMsg(msg);
                System.out.println(msg);
            }

            @Override
            public void onCompoleted() {
                com.windoer.warehouse.utils.loadDialogUtils.closeDialog(mDialog);
            }
        }));
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ruku:
                //上一页
                if(page==1){
                    //上一页不可点击
                    showMsg("没有上一页数据!");
                }else{
                    //上一页可以点击
                    getData(page-1+"");
                }
                break;
            case R.id.cancel:
                //下一页
                if(page==pages){
                    //下一页不可点击
                    showMsg("没有下一页数据!");
                }else{
                    //下一页可以点击
                    getData(page+1+"");
                }
                break;
            default:
                break;
        }
    }

    /**
     *  RecyclerView 列表
     */
    private class VH extends RecyclerView.ViewHolder {
        TextView YQBH;
        TextView ZCMC;
        TextView XH;
        TextView HJH;
        TextView JYZT;

        public VH(@NonNull View itemView) {
            super(itemView);
            YQBH = itemView.findViewById(R.id.YQBH_CK);
            ZCMC = itemView.findViewById(R.id.ZCMC_CK);
            XH = itemView.findViewById(R.id.XH_CK);
            HJH = itemView.findViewById(R.id.HJH_CK);
            JYZT = itemView.findViewById(R.id.JYZT_CK);
        }
    }

    private class ZcAdapter extends RecyclerView.Adapter<VH> {
        private List<PageData> dataList = new ArrayList<>();

        public ZcAdapter(List<PageData> dataList) {
            this.dataList = dataList;
        }
        public ZcAdapter() {
        }

        public void setDataList(List<PageData> dataList) {
            this.dataList = dataList;
            notifyDataSetChanged();
        }

        public void insertData(PageData data) {
            dataList.add(0, data);
            notifyDataSetChanged();
        }

        @NonNull
        @Override
        public ZcckMainActivity.VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new ZcckMainActivity.VH(LayoutInflater.from(parent.getContext()).inflate(R.layout.zcck_one, parent, false));
        }
        public final int[] colors = new int[] { 0x3000000, 0xFDF0F0F9 };
        @Override
        public void onBindViewHolder(@NonNull ZcckMainActivity.VH holder, int position) {
            PageData c = dataList.get(position);
            holder.YQBH.setText(c.getString("YQBH"));
            holder.ZCMC.setText(c.getString("ZCMC"));
            holder.XH.setText(c.getString("XH"));
            holder.HJH.setText(c.getString("HJH"));
            holder.JYZT.setText(c.getString("JYZT_STR"));
            int colorPos = position % colors.length;
            holder.itemView.setBackgroundColor(colors[colorPos]);
        }

        @Override
        public int getItemCount() {
            return dataList.size();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值