zoukankan      html  css  js  c++  java
  • 团队冲刺--第一阶段(八)

    一、前言

      昨天实现了记住用户登录状态的功能。

      由于后台功能方面已经可以说是彻底完工了,所以我又继续转为对UI页面优化。今天实现了对CardView赋予随机背景色,自定义了头部标题栏。遇到的困难是开始没有理清adapter的思路。

      明天继续美化页面。

    二、成果展示

     三、代码

    RecycleViewBaseAdapter.java

    package com.androidlearing.tdtreehole.adapter;
    
    import android.graphics.Color;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    import androidx.recyclerview.widget.RecyclerView;
    
    
    import com.androidlearing.tdtreehole.R;
    import com.androidlearing.tdtreehole.bean.ItemBean;
    
    import java.util.List;
    import java.util.Random;
    
    /**
     * @ProjectName: RecyclerViewDemo
     * @Package: com.androidlearing.recyclerviewdemo.adapter
     * @ClassName: RecyclerViewBaseAdapter
     * @Description: java类作用描述
     * @Author: 武神酱丶
     * @CreateDate: 2020/3/29 21:46
     * @UpdateUser: 更新者
     * @UpdateDate: 2020/3/29 21:46
     * @UpdateRemark: 更新说明
     * @Version: 1.0
     */
    public abstract class RecyclerViewBaseAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
        protected  static List<ItemBean> mData;
        private onItemClickListener mOnItemClickListener;
    
    
        public RecyclerViewBaseAdapter(List<ItemBean> data)
        {
            this.mData = data;
        }
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = getSubView(parent,viewType);
            return new InnerHolder(view);
        }
    
        protected abstract View getSubView(ViewGroup parent, int viewType);
    
        /**
         * 这个方法是用于绑定holder的,一般用来设置数据
         * @param holder
         * @param position
         */
        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            //在这里设置数据
            ((InnerHolder) holder).setData(mData.get(position),position);
        }
    
        /**
         * 返回条目的个数
         * @return
         */
        @Override
        public int getItemCount() {
            if(mData!=null){
                return mData.size();
            }
            return 0;
        }
    
        public void setOnItemClickListener(onItemClickListener Listener) {
            //设置一个监听,其实就是要设置一个接口,一个回调的接口
            this.mOnItemClickListener = Listener;
        }
    
        /**
         * 编写回调的步骤
         * 1.创建这个接口
         * 2.定义接口内部的方法
         * 3.提供设置接口的方法(外部实现)
         * 4.接口调用
         */
        public interface onItemClickListener{
            void onItemClick(int position);
        }
    
        //获取随机颜色
        public static int getRandColor(){
            int R=0,G=0,B=0;
            Random random = new Random();
            for (int i = 0;i<2;i++){
                int temp = random.nextInt(16);
                R = R*16+temp;
                temp = random.nextInt(16);
                G = G*16+temp;
                temp = random.nextInt(16);
                B = B*16+temp;
            }
            //设置透明度为60%
            return Color.argb(60,R,G,B);
        }
    
        public class InnerHolder extends RecyclerView.ViewHolder {
            private final TextView mTitle;
            private final TextView mContent;
            private int mPosition;
            private final TextView mPostTime;
    
            public InnerHolder( View itemView) {
                super(itemView);
                //给cardview随机设置背景颜色
                itemView.findViewById(R.id.cv_post).setBackgroundColor(getRandColor());
                //找到条目控件
                mTitle = itemView.findViewById(R.id.title_tv);
                mContent = itemView.findViewById(R.id.content_tv);
                mPostTime = itemView.findViewById(R.id.post_time_tv);
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mOnItemClickListener!=null) {
                            mOnItemClickListener.onItemClick(mPosition);
                        }
                    }
                });
    
            }
    
            /**
             * 这个方法用于设置数据
             * @param itemBean
             */
            public void setData(ItemBean itemBean,int position) {
                this.mPosition = position;
                //开始设置数据
                mTitle.setText(itemBean.getTitle());
                mContent.setText(itemBean.getContent());
                mPostTime.setText(itemBean.getPosttime());
    
            }
        }
    }
    View Code

    item_post_index.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto">
        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:cardUseCompatPadding="true"
            app:cardPreventCornerOverlap="false"
            app:cardCornerRadius="10dp"
            app:contentPadding="0dp">
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/cv_post">
                <RelativeLayout
                    android:id="@+id/relative"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                <TextView
                    android:id="@+id/title_tv"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="我是标题"
                    android:textSize="15sp"
                    />
                    <TextView
                        android:layout_alignParentRight="true"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/post_time_tv"
                        android:text="我是发布日期"
                        android:textSize="15sp"
                        />
                </RelativeLayout>
                <TextView
                    android:id="@+id/content_tv"
                    android:layout_centerHorizontal="true"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/relative"
                    android:textSize="20sp"
                    android:text="我是内容"
                    android:textColor="#000000"
                    />
            </RelativeLayout>
        </androidx.cardview.widget.CardView>
    </RelativeLayout>
    View Code

    四、今日团队链接

    https://www.cnblogs.com/three3/p/12770450.html

  • 相关阅读:
    设计模式——代理模式
    设计模式——建造者模式
    设计模式——模板方法
    springboot+mybatis项目自动生成
    【小坑】java下载excel文件
    设计模式——工厂方法模式 和 抽象工厂模式
    设计模式——单例模式
    容易忽略的递归当中的return
    Android 4.0以后正确的获取外部sd卡存储目录
    Android 串口设置校验位、速率、停止位等参数
  • 原文地址:https://www.cnblogs.com/xhj1074376195/p/12770460.html
Copyright © 2011-2022 走看看