Android view的预加载占位效果封装使用

作为UI的基础功能组件,如果不进行封装后再使用,会出现大量重复代码,所以我封装了。

1.静态效果图展示
2.代码调用
3.自定义动画效果
4.代码调用
5.在RecyclerView使用
6.代码调用
7.钉钉考勤-圆形和方形的效果
8.代码调用
9.封装代码BroccoliManager.java
10.总结

1.静态效果图展示

image

2.代码调用

    private void initPlaceholders(){
        BroccoliManager.initStatic(this, R.id.tv_view_time, R.id.tv_collect_time,
                R.id.tv_price, R.id.iv_clock, R.id.tv_time, R.id.iv_calendar, R.id.iv_location,
                R.id.tv_location, R.id.iv_arrow_right, R.id.iv_logo, R.id.tv_organizer_name, R.id.tv_organizer_description,
                R.id.tv_fans, R.id.tv_fans_number, R.id.tv_events, R.id.tv_events_number, R.id.tv_follow, R.id.tv_station);
        BroccoliManager.show();
        showPlaceholders();
    }

    private void showPlaceholders(){
        mHandler.removeCallbacks(task);
        mHandler.postDelayed(task,2000);
    }

    private Runnable task = new Runnable() {
        @Override
        public void run() {
            showData();
        }
    };

    private void showData() {
        BroccoliManager.staticClear();
    }

3.自定义动画效果

image

4.代码调用

    private void initPlaceholders() {
        BroccoliManager.initAction(
                tvViewTimes,
                tvCollectTimes,
                tvPrice,
                ivClock,
                tvTime,
                ivCalendar,
                ivLocation,
                tvLocation,
                ivRightArrow,
                ivLogo,
                tvOrganizer,
                tvOrganizerDescription,
                tvFans,
                tvFansNumber,
                tvEvents,
                tvEventsNumber,
                tvFollow,
                tvStation
        );
        BroccoliManager.show();
        showPlaceholders();
    }

    private void showPlaceholders() {
        mHandler.removeCallbacks(task);
        mHandler.postDelayed(task, 3000);
    }

    private Runnable task = new Runnable() {
        @Override
        public void run() {
            showData();
        }
    };

    private void showData() {
        BroccoliManager.staticClear();
    }

5.在RecyclerView使用

image

6.代码调用


    private class MyAdapter extends RecyclerView.Adapter {

        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View view = getLayoutInflater().inflate(R.layout.recyclerview_sample_item, viewGroup, false);
            return new MyViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull final MyViewHolder myViewHolder, final int i) {
            BroccoliManager.initRecyclerView(myViewHolder.itemView
                    , myViewHolder.tvTitle
                    , myViewHolder.imageView
                    , myViewHolder.tvPrice
                    , myViewHolder.tvDescription);


            //delay to show the data
            Runnable task = mTaskManager.get(myViewHolder.itemView);
            if (task == null) {

                task = new Runnable() {
                    @Override
                    public void run() {
                        //when you need to update data, you must to call the remove or the clear method.
                        BroccoliManager.byItemViewClear(myViewHolder.itemView);

                        if (mDataList.isEmpty()) {
                            return;
                        }

                        myViewHolder.imageView.setImageResource(mDataList.get(i).imageRes);
                        myViewHolder.tvPrice.setText("¥ " + String.valueOf(mDataList.get(i).price));
                        myViewHolder.tvTitle.setText(mDataList.get(i).title);
                        myViewHolder.tvDescription.setText(mDataList.get(i).description);
                    }
                };
                mTaskManager.put(myViewHolder.itemView, task);
            } else {
                myViewHolder.itemView.removeCallbacks(task);
            }
            myViewHolder.itemView.postDelayed(task, 3000);
        }

        @Override
        public int getItemCount() {
            return mDataList.size();
        }
    }

    private class MyViewHolder extends RecyclerView.ViewHolder {
        private ImageView imageView;
        private TextView tvTitle;
        private TextView tvDescription;
        private TextView tvPrice;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);

            imageView = itemView.findViewById(R.id.iv_image);
            tvTitle = itemView.findViewById(R.id.tv_title);
            tvDescription = itemView.findViewById(R.id.tv_description);
            tvPrice = itemView.findViewById(R.id.tv_price);
        }
    }

7.钉钉考勤-圆形和方形的效果

image
image

8.代码调用


    private void initPlaceholders() {
        BroccoliManager.staticSquare(
                 findViewById(R.id.tv_name),
                 findViewById(R.id.tv_description),
                 findViewById(R.id.tv_day_des),
                 findViewById(R.id.v_shangban),
                 findViewById(R.id.v_xiaban),
                 findViewById(R.id.v_timeline),
                 findViewById(R.id.tv_shangban),
                 findViewById(R.id.tv_xiaban),
                 findViewById(R.id.tv_daka_des));
        BroccoliManager.staticCircular(
                findViewById(R.id.iv_head),
                findViewById(R.id.tv_daka) );
        BroccoliManager.show();


        showPlaceholders();
    }

    private void showPlaceholders() {
        mHandler.removeCallbacks(task);
        mHandler.postDelayed(task, 2000);
    }

    private Runnable task = new Runnable() {
        @Override
        public void run() {
            BroccoliManager.staticClear();
        }
    };

9.封装代码BroccoliManager.java


/**
 * 占位符管理类
 */
public class BroccoliManager {
    private static Broccoli mBroccoli;
    // 给Recycler的item用的。
    private static Map mViewPlaceholderManager = new HashMap<>();

    /**
     * 静态的占位符
     *
     * @param context 上下文 activity
     * @param views   R.id.控件的id
     */
    public static void initStatic(Activity context, int... views) {
        if (mBroccoli == null) {
            mBroccoli = new Broccoli();
        }
        for (int view : views) {
            mBroccoli.addPlaceholders(context, view);
        }
    }

    /**
     * 动态占位符
     *
     * @param views 已绑定好的view
     */
    public static void initAction(View... views) {
        mBroccoli = new Broccoli();
        for (View view : views) {
            mBroccoli.addPlaceholders(PlaceholderHelper.getParameter(view));
        }

    }

    /**
     * RecyclerView的动态占位符
     *
     * @param itemView recyclerView整项内容的View
     * @param views    里面各小项内容的view
     */
    public static void initRecyclerView(View itemView, View... views) {
        Broccoli broccoli = mViewPlaceholderManager.get(itemView);
        if (broccoli == null) {
            broccoli = new Broccoli();
            mViewPlaceholderManager.put(itemView, broccoli);
        }
        for (View view : views) {
            broccoli.addPlaceholder(new PlaceholderParameter.Builder()
                    .setView(view)
                    .setDrawable(new BroccoliGradientDrawable(Color.parseColor("#DDDDDD"),
                            Color.parseColor("#CCCCCC"), 0, 1000, new LinearInterpolator()))
                    .build());
        }
        broccoli.show();
    }

    /**
     * 根据大项itemView来清除占位符
     *
     * @param itemView
     */
    public static void byItemViewClear(View itemView) {
        if (itemView != null) {
            Broccoli broccoli = mViewPlaceholderManager.get(itemView);
            broccoli.removeAllPlaceholders();
        }
    }

    /**
     * 当用recyclerView时在onDestroy时将mViewPlaceholderManager清空
     * 防止使用BroccoliGradientDrawable时内存泄露
     */
    public static void recyclerViewOnDestroy() {
        //Prevent memory leaks when using BroccoliGradientDrawable
        //防止使用BroccoliGradientDrawable时内存泄露
        for (Broccoli broccoli : mViewPlaceholderManager.values()) {
            broccoli.removeAllPlaceholders();
        }

        mViewPlaceholderManager.clear();
    }

    /**
     * 清除静态占位符
     */
    public static void staticClear() {
        if (mBroccoli != null) {
            mBroccoli.removeAllPlaceholders();
        }
    }

    /**
     * 圆形占位符
     *
     * @param views 可以多个圆形view 进行处理
     */
    public static void staticSquare(View... views) {
        int placeholderColor = Color.parseColor("#DDDDDD");
        if (mBroccoli == null) {
            mBroccoli = new Broccoli();
        }
        for (View view : views) {
            mBroccoli.addPlaceholders(
                    new PlaceholderParameter.Builder()
                            .setView(view)
                            .setDrawable(DrawableUtils.createRectangleDrawable(placeholderColor, 0))
                            .build());
        }

    }
    public static void show(){
        mBroccoli.show();
    }

    /**
     * 圆形占位符
     *
     * @param views 可以多个圆形view 进行处理
     */
    public static void staticCircular(View... views) {
        int placeholderColor = Color.parseColor("#DDDDDD");
        if (mBroccoli == null) {
            mBroccoli = new Broccoli();
        }
        for (View view : views) {
            mBroccoli.addPlaceholders(
                    new PlaceholderParameter.Builder()
                            .setView(view)
                            .setDrawable(DrawableUtils.createOvalDrawable(placeholderColor))
                            .build());
        }
    }

}

10.总结

我是参考Android view的预加载占位效果封装使用
的文章进行更改的。
我的源码:https://pan.baidu.com/s/1rzMmcXAqFmSBL1I8O6ZHpg
提取码:w9a1

依赖

Gradle

将其添加到你的module的build.gradle中:

dependencies {
    implementation 'me.samlss:broccoli:1.0.0'
}

Maven


  me.samlss
  broccoli
  1.0.0
  pom


Github地址

你可能感兴趣的:(Android view的预加载占位效果封装使用)