i want to set banner(image) at top of list view dynamically.i am calling list view data from webservices .i did this coding in my lazy adapter class but no banner being visible.i wana set banner like this.
RelativeLayout layout1 = new RelativeLayout(mContext);
ImageView image = new ImageView(mContext);
LayoutParams params2 = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
image.setLayoutParams(params2);
image.setScaleType(ImageView.ScaleType.CENTER);
image.setMaxHeight(50);
image.setMaxWidth(50);
image.getPaddingTop();
image.setImageResource(R.drawable.topheader);
layout1.getPaddingTop();
layout1.addView(image);*/
i have banner image in my drawable.
this is my full code of lazy adpter class
package com.example.jsonparsing;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.RelativeLayout.LayoutParams;
public class LazyAdapter extends BaseAdapter {
int counter = 0;
Context mContext;
Activity a;
public LazyAdapter(Context context) {
this.mContext = context;
this.a = a;
}
public int getCount() {
System.out.println("Called..."+ Constants.vctrCategoryId.size());
return Constants.vctrCategoryId.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
System.out.println("Exception before..");
String strUrl = Constants.vctrImagePath.elementAt(counter).toString();// getting url of the images
System.out.println("Urls...." + strUrl);
URL url =null;
try {
url = new URL(strUrl);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Bitmap bitmap = Constants.DownloadImage(strUrl);
Bitmap resized = Bitmap.createScaledBitmap(bitmap, 100, 100, true);
//text.setImageBitmap(bitmap);
/* this coding i did for setting banner
/*RelativeLayout layout1 = new RelativeLayout(mContext);
ImageView image = new ImageView(mContext);
LayoutParams params2 = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
image.setLayoutParams(params2);
image.setScaleType(ImageView.ScaleType.CENTER);
image.setMaxHeight(50);
image.setMaxWidth(50);
image.getPaddingTop();
image.setImageResource(R.drawable.topheader);
layout1.getPaddingTop();
layout1.addView(image);*/
RelativeLayout layout = new RelativeLayout(mContext);
//RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
//layout.setLayoutParams(params);
TextView text1 = new TextView(mContext);
text1.setText(Constants.vctrCategory.elementAt(counter).toString());
LayoutParams params1 = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
text1.setLayoutParams(params1);
text1.setTextSize(20);
text1.setGravity(Gravity.RIGHT);
layout.addView(text1);
ImageView img = new ImageView(mContext);
img.setImageBitmap(resized);
layout.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
layout.addView(img);
counter++;
return layout;
}
private void setContentView(ImageView image) {
// TODO Auto-generated method stub
}
}
Your solution from the layout point of view:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/imageView1" >
</ListView>
</RelativeLayout>
Guess it helps!
您是否尝试过listView类的addHeaderView()方法
Use this:
View headerView1 = ((LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.custom_add, null, false);///this is your imageview
list_view.addHeaderView(headerView1);
Perfect answer by @kittu88. And if you want to add it dynamically, you can try this.
XML:
<LinearLayout
android:od="@+id/banerLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
Java code
LinearLayout bLayout = (LinearLayout) findViewbyId(R.id.banerLayout);
bLayout.addView(YOUR Image);
you can use the code below ,
list_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="none"/>
<TextView
android:id="@+id/android:empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/empty_list" />
</LinearLayout>
list_header.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/header_layout_root"
android:background="#ffffff"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:background="#000000"
android:textColor="#ffffff"
android:textSize="28dp"
android:gravity="center_horizontal"
android:paddingTop="10dp"
android:paddingBottom="10dp">
</TextView>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/sample_image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@drawable/sample_image"
android:scaleType="fitXY">
</ImageView>
<TextView
android:id="@+id/sample_title"
android:text="@string/sample_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffffff"
android:textSize="12dp"
android:background="#AA000000"
android:padding="5dp"
android:layout_alignParentLeft="true"
android:layout_alignBottom="@id/sample_image">
</TextView>
</RelativeLayout>
</LinearLayout>
Next go back to your ListActivity class and it's time to put in the code to add the header to your list, place this just below the call to setContentView():
ListView lv = getListView();
LayoutInflater inflater = getLayoutInflater();
View header = inflater.inflate(R.layout.list_header, (ViewGroup) findViewById(R.id.header_layout_root));
lv.addHeaderView(header, null, false);
ListWithHeader.java
import android.app.ListActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ListWithHeader extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_main);
//add header to list
ListView lv = getListView();
LayoutInflater inflater = getLayoutInflater();
View header = inflater.inflate(R.layout.list_header, (ViewGroup) findViewById(R.id.header_layout_root));
lv.addHeaderView(header, null, false);
//add some list items
String listItems[] = {"List Item One", "List Item Two", "List Item Three", "List Item Four", "List Item Five"};
lv.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, listItems));
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.