使用recycleview实现简单的柱状图

来源:互联网 发布:web 数据库 编辑:程序博客网 时间:2024/05/21 02:53

在item中展示条目,也就是柱状图

@Overridepublic void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {    if (holder instanceof MyHolder) {        MyItemBean listBean = informa.get(position);        int i = 0;        if (TextUtils.equals(maxValue, "500") || TextUtils.equals(maxValue, ((int) kaluliMaxValue) + "")) {            if (TextUtils.equals(maxValue, "500")){                 i = (int) (Float.parseFloat(listBean.caluli) / Integer.parseInt(maxValue) * maxHight);            }else{                 i = (int) (Float.parseFloat(listBean.caluli) / kaluliMaxValue * maxHight);            }            ((MyHolder) holder).mNumber.setText(spiltPoint(listBean.caluli));            ((MyHolder) holder).mNumber.setTextColor(UIUtil.getColorStateList(R.color.red));            ((MyHolder) holder).zhuzi.setBackground(UIUtil.getDrawable(R.drawable.recatan_red));            Log.v(TAG, "listBean.distance==============" + listBean.caluli);        } else if (TextUtils.equals(maxValue, ((int) ditanceMaxValue) + "")||TextUtils.equals(maxValue, "50")) {            if (TextUtils.equals(maxValue, "50")) {                i = (int) (Float.parseFloat(listBean.distance) / 100 / 50 * maxHight);            }else{                i = (int) (Float.parseFloat(listBean.distance) / 100 / ditanceMaxValue * maxHight);            }            ((MyHolder) holder).mNumber.setTextColor(UIUtil.getColorStateList(R.color.yellow));            ((MyHolder) holder).zhuzi.setBackground(UIUtil.getDrawable(R.drawable.recatan_yellow));            ((MyHolder) holder).mNumber.setText(Float.parseFloat(listBean.distance) / 100 + "");            Log.v(TAG, "listBean.distance==============" + listBean.distance);        } else if (TextUtils.equals(maxValue, ((int) timeMaxValue) + "")||TextUtils.equals(maxValue, "100")) {            if (TextUtils.equals(maxValue, ((int) timeMaxValue) + "")) {                i = (int) ((listBean.totalSecond + 0.1f) / 60 / timeMaxValue * maxHight);            }else{                i = (int) ((listBean.totalSecond + 0.1f) / 60 / 100 * maxHight);            }            ((MyHolder) holder).mNumber.setText(getSecond(listBean.totalSecond));            ((MyHolder) holder).zhuzi.setBackground(UIUtil.getDrawable(R.drawable.recatan_blue));            ((MyHolder) holder).mNumber.setTextColor(UIUtil.getColorStateList(R.color.blue));            Log.v(TAG, "getSecond(listBean.totalSecond)=====" + getSecond(listBean.totalSecond));            Log.v(TAG, "listBean.motionHour==============" + listBean.motionHour);            Log.v(TAG, "listBean.totalSecond==============" + listBean.totalSecond);        }        Log.v(TAG, "i==============" + i);        Log.v(TAG, "maxHight==============" + maxHight);        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) ((MyHolder) holder).zhuzi.getLayoutParams();        params.height = i;        ((MyHolder) holder).zhuzi.setLayoutParams(params);        ((MyHolder) holder).mDay.setText(listBean.numberName);    }}
中间空心的条目是条目类型返回的是-1,有实体的是返回默认的数字

@Overridepublic int getItemViewType(int position) {    if (position % 2 == 0) {        return super.getItemViewType(position);    } else {        return -1;    }}
在这里进行分解,选中不同的holder

@Overridepublic RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {    if (viewType == -1) {        View inflate = View.inflate(getActivity(), R.layout.recycle_item1, null);        MyHolder1 holder = new MyHolder1(inflate);        return holder;    } else {        View inflate = View.inflate(getActivity(), R.layout.recycle_item, null);        MyHolder holder = new MyHolder(inflate);        return holder;    }}
在布局中,有两个layout,一个是有控件的,一个是没有带任何控件的

<?xml version="1.0" encoding="utf-8"?><RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="@dimen/dp50"    android:layout_height="match_parent"    android:orientation="vertical">    <LinearLayout        android:layout_width="@dimen/dp50"        android:orientation="vertical"        android:layout_height="match_parent">        <RelativeLayout            android:background="@color/grey_back_tran"            android:id="@+id/gaodu"            android:layout_width="@dimen/dp50"            android:layout_height="0dp"            android:layout_weight="0.9"            android:orientation="vertical">            <ImageView                android:layout_alignParentBottom="true"                android:id="@+id/zhuzi"                android:background="@drawable/recatan_red"                android:layout_width="@dimen/dp50"                android:layout_height="wrap_content"/>            <TextView                android:textSize="@dimen/sp10"                android:textColor="#d20037"                android:layout_above="@+id/zhuzi"                android:id="@+id/number"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:gravity="center_horizontal"                android:text=" "/>        </RelativeLayout>        <RelativeLayout            android:layout_width="match_parent"            android:layout_height="0dp"            android:layout_weight="0.1">            <TextView                android:layout_marginLeft="@dimen/dp5"                android:layout_centerHorizontal="true"                android:layout_marginTop="@dimen/dp5"                android:id="@+id/day"                android:textSize="@dimen/sp10"                android:layout_width="match_parent"                android:layout_height="wrap_content"                />        </RelativeLayout>    </LinearLayout></RelativeLayout>

<?xml version="1.0" encoding="utf-8"?><RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="@dimen/dp90"    android:background="#22ffffff"    android:layout_height="match_parent"    >    <ImageView        android:layout_width="@dimen/dp90"        android:layout_height="match_parent"/></RelativeLayout>
柱状图的高度比例根据屏幕的高度然后*
float density = getResources().getDisplayMetrics().density;

0 0
原创粉丝点击