当前位置: 首页 > 知识库问答 >
问题:

RecycerView中的MP折线图

徐星阑
2023-03-14

在回收器视图中,我试图实现MP折线图。我有一个rest api,用于获取数据并将其保存在HashMap中。现在我想根据我的API响应来设置数据。请协助我在一个列表项目中创建一个线图。

 public class MarketWatchSymbolAdapter extends  RecyclerView.Adapter<MarketWatchSymbolAdapter.ViewHolder> {

    private Symbol[] marketSymbol;
    public  Context context;
    private ArrayList<String> rows = new ArrayList<String>();
    private ArrayList<MarketFeedBean> arrList;
    private Map<String, MarketFeedBean> watchList;
    private List<String> keys = new ArrayList<>();
    private Double changePercentage;
    private static LineDataSet set1;
    public static Drawable drawablePositive = ContextCompat.getDrawable(Logs.globalContext, R.drawable.graph_green_bg);
    public static Drawable drawable_negative = ContextCompat.getDrawable(Logs.globalContext, R.drawable.graph_bg);





    public MarketWatchSymbolAdapter(Symbol[] marketSymbol, Context context) {
        this.marketSymbol = marketSymbol;
        this.context = context;

        notifyDataSetChanged();

    }




    public MarketWatchSymbolAdapter(Context context, ArrayList<MarketFeedBean> arrList) {
        this.context = context;
        this.arrList = arrList;
    }

    public MarketWatchSymbolAdapter(Context ctx, Map<String, MarketFeedBean> watchList) {
        this.watchList = watchList;
        this.context = ctx;

        keys.addAll(watchList.keySet());
    }

    public MarketWatchSymbolAdapter(Context context) {
        this.context = context;

    }

    @NonNull
    @Override
    public MarketWatchSymbolAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View listItem= layoutInflater.inflate(R.layout.marketwatch_list_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(listItem);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MarketWatchSymbolAdapter.ViewHolder holder, int position) {


        MarketFeedBean marketFeedBean = Logs.marketFeedBeanMap.get(rows.get(position));




        if(marketFeedBean !=null) {
            changePercentage = marketFeedBean.getChange() / (marketFeedBean.getLastTradePrice() - marketFeedBean.getChange()) * 100;

            holder.indexName.setText(marketFeedBean.getMarket());
            holder.scripName.setText(marketFeedBean.getScrip());
            holder.SymbolName.setText(marketFeedBean.getScripName());
            holder.indexValue.setText(Logs.priceFormat.format(marketFeedBean.getLastTradePrice()).replace("-", ""));
         

            if(marketFeedBean.getChange() != 0.0) {
                if (Logs.priceFormat.format(changePercentage).contains("-")) {

                    holder.indexChange.setBackgroundColor(context.getResources().getColor(R.color.red));
                    set1.setDrawFilled(true);
                    set1.setFillColor(Color.RED);
                    set1.setFillDrawable(drawable_negative);
                    set1.setHighLightColor(Color.RED);
                    set1.setColor(Color.RED);

                }
                else
                {

                    holder.indexChange.setBackgroundColor(context.getResources().getColor(R.color.green));
                    set1.setDrawFilled(true);
                    set1.setFillColor(Color.GREEN);
                    set1.setFillDrawable(drawablePositive);
                    set1.setHighLightColor(Color.GREEN);
                    set1.setColor(Color.GREEN);


                }
                holder.indexChange.setText("(" + Logs.priceFormat.format(changePercentage).replace("-", "") + "%)");
            }

            else
            {
                holder.indexChange.setText("(0.00%)");
                set1.setDrawFilled(true);
                set1.setFillColor(Color.GREEN);
                set1.setFillDrawable(drawablePositive);
                set1.setHighLightColor(Color.GREEN);
                set1.setColor(Color.GREEN);
            }


        }
        else {

            String sm = rows.get(position);
            holder.indexName.setText(sm.split("\\:", -1)[1]);
            holder.scripName.setText(sm.split("\\:", -1)[0]);
            holder.SymbolName.setText("");
            holder.indexValue.setText("0.00");
            holder.indexChange.setText("(0.00%)");


        }


    }


    @Override
    public int getItemCount() {

        if(rows == null){
            return  0;
        }

        else {
            return rows.size();
        }
    }


    public synchronized void refill(ArrayList<String> data) {
        this.rows.clear();
        this.rows.addAll(data);
        notifyDataSetChanged();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView indexName;
        public TextView SymbolName;
        public TextView indexValue;
        public AutoResizeTextView scripName;
        public LineChart symbolChart;
        public ImageButton imageButton;
        private Button indexChange;
        public CardView cardView;
        public ArrayList<Entry> values;
        public LineData data;
        public Context context;
        public  String Scrip;
        public  String market;




        public ViewHolder(View itemView) {
            super(itemView);



            context = itemView.getContext();
            indexName = itemView.findViewById(R.id.symbolMarket);
            scripName = itemView.findViewById(R.id.mainSymbolName);
            SymbolName = itemView.findViewById(R.id.symbolName);
            indexValue = itemView.findViewById(R.id.indexValue);
            indexChange = itemView.findViewById(R.id.indexChange);
            imageButton = itemView.findViewById(R.id.homeCross);
            cardView = itemView.findViewById(R.id.crdlayout);
            symbolChart = itemView.findViewById(R.id.marketwatchgraph);
            setData(10,3f);
            customiseChart();





            cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AppConfig.mktWatchlistIndex = getAdapterPosition();
                    Intent intent = new Intent(context, OrderActivity.class);
                    market = indexName.getText().toString();
                    Scrip = scripName.getText().toString();
                    intent.putExtra("scripName", Scrip);
                    intent.putExtra("marketName", market);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent);

                }
            });

            imageButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AppConfig.mktWatchlistIndex = getAdapterPosition();
                    onCrossClick();

                }
            });


        }

        private void customiseChart()
        {
            symbolChart.getDescription().setEnabled(false);
            symbolChart.setDrawGridBackground(false);
            symbolChart.setDragEnabled(false);
            symbolChart.getLegend().setEnabled(false);
            symbolChart.setScaleEnabled(true);
            symbolChart.setScaleYEnabled(false);
            symbolChart.setScaleXEnabled(true);
            symbolChart.setDrawGridBackground(false);
            symbolChart.getXAxis().setEnabled(false);
            symbolChart.getLineData().setDrawValues(false);
            symbolChart.getXAxis().setDrawGridLines(false);
            symbolChart.getXAxis().setDrawAxisLine(false);
            symbolChart.getAxisLeft().setDrawGridLines(false);
            symbolChart.getAxisRight().setDrawGridLines(false);
            symbolChart.getAxisRight().setDrawZeroLine(true);
            symbolChart.getAxisLeft().setDrawZeroLine(true);
            symbolChart.getAxisRight().setDrawLabels(false);
            symbolChart.getAxisLeft().setDrawLabels(false);
            symbolChart.getAxisLeft().setEnabled(false);
            symbolChart.getAxisRight().setEnabled(true);
            symbolChart.getAxisRight().setZeroLineColor(Color.BLACK);
            symbolChart.getAxisRight().setAxisLineColor(Color.BLACK);
            symbolChart.setMaxHighlightDistance(150);
            symbolChart.setViewPortOffsets(0, 0, 0, 0);
            symbolChart.setTouchEnabled(false);
            symbolChart.setPinchZoom(false);
        }

        public void onCrossClick(){
            MarketWatchFragment.getInstance().onRemoveItem();

        }

        private void setData(int count, float range) {

            values = new ArrayList<>();

            values.add(new Entry(1,10f));
            values.add(new Entry(2,12f));
            values.add(new Entry(3,8f));
            values.add(new Entry(4,12f));
            values.add(new Entry(5,3f));
            values.add(new Entry(6,4f));
            values.add(new Entry(7,4f));
            values.add(new Entry(8,5f));
            values.add(new Entry(9,2f));
            values.add(new Entry(10,3f));
            values.add(new Entry(11,9f));
            values.add(new Entry(12,4f));
            values.add(new Entry(13,7f));
            values.add(new Entry(14,10));



            set1 = new LineDataSet(values, "DataSet 1");
            set1.setDrawCircles(false);
            set1.setDrawFilled(true);
            set1.setLineWidth(1f);
            set1.setColor(Color.GREEN);
            set1.setMode(LineDataSet.Mode.CUBIC_BEZIER);
            set1.setDrawFilled(true);
            set1.setFillDrawable(drawablePositive);
            set1.setFillFormatter(new IFillFormatter() {
                @Override
                public float getFillLinePosition(ILineDataSet dataSet, LineDataProvider dataProvider) {
                    return symbolChart.getAxisLeft().getAxisMinimum();

                }
            });
            ArrayList<ILineDataSet> dataSets = new ArrayList<>();
            dataSets.add(set1);
            data = new LineData(dataSets);

            symbolChart.setData(data);


        }


    }
}






<androidx.cardview.widget.CardView xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/crdlayout"
    android:background="@color/cardview_light_background"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:layout_margin="5dp"
            android:id="@+id/symbolName"
            android:layout_width="250dp"
            android:textSize="9sp"
            android:layout_height="wrap_content"
            android:text="LOTCHEM"
            android:textColor="@color/black"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/listlayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="3">


        <TextView
            android:id="@+id/symbolMarket"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="10sp"
            android:layout_weight="0.35"
            android:gravity="center"
            android:singleLine="true"
            android:text=""
            android:textSize="15dp"
            android:textStyle="bold" />

        <com.catalyst.maksl.Components.AutoResizeTextView
            android:id="@+id/mainSymbolName"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="2dp"
            android:layout_marginLeft="2dp"
            android:layout_weight="0.65"
            android:gravity="left"
            android:singleLine="true"
            android:text=""
            android:textSize="15dp"
            />



        <com.github.mikephil.charting.charts.LineChart
            android:id="@+id/marketwatchgraph"
            android:layout_width="0dp"
            android:layout_height="20dp"
            android:layout_gravity="center_vertical"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:layoutDirection="locale" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:orientation="vertical"
            android:weightSum="3">

            <TextView
                android:id="@+id/indexValue"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:gravity="bottom"
                android:text="456,99"
                android:textStyle="bold"

                />

            <Button
                android:id="@+id/indexChange"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_gravity="center"
                android:layout_weight="2"
                android:background="@color/green"
                android:clickable="false"
                android:shadowRadius="100"
                android:text="+15.22%"
                android:textColor="@color/white" />

        </LinearLayout>


        <ImageButton
            android:id="@+id/homeCross"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:src="@drawable/remove"
            android:clickable="true"
            android:background="@android:color/transparent"
            android:focusable="true"
            />


    </LinearLayout>

共有1个答案

谭桐
2023-03-14

对mpchart尝试此替代方案

易于使用

 类似资料:
  • 我正在使用库MPAndroid,< code >编译' com . github . philjay:MPAndroidChart:v 3 . 0 . 0-beta 1 ' 我必须在MPAndroid折线图中传递x轴上的日期和y轴上的值,当我传递x轴或y轴上的值时,应用程序崩溃,显示< code > ArrayIndexOutOfBound < code >异常,数组大小为-2。 我怎么能做到这一

  • 本文向大家介绍jQuery插件echarts设置折线图中折线线条颜色和折线点颜色的方法,包括了jQuery插件echarts设置折线图中折线线条颜色和折线点颜色的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了jQuery插件echarts设置折线图中折线线条颜色和折线点颜色的方法。分享给大家供大家参考,具体如下: 1、问题背景 设计一条折线图,但是图形中不用插件自带的颜色,需要自定义

  • 在使用CollapsingToolBarLayout和RecycerView时,我发现滚动时工具栏的折叠不一致。我可以在任何东西崩溃之前在RecycerView中滚动到一半,然后它会断断续续地崩溃。这种行为并不总是一致的--有时它会比其他行为崩溃得更多。然而,当我滚动到底部时,它总是完全折叠。我的XML如下所示:

  • 基本折线图 <template> <ve-line :data="chartData" :settings="chartSettings"></ve-line> </template> <script> export default { data () { this.chartSettings = {} return { chartData: { columns: ['日期',

  • 实时显示传感器数据。 用法 Your browser does not support the video tag. 案例:数据变化趋势 功能:显示数字改变的规律

  • 再次强调,在绑定的ViewHolder中,如果我开始在嵌套的RecycerView之外的任何其他项上滚动,滚动工作很好。但是,如果我在嵌套的RecycerView上开始滚动,那么它就会中断。 是否可以防止内部RecycerView上的滚动拦截,只让父级滚动?但是,我希望内部的RecycerView仍然可以处理点击。 以下是关于这种现象的YouTube链接:https://youtu.be/fzj7