首页 > 代码库 > Android HorizontalScrollView 水平滑动 在listview上面动态添加图片

Android HorizontalScrollView 水平滑动 在listview上面动态添加图片

Android HorizontalScrollView   水平滑动    listview 上动态添加图片 


最近遇到了个 在listview展示广告的需要动态添加图片

如图:

技术分享



使用了 horizontalScrollView   在listview上进行添加  


java代码:

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>();

	@Override
	protected 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(this).inflate(
					R.layout.home_item, null);
			ImageView icon = (ImageView) coupon_home_ad_item
					.findViewById(R.id.coupon_ad_iv);// 拿个这行的icon 就可以设置图片

			final String href = http://www.mamicode.com/"http://www.blog.csdn.net/aaawqqq";>

xml代码:


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"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ptr="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coupon_home_ad_hv"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:scrollbars="none" >

    <LinearLayout
        android:id="@+id/header_ll"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="15dp"
        android:background="#dddddd"
        android:orientation="horizontal" >
    </LinearLayout>

</HorizontalScrollView>

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_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="10dp"
    android:gravity="center"
    android:orientation="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="点我"
        android:textColor="#000000"
        android:textSize="20sp" />

    <View
        android:layout_width="1dp"
        android:layout_height="90dp"
        android:background="#000000" />

</FrameLayout>


步骤:

首先加载 header  

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

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


获取header里面的  LinearLayout  


将服务器获取的imageview 通过for循环    add   到header上的linearlayout

添加点击事件等


不足之处请大家指正 谢谢


Android HorizontalScrollView 水平滑动 在listview上面动态添加图片