Android Horizontalscrollview Horizontal Swipe to add pictures dynamically above the ListView

Source: Internet
Author: User

Android Horizontalscrollview Horizontal swipe on the ListView to add pictures dynamically


Recently encountered a need to dynamically add pictures in the ListView display ad




Using Horizontalscrollview to add on a ListView


Java code:

Package Com.baozi.bzhorizontalscrollview;import Java.util.arraylist;import Android.app.activity;import Android.content.intent;import Android.net.uri;import Android.os.bundle;import Android.text.TextUtils;import Android.view.layoutinflater;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.arrayadapter;import Android.widget.imageview;import Android.widget.linearlayout;import Android.widget.listview;public class Mainactivity extends Activity {private ListView lv;private View headerview;private LinearLayout Header_ll; arraylist<string> ArrayList = new arraylist<string> (), @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); LV = (ListView) Findviewbyid (r.id.lv); Headerview = Layoutinflater.from (this). Inflate (R.layout.item_home_header, null); Header_ll = ( LinearLayout) Headerview.findviewbyid (R.ID.HEADER_LL); for (int i = 0; i <; i++) {View Coupon_home_ad_item = Layoutinflater.from (This). Inflate (R.layout.home_item, null); ImageView icon = (ImageView) coupon_home_ad_ Item.findviewbyid (R.ID.COUPON_AD_IV);//Take this line of icon to set the picture final String href = "HTTP://WWW.BLOG.CSDN.NET/AAAWQQQ"; Textutils.isempty (href)) {Coupon_home_ad_item.setonclicklistener (new Onclicklistener () {// The Click event for each item is added here @overridepublic void OnClick (View v) {uri uri = uri.parse (href); Intent Intent = new Intent (intent.action_v Iew,uri); startactivity (intent);});} Header_ll.addview (Coupon_home_ad_item);} Lv.addheaderview (Headerview);//The header is added to the//ListView test data for (int i = 0; i <) through the Addheaderview method of the ListView 20; i++) {Arraylist.add (i + "-------------" + System.currenttimemillis ());} Arrayadapter adapter = new Arrayadapter (this, r.layout.item,r.id.textview1, arrayList); Lv.setadapter (adapter);}}


XML code:


Activity_main.xml

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android:background= "#f4f4f4"    android:paddingbottom= "@dimen/activity_vertical_margin"    android: paddingleft= "@dimen/activity_horizontal_margin"    android:paddingright= "@dimen/activity_horizontal_margin"    android:paddingtop= "@dimen/activity_vertical_margin"    tools:context= " Com.baozi.bzhorizontalscrollview.MainActivity ">    <listview        android:id=" @+id/lv "        android: Layout_width= "Fill_parent"        android:layout_height= "fill_parent" >    </listview></ Relativelayout>

Item_home_header.xml

<?xml version= "1.0" encoding= "UTF-8"? >

Home_item.xml

<?xml version= "1.0" encoding= "UTF-8"? ><framelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "xmlns:ptr=" Http://schemas.android.com/apk/res-auto "android:id=" @+id/coupon_home_ad_ll "Android:layout_w Idth= "Wrap_content" android:layout_height= "wrap_content" android:layout_marginbottom= "10DP" Android:layout_margin left= "5DP" android:layout_marginright= "5DP" android:layout_margintop= "10DP" android:gravity= "center" android:o        rientation= "Horizontal" > <imageview android:id= "@+id/coupon_ad_iv" android:layout_width= "120DP"        android:layout_height= "90DP" android:layout_margin= "4DP" android:background= "@drawable/ic_launcher" Android:scaletype= "Centercrop"/> <textview android:layout_width= "Wrap_content" Android:layout_        height= "wrap_content" android:layout_gravity= "center" android:text= "Point Me" android:textcolor= "#000000" Android:textsize= "20sp"/>   <view android:layout_width= "1DP" android:layout_height= "90DP" android:background= "#000000"/&gt ;</framelayout>


Steps:

Load Header First

Headerview = Layoutinflater.from (this). Inflate (r.layout. Item_home_header,null);

header_ll = (linearlayout) Headerview. Findviewbyid (r.id. Header_ll);


Get the LinearLayout inside the header


ImageView the server gets through the For loop add to LinearLayout on the header

Add Click events, etc.


Please correct me for the shortcomings.


Android Horizontalscrollview Horizontal swipe to add pictures dynamically above the ListView

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.