Android HorizontalScrollView slide horizontally to add images dynamically on the listview

Source: Internet
Author: User

Android HorizontalScrollView slide horizontally to add images dynamically on the listview

Android HorizontalScrollView horizontal slide listview dynamically add images

 

 

Recently, I encountered a need to dynamically add images to display advertisements in listview.

 

 

Added a horizontalScrollView to the 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
 
  
ArrayList = new ArrayList
  
   
(); @ 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 <10; I ++) {View coupon_home_ad_item = LayoutInflater. from (t His ). inflate (R. layout. home_item, null); ImageView icon = (ImageView) coupon_home_ad_item.findViewById (R. id. coupon_ad_iv); // you can set the final String href = http://www.blog.csdn.net/aaawqqq;if (! TextUtils. isEmpty (href) {coupon_home_ad_item.setOnClickListener (new OnClickListener () {// click events of each item are added here @ Overridepublic void onClick (View v) {Uri uri = Uri. parse (href); Intent intent = new Intent (Intent. ACTION_VIEW, uri); startActivity (intent) ;}}) ;}header_ll.addview (coupon_home_ad_item);} lv. addHeaderView (headerView); // Add the header to the // listview using the addHeaderView method of listview. // test data of listview for (int I = 0; I <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

 


Item_home_header.xml

 

 


Home_item.xml

 

 


 

Steps:

First load the header

HeaderView = LayoutInflater. from (this). inflate (R. layout. item_home_header, null );

 

Header_ll = (LinearLayout) headerView. findViewById (R. id. header_ll );

 

Get the LinearLayout in the header

 

Add the imageview obtained by the server to the linearlayout of the header Through the for Loop

Add click events

 

 

 

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.