简体   繁体   English

MPAndroidChart长按监听器

[英]MPAndroidChart long click listener

In my application I have some bar charts using MPAndroidChart , when I touch in one bar an activity opens showing some informations about the value selected. 在我的应用程序中,我有一些使用MPAndroidChart的条形图 ,当我触摸一个条形时,将打开一个活动,显示一些有关所选值的信息。 I'm using "OnChartValueSelectedListener" to do that. 我正在使用“ OnChartValueSelectedListener”来做到这一点。

The problem is which this is too much sensible to touch. 问题是,这太过明智了。 When I touch the screen just to scroll it, if I touch in one value, one activity opens. 当我触摸屏幕以滚动屏幕时,如果触摸一个值,则会打开一个活动。

I'm looking for something like "OnLongClickListener" to avoid the activity to be opened every time I touch the bars. 我正在寻找类似“ OnLongClickListener”的东西,以避免每次我触摸条时都打开活动。 But I could not find anything. 但是我什么也找不到。 There's some way to emulate an "long touch value" on MP Android Chart? 有什么方法可以在MP Android图表上模拟“长时间触摸值”?

That is part of my code: 那是我的代码的一部分:

mChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
        @Override
        public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
            // registra o estado da selecao para chamar esta funcao na funcao onNothingSelected
            entry = e;
            index = dataSetIndex;
            highlight = h;
            // caso algum cliente tenha sido cadastrado no dia selecionado
            if(e.getVal() > 0) {
                ViewUtil.exibirMensagemAguarde(R.string.aguarde, R.string.carregando_dados, OverviewActivity.this);
                // guarda os dados para serem usados pelo metodo onNothingSelected
                entry = e;
                index = dataSetIndex;
                highlight = h;
                // obtem o ano atual
                int currentYear = Calendar.getInstance().get(Calendar.YEAR);
                // obtem o dia selecionado e concatena o ano atual
                dateSelected = xVals.get(h.getXIndex()) + "/" + currentYear;
                // chama a tarefa assincrona que obtem os clientes cadastrados no dia selecionado
                // e abre a activity
                ClientesCadastradosDiaAsync task = new ClientesCadastradosDiaAsync();
                task.execute();
            }

        }

        @Override
        public void onNothingSelected(){
            onValueSelected(entry, index, highlight);
        }
    });

Your solution would involve creating a class that implements OnChartGestureListener 您的解决方案将涉及创建一个实现OnChartGestureListener的类

Looking at the linked javadocs above, you can immediately see there is a method: 查看上面链接的javadocs,您可以立即看到一个方法:

void onChartLongPressed(MotionEvent me);

You would have to implement this method with the functionality you want. 您将必须使用所需的功能来实现此方法。 It would probably involve getting the raw pixel touch points from the MotionEvent and converting them to x and y values on the chart. 这可能涉及从MotionEvent获取原始像素接触点,并将其转换为图表上的x和y值。 Then you could open the appropriate activity as per your requirement. 然后,您可以根据需要打开适当的活动。 Referring to this answer we can do that like this: 参照这个答案,我们可以这样做:

@Override
public void onChartLongPressed(MotionEvent me) {
    float tappedX = me.getX();
    float tappedY = me.getY();
    MPPointD point = mChart.getTransformer(YAxis.AxisDependency.LEFT).getValuesByTouchPoint(tappedX, tappedY);
    Log.d(TAG, "long pressed at: " + point.x + "," + point.y);
    //TODO: check for long presses that don't correspond to a value on the chart
    //launch the Activity as per your requirement
}

There is an example of a custom OnChartGestureListener inside the example project here 这里的示例项目中有一个自定义OnChartGestureListener示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM