Android Chart Library Mpandroidchart (14)--Using the same chart in a ListView type

Source: Internet
Author: User

Android Chart Library Mpandroidchart (14)--Using the same chart in a ListView type

You long time no see, recently very busy, all the blog updated relatively little, here today to say a relatively simple chart, that is, in the ListView using the same icon, because we will explain in the next article using different charts, the same chart is relatively simple, we look at

Specifically how to achieve, here we first write a cushion, for example, we need a base class activity

Viewpagerbaseactivity
 Packagecom.liuguilin.mpandroidchartsample.activity;/ * * Project name: Mpandroidchartsample * Package Name: com.liuguilin.mpandroidchartsample.activity * file name: Viewpagerbaseactivit Y * Created by: LGL * Created: 2016/12/13 16:28 * Description: TODO */ImportAndroid.os.Bundle;Importandroid.support.v4.app.FragmentActivity;ImportCOM.LIUGUILIN.MPANDROIDCHARTSAMPLE.R; Public Abstract  class viewpagerbaseactivity extends fragmentactivity {    protectedstring[] Mmonths =NewString[] {"Jan","The Feb","Mar","APR","may","June","Jul"," the","Sep","Okt","Nov","Dec"};protectedstring[] Mparties =NewString[] {"Party A","Party B","Party C","Party D","Party E","Party F","Party G","Party H","Party I","Party J","Party K","Party L","Party M","Party N","Party O","Party P","Party Q","Party R","Party S","Party T","Party U","Party V","Party W","Party X","Party Y","Party Z"};@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); }protected float Getrandom(floatRangefloatStartsfrom) {return(float) (Math.random () * range) + Startsfrom; }@Override     Public void onbackpressed() {Super. onbackpressed ();    Overridependingtransition (r.anim.move_left_in_activity, r.anim.move_right_out_activity); }}

This base class is just as we use a lot of repetition of things including month and day conversion, we can ignore, we just need to use a method in this article, there are two of animation

Move_left_in_activity
<?xml version="1.0" encoding="utf-8"?><translate       xmlns:android="http://schemas.android.com/apk/res/android"      android:duration="300"      android:fromXDelta="-100%p"       android:toXDelta="0%p"></translate>
Move_right_out_activity
<?xml version="1.0" encoding="utf-8"?><translate       xmlns:android="http://schemas.android.com/apk/res/android"      android:duration="300"      android:fromXDelta="0%p"       android:toXDelta="100%p"></translate>

Is the simple translation of the animation, OK, we formally began to implement, in our XML, we just need to implement a ListView on the line

Activity_listview_char
<?xml version= "1.0" encoding= "Utf-8"?><linearlayoutxmlns:android="Http://schemas.android.com/apk/res/android"  Android:layout_width="Match_parent"android:layout_height="Match_parent"  Android:orientation="vertical">                    <ListViewandroid:id="@+id/mlistview"android:layout_width= "Match_parent" android:layout_height="match_parent"/>                        </linearlayout>

The logic is actually in the GetView of the adapter.

Listviewbarchartactivity
Packagecom. Liuguilin. Mpandroidchartsample. Activity;/ * * Project name: Mpandroidchartsample * Package Name: com.liuguilin.mpandroidchartsample.activity * file name: listviewbarchartacti Vity * Created by: LGL * Created: 2016/12/2 13:41 * Description: Use the same chart in the ListView */Import Android. Content. Context;Import Android. Graphics. Color;Import Android. OS. Bundle;Import Android. View. Layoutinflater;Import Android. View. View;Import Android. View. ViewGroup;Import Android. Widgets. Arrayadapter;Import Android. Widgets. ListView;Importcom. GitHub. Mikephil. Charting. Charts. Barchart;Importcom. GitHub. Mikephil. Charting. Components. Xaxis;Importcom. GitHub. Mikephil. Charting. Components. YAxis;Importcom. GitHub. Mikephil. Charting. Data. BarData;Importcom. GitHub. Mikephil. Charting. Data. Bardataset;Importcom. GitHub. Mikephil. Charting. Data. Barentry;Importcom. GitHub. Mikephil. Charting. Interfaces. Datasets. Ibardataset;Importcom. GitHub. Mikephil. Charting. Utils. Colortemplate;Importcom. Liuguilin. Mpandroidchartsample. R;Import Java. Util. ArrayList;Import Java. Util. List;public class Listviewbarchartactivity extends viewpagerbaseactivity {@Override protected void onCreate (Bundle saved Instancestate) {Super. OnCreate(savedinstancestate);Setcontentview (R. Layout. Activity_listview_char);Initview ();} private void Initview () {ListView LV = (ListView) Findviewbyid (R. ID. Mlistview);arraylist<bardata> list = new arraylist<bardata> ();// -Items for (int i =0; i <; i++) {List. Add(Generatedata (i +1));} chartdataadapter CDA = new Chartdataadapter (Getapplicationcontext (), list);Lv. Setadapter(CDA);} private class Chartdataadapter extends Arrayadapter<bardata> {public Chartdataadapter (context context, List<bardata> objects) {Super (context,0, objects);} @Override public View getView (int position, view Convertview, ViewGroup parent) {BarData data = GetItem (position);Viewholder holder = null;if (Convertview = = null) {holder = new Viewholder ();Convertview = Layoutinflater. from(GetContext ()). Inflate(R. Layout. List_item_barchart, NULL);Holder. Chart= (Barchart) convertview. Findviewbyid(R. ID. Chart);Convertview. Settag(holder);} else {holder = (Viewholder) convertview. Gettag();} data. Setvaluetextcolor(Color. BLACK);Holder. Chart. GetDescription(). setenabled(false);Holder. Chart. Setdrawgridbackground(false);Xaxis Xaxis = Holder. Chart. Getxaxis();Xaxis. SetPosition(Xaxis. Xaxisposition. BOTTOM);Xaxis. Setdrawgridlines(false);YAxis Leftaxis = Holder. Chart. Getaxisleft();Leftaxis. Setlabelcount(5, false);Leftaxis. Setspacetop( theF;YAxis Rightaxis = Holder. Chart. Getaxisright();Rightaxis. Setlabelcount(5, false);Rightaxis. Setspacetop( theF;Holder. Chart. SetData(data);Holder. Chart. Setfitbars(true);Holder. Chart. Animatey( the);Return Convertview;} private class Viewholder {Barchart chart;}} private BarData generatedata (int cnt) {arraylist<barentry> entries = new arraylist<barentry> ();for (int i =0; i <; i++) {Entries. Add(New Barentry (I, (float) (Math. Random() * -) + -));} bardataset D = new Bardataset (entries,"New DataSet"+ CNT);D. Setcolors(colortemplate. Vordiplom_colors);D. Setbarshadowcolor(Color. RGB(203,203,203));arraylist<ibardataset> sets = new Arraylist<ibardataset> ();Sets. Add(d);BarData cd = new BarData (sets);Cd. Setbarwidth(0.9F;Return CD;}}

Can see, here we just implemented a ListView, in the adapter to write the implementation of my data, and then randomly 20 item on the OK, or quite simple, right, this is just a way of thinking, you can understand this, for our next article to do the groundwork, we will tell the next chapter is a different chart, That's dead end.

Sample: The next article provides

Android Chart Library Mpandroidchart (14)--Using the same chart in a ListView type

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.