The last time I told you about the use of Baidu map, I briefly summarized the methods that Baidu map has used to call APIs. Today I started to introduce the use of the news client.
Direct Flight --> download the source code from the news client of xiaoshuai
First, let's take a look at the film.
First of all, I would like to thank ruoshui for his selfless congratulations on the implementation of this client. I hope you can provide more support.
First, create the project as shown in
Next, open the main. xml file and edit the following code:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@id/main_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"android:background="@drawable/main_background"> <RelativeLayout android:id="@id/titlebar_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/titlebar_background"> <TextView android:id="@id/titlebar_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/titlebar_xiaoshuai" android:textSize="23sp" android:layout_marginTop="8dp" android:layout_marginLeft="10dp" /> <Button android:id="@id/titlebar_refresh" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/titlebar_btn_refresh_selector" android:layout_alignParentRight="true" android:layout_marginTop="8dp" android:layout_marginRight="12dp" /> </RelativeLayout> <RelativeLayout android:id="@id/categorybar_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/categorybar_background" android:layout_marginTop="-16dp"> <HorizontalScrollView android:id="@id/category_scrollview" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="6dp" android:scrollbars="none" android:layout_toLeftOf="@id/category_arrow_right" android:layout_centerVertical="true"> <LinearLayout android:id="@id/category_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical"/> </HorizontalScrollView> <Button android:id="@id/category_arrow_right" android:layout_width="6dp" android:layout_height="10dp" android:background="@drawable/categorybar_right_arrow" android:layout_alignParentRight="true" android:layout_marginRight="15dp" android:gravity="center_vertical" android:layout_centerVertical="true"/> </RelativeLayout> <ListView android:id="@id/newslist" android:layout_width="wrap_content" android:layout_height="fill_parent" android:listSelector="@drawable/newslist_item_selector" android:cacheColorHint="#00000000" android:divider="@drawable/list_separator_line"> </ListView></LinearLayout>
The following code implements the mainactivity. Java file.
Package COM. sy. news. activity; import Java. util. arraylist; import Java. util. hashmap; import Java. util. list; import android. app. activity; import android. graphics. color; import android. graphics. drawable. colordrawable; import android. OS. bundle; import android. view. gravity; import android. view. view; import android. view. view. onclicklistener; import android. view. viewgroup. layoutparams; import android. widget. button; import android. widget. gridview; import android. widget. horizontalscrollview; import android. widget. linearlayout; import android. widget. simpleadapter; import COM. sy. news. r; import COM. sy. news. util. densityutil; public class mainactivity extends activity {// defines category. setcolumnwidth: Private Final int columnwidthpx = 130; private int mcolumnwidthdip; // defines the scrolling distance from private final int distance = 1000; private int mflingvelocitydip; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); mcolumnwidthdip = densityutil. px2dip (this, columnwidthpx); mflingvelocitydip = densityutil. px2dip (this, flingvelocitypx); // obtain the classification information of the article string [] categoryarray = getresources (). getstringarray (R. array. categories); // set the data adapter list
You also need to use a class to convert between pixels and DP.
Package COM. sy. news. util; import android. content. context; public class densityutil {/*** convert from the unit of DP to PX (pixel) */public static int dip2px (context, float dpvalue) based on the resolution of the mobile phone) {final float scale = context. getresources (). getdisplaymetrics (). density; Return (INT) (dpvalue * scale + 0.5f);}/*** based on the resolution of the phone) to DP */public static int px2dip (context, float pxvalue) {final float scale = context. getresources (). getdisplaymetrics (). density; Return (INT) (pxvalue/scale + 0.5f );}}
In this way, the basic steps of the news client are completed through the above steps. The Code just completed is provided below.