吸顶效果

导包
implementation ‘com.gavin.com.library:stickyDecoration:1.1.0’
MainActivity

public class MainActivity extends AppCompatActivity {

    private RecyclerView recy;
   List list=new ArrayList<>();
    private PowerfulStickyDecoration build;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        recy = (RecyclerView) findViewById(R.id.recy);
        //这个是从类里那数据
        list.addAll(FoodUtils.getFoodList());
        list.addAll(FoodUtils.getFoodList());
        recy.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        //主要代码
        build = PowerfulStickyDecoration.Builder
                .init(new PowerGroupListener() {
                    @Override
                    public String getGroupName(int position) {
                        if (list.size() > position) {
                            return list.get(position).getFoodName();
                        }
                        return null;
                    }

                    @Override
                    public View getGroupView(int position) {
                        if (list.size()>position){
                            View inflate = getLayoutInflater().inflate(R.layout.city_group, null);
                            TextView textView = inflate.findViewById(R.id.tv);
                            textView.setText(list.get(position).getFoodName());
                            return inflate;
                        }else{
                            return null;
                        }

                    }
                })
                .setGroupHeight(DensityUtil.dip2px(MainActivity.this,40))
                .build();
        //
        recy.addItemDecoration(build);
        SimpleAdapter simpleAdapter = new SimpleAdapter(MainActivity.this, list);
        recy.setAdapter(simpleAdapter);
    }
}

Adapter

public class SimpleAdapter extends RecyclerView.Adapter {
    private List mCities = new ArrayList<>();
    private Context mContext;

    public SimpleAdapter(Context context, List cities) {
        mCities.addAll(cities);
        mContext = context;
    }

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

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        ViewHolder holder = (ViewHolder) viewHolder;
        int i = position % 5 + 1;
        if (i == 1) {
               holder.ll_bg.setBackgroundColor(mContext.getResources().getColor(android.R.color.holo_red_dark));

        } else if (i == 2) {
            holder.ll_bg.setBackgroundColor(mContext.getResources().getColor(android.R.color.darker_gray));

        } else if (i == 3) {
            holder.ll_bg.setBackgroundColor(mContext.getResources().getColor(android.R.color.holo_blue_bright));

        } else if (i == 4) {
            holder.ll_bg.setBackgroundColor(mContext.getResources().getColor(android.R.color.holo_green_dark));

        } else {
            holder.ll_bg.setBackgroundColor(mContext.getResources().getColor(android.R.color.holo_orange_dark));

        }
        holder.tv_city.setText(mCities.get(position).getName());
    }

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


    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv_brief,tv_city;
        LinearLayout ll_bg;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_city=itemView.findViewById(R.id.tv_city);
            tv_brief=itemView.findViewById(R.id.tv_brief);
            ll_bg=itemView.findViewById(R.id.ll_bg);
        }
    }
}

Utils

public class DensityUtil {

    /**
     * 获得屏幕宽度
     *
     * @param context
     * @return
     */
    public static int getScreenWidth(Context context) {
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.widthPixels;
    }

    /**
     * 获得屏幕宽度
     *
     * @param context
     * @return
     */
    public static int getScreenHeight(Context context) {
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.heightPixels;
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    // 将px值转换为sp值,保证文字大小不变
    public static int px2sp(Context context, float pxValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }

    // 将sp值转换为px值,保证文字大小不变
    public static int sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }
}

你可能感兴趣的:(记录)