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