Android ListView滚动冲突/挑战项目触摸事件

时间:2017-02-09 18:16:49

标签: android listview scroll touch mpandroidchart

我有一个列表视图,其中每个项目都是MPAndroidChart。问题是在图表上我设置了一些手势,但是当我尝试执行这些手势时,列表视图拦截手势并执行滚动。 现在我想维护列表视图的滚动但我想在图表上执行手势,这是怎么回事?

同样禁用滚动是没用的,因为列表视图无论如何拦截触摸事件和sto以在图表上执行手势。

所以我想做的是停止列表视图拦截图表手势但保持列表视图的滚动

列表视图的类

public class ListViewMultiChartActivity extends DemoBase {

public static Button check_button, close_button;
public static LineChartItem chart;
public static ScatterChartItem chart2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_listview_chart);

    check_button = (Button) findViewById(R.id.button);
    close_button = (Button) findViewById(R.id.button2);
    check_button.setVisibility(View.INVISIBLE);
    close_button.setVisibility(View.INVISIBLE);

    ListView lv = (ListView) findViewById(R.id.listView1);

    ArrayList<ChartItem> list = new ArrayList<ChartItem>();

    list.add(new LineChartItem(generateDataLine(0), getApplicationContext()));
    list.add(new ScatterChartItem(generateDataScatter(1), getApplicationContext()));
    ChartDataAdapter cda = new ChartDataAdapter(getApplicationContext(), list);
    lv.setAdapter(cda);

    chart = (LineChartItem) list.get(0);
    chart2 = (ScatterChartItem) list.get(1);

    check_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (chart.mChart.end_selection || chart2.mChart.end_selection) {
                Highlight[] high;
                if (chart.mChart.end_selection) {
                    high = chart.mChart.getHighlighted();
                } else {
                    high = chart2.mChart.getHighlighted();
                }
                ArrayList<Entry> position;
                ArrayList<Entry> position2;
                LineDataSet d;
                ScatterDataSet d2;
                int i;
                int j = 0;
                int size = chart.mChart.getData().getDataSetCount();
                while (j < size) {
                    i = 0;
                    position = new ArrayList<>();
                    position2 = new ArrayList<>();
                    while (i < high.length) {
                        int k = 0;
                        while (k < chart.mChart.getData().getDataSetByIndex(j).getEntryCount()) {
                            if (high[i].getX() == chart.mChart.getData().getDataSetByIndex(j).getEntryForIndex(k).getX() && j == high[i].getDataSetIndex()) {
                                position.add(chart.mChart.getData().getDataSetByIndex(j).getEntryForIndex(k));
                                position2.add(chart.mChart.getData().getDataSetByIndex(j).getEntryForIndex(k));
                            }
                            k++;
                        }
                        i++;
                    }
                    if (position.size() != 0) {
                        d = new LineDataSet(position, "Data" + j);
                        d.setLineWidth(2.5f);
                        d.setCircleRadius(4f);
                        int color = mColors[j % mColors.length];
                        d.setColor(color);
                        d.setCircleColor(color);
                        chart.mChart.getData().addDataSet(d);
                        d2 = new ScatterDataSet(position2, "Data" + j);
                        d2.setColor(color);
                        chart2.mChart.getData().addDataSet(d2);
                    }
                    j++;
                }
                i = 0;
                while (i < size) {
                    chart.mChart.getData().removeDataSet(0);
                    chart2.mChart.getData().removeDataSet(0);
                    i++;
                }

                Highlight[] highvoid = new Highlight[0];
                chart.mChart.highlightValues(highvoid);
                chart2.mChart.highlightValues(highvoid);
                chart.mChart.invalidate();
                chart2.mChart.invalidate();
                check_button.setVisibility(View.INVISIBLE);
                close_button.setVisibility(View.INVISIBLE);
            }
        }
    });

    close_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (chart.mChart.end_selection || chart2.mChart.end_selection) {
                Highlight[] high;
                if (chart.mChart.end_selection) {
                    high = chart.mChart.getHighlighted();
                } else {
                    high = chart2.mChart.getHighlighted();
                }
                int i = 0;
                int j = 0;
                while (i < high.length) {
                    j = 0;
                    while (j < chart.mChart.getData().getDataSetCount()) {
                        int k = 0;
                        while (k < chart.mChart.getData().getDataSetByIndex(j).getEntryCount()) {
                            if (high[i].getX() == chart.mChart.getData().getDataSetByIndex(j).getEntryForIndex(k).getX() && j == high[i].getDataSetIndex()) {
                                chart.mChart.getData().getDataSetByIndex(j).removeEntry(k);
                                chart2.mChart.getData().getDataSetByIndex(j).removeEntry(k);
                            }
                            k++;
                        }
                        j++;
                    }
                    i++;
                }
                Highlight[] highvoid = new Highlight[0];
                chart.mChart.highlightValues(highvoid);
                chart2.mChart.highlightValues(highvoid);
                chart.mChart.invalidate();
                chart2.mChart.invalidate();
                check_button.setVisibility(View.INVISIBLE);
                close_button.setVisibility(View.INVISIBLE);
            }
        }
    });
}


/**
 * adapter that supports 3 different item types
 */
private class ChartDataAdapter extends ArrayAdapter<ChartItem> {

    public ChartDataAdapter(Context context, List<ChartItem> objects) {
        super(context, 0, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return getItem(position).getView(position, convertView, getContext());
    }

    @Override
    public int getItemViewType(int position) {
        // return the views type
        return getItem(position).getItemType();
    }

    @Override
    public int getViewTypeCount() {
        return 3; // we have 3 different item-types
    }
}

/**
 * generates a random ChartData object with just one DataSet
 *
 * @return
 */

private LineData generateDataLine(int cnt) {
    ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
    ArrayList<Entry> values = new ArrayList<>();
    for (int z = 0; z < 3; z++) {
        values = new ArrayList<Entry>();
        for (int i = 0; i < 20; i++) {
            double val = (Math.random() * 100) + 3;
            values.add(new Entry(i, (float) val));
        }
        LineDataSet d = new LineDataSet(values, "DataSet" + z);
        d.setLineWidth(2.5f);
        d.setCircleRadius(4f);

        int color = mColors[z % mColors.length];
        d.setColor(color);
        d.setCircleColor(color);
        dataSets.add(d);
    }
    LineData data = new LineData(dataSets);
    return data;
}

/**
 * generates a random ChartData object with just one DataSet
 *
 * @return
 */

private ScatterData generateDataScatter(int cnt) {

    ArrayList<IScatterDataSet> dataSets = new ArrayList<IScatterDataSet>();
    ArrayList<Entry> values = new ArrayList<>();
    for (int z = 0; z < 3; z++) {
        values = new ArrayList<Entry>();
        for (int i = 0; i < 20; i++) {
            double val = (Math.random() * 100) + 3;
            values.add(new Entry(i, (float) val));
        }
        ScatterDataSet d = new ScatterDataSet(values, "DataSet" + z);

        int color = mColors[z % mColors.length];
        d.setColor(color);
        dataSets.add(d);
    }
    ScatterData data = new ScatterData(dataSets);
    return data;
}

private int[] mColors = new int[]{
        ColorTemplate.VORDIPLOM_COLORS[0],
        ColorTemplate.VORDIPLOM_COLORS[1],
        ColorTemplate.VORDIPLOM_COLORS[2]
};


}

列表视图项目的摘要

public abstract class ChartItem  {

protected static final int TYPE_BARCHART = 0;
protected static final int TYPE_LINECHART = 1;
protected static final int TYPE_PIECHART = 2;
protected static final int TYPE_SCATTERCHART = 3;

protected ChartData<?> mChartData;

public ChartItem(ChartData<?> cd) {
    this.mChartData = cd;      
}

public abstract int getItemType();

public abstract View getView(int position, View convertView, Context c);

}

列表视图项之一的实现

public class ScatterChartItem extends ChartItem implements OnChartValueSelectedListener {

private Typeface mTf;
public ScatterChart mChart;

public ScatterChartItem(ChartData<?> cd, Context c) {
    super(cd);

    mTf = Typeface.createFromAsset(c.getAssets(), "OpenSans-Regular.ttf");
}

@Override
public int getItemType() {
    return TYPE_SCATTERCHART;
}

@Override
public View getView(int position, View convertView, Context c) {

    ViewHolder holder = null;

    if (convertView == null) {

        holder = new ViewHolder();

        convertView = LayoutInflater.from(c).inflate(
                R.layout.list_item_scatterchart, null);
        holder.chart = (ScatterChart) convertView.findViewById(R.id.chart);

        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    // apply styling
    // holder.chart.setValueTypeface(mTf);
    holder.chart.setDescription("");
    holder.chart.setDrawGridBackground(false);

    XAxis xAxis = holder.chart.getXAxis();
    xAxis.setPosition(XAxisPosition.BOTTOM);
    xAxis.setTypeface(mTf);
    xAxis.setDrawGridLines(false);
    xAxis.setDrawAxisLine(true);

    YAxis leftAxis = holder.chart.getAxisLeft();
    leftAxis.setTypeface(mTf);
    leftAxis.setLabelCount(5, false);
    leftAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)

    YAxis rightAxis = holder.chart.getAxisRight();
    rightAxis.setTypeface(mTf);
    rightAxis.setLabelCount(5, false);
    rightAxis.setDrawGridLines(false);
    rightAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)

    // set data
    holder.chart.setData((ScatterData) mChartData);


    // do not forget to refresh the chart
    // holder.chart.invalidate();
    holder.chart.animateX(750);
    holder.chart.setOnChartValueSelectedListener(this);
    mChart=holder.chart;
    return convertView;
}

@Override
public void onValueSelected(Entry e, Highlight h) {

}

@Override
public void onNothingSelected() {

}

@Override
public void onMultipleValueSelected() {
    if (mChart.end_selection) {
        Highlight[] high= mChart.getHighlighted();
        ListViewMultiChartActivity.chart.mChart.highlightValues(high);
        ListViewMultiChartActivity.check_button.setVisibility(View.VISIBLE);
        ListViewMultiChartActivity.close_button.setVisibility(View.VISIBLE);
    }else{
        ListViewMultiChartActivity.check_button.setVisibility(View.INVISIBLE);
        ListViewMultiChartActivity.close_button.setVisibility(View.INVISIBLE);
    }
}

private static class ViewHolder {
    ScatterChart chart;
}
}

我使用MPAndroidChart,here有一个类我设置图表的手势

0 个答案:

没有答案