I'm working on an e-commerce application, i need to display a list of products -a product = one ImageView and some textViews- from the database, but when i extract the data from the database, everting works fine except the imageView, it shows the same image that is in the source Layout.
this is the getView() Method in my adapter.
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView==null)
{
holder=new ViewHolder();
convertView = inflater.inflate(R.layout.lesproduits, null);
holder.nomduProduit = (TextView)convertView.findViewById(R.id.nomProduit);
holder.prixDuProduit = (TextView)convertView.findViewById(R.id.prixProduit);
holder.imageDuProduit = (ImageView)convertView.findViewById(R.id.imageProduit);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
Bitmap bitmapImage = BitmapFactory.decodeFile(path+File.separator+lesProduits.get(position).getImage());
Drawable drawableImage = new BitmapDrawable(bitmapImage);
System.out.println(path+File.separator+lesProduits.get(position).getImage());
holder.imageDuProduit.setBackgroundDrawable(drawableImage);
holder.nomduProduit.setText(lesProduits.get(position).getNomDuProduit());
holder.prixDuProduit.setText(lesProduits.get(position).getPrixDuProduit());
return convertView;
}
and below is the source Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageProduit"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="30dp"
android:layout_marginTop="5dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/nomProduit"
android:layout_marginTop="5dp"
android:layout_width="170dp"
android:layout_height="30dp"
android:layout_toRightOf="#+id/imageProduit"
android:text="Smart phone"
android:textSize="25sp" />
<TextView
android:id="#+id/prixProduit"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_alignLeft="#+id/nomProduit"
android:layout_below="#+id/nomProduit"
android:layout_marginTop="5dp"
android:text="Large Text"
android:textSize="15sp" />
</RelativeLayout>
You are changing the background, not the foreground.
ImageView has both foregrond and background images.
instead of this
holder.imageDuProduit.setBackgroundDrawable(drawableImage);
use this
holder.imageDuProduit.setImageDrawable(drawableImage);
Related
I have a custom list view which I recently upgraded to use View Holder. Right now the project works fine but the images in the two Image Views don't show on the screen.
This two black icons appear "transparent" but work if I click them.
This is the layout:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/imgInfo"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="12dp"
android:layout_weight="0.04"
android:visibility="visible"
app:srcCompat="#drawable/details64" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/imgInfo"
android:layout_toLeftOf="#+id/imgEdit"
android:layout_toRightOf="#+id/imgInfo"
android:layout_weight="0.24"
android:orientation="vertical">
<TextView
android:id="#+id/txtChamado"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAlignment="center"
android:textColor="#android:color/background_dark"
android:textSize="25sp" />
<TextView
android:id="#+id/txtSolicitante"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textColor="#android:color/background_dark"
android:textSize="15sp" />
<TextView
android:id="#+id/txtItemDeCatalogo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textColor="#android:color/background_dark"
android:textSize="15sp" />
</LinearLayout>
<ImageView
android:id="#+id/imgEdit"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginTop="12dp"
android:layout_weight="0.03"
android:contentDescription=""
app:srcCompat="#drawable/pencil64" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
This is the code snippet:
#Override
public View getView(final int position, View view, final ViewGroup parent) {
View convertView;
ViewHolder holder;
if (view == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.customized_list_view, parent, false);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}else{
convertView = view;
holder = (ViewHolder) convertView.getTag();
}
// Object declaration
final Sette setteData = getItem(position);
//region Put the Tasks values to the textviews
// Get me two strings, one with the older value and one with the newest
String oldChamado = holder.txtChamado.getText().toString();
String newChamado = setteData.getChamado();
// Do not repeat the string in the textview
if (!(oldChamado.equals(newChamado))) {
holder.txtChamado.setText(newChamado);
}
//region Click Listener for the button that leads to Approvals Activity
if (!aBoolean) {
holder.imgEdit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
chamadoClicked = setteData.getChamado();
solicitanteClicked = setteData.getSolicitante();
itemDeCatalogoClicked = setteData.getItemDeCatalogo();
id = setteData.getId();
Intent intent = new Intent(context.getApplicationContext(), ApprovalsActivity.class);
intent.putExtra("Id", id);
intent.putExtra("chamadoClicked", chamadoClicked);
intent.putExtra("solicitanteClicked", solicitanteClicked);
intent.putExtra("itemDeCatalogoClicked", itemDeCatalogoClicked);
context.startActivity(intent);
}
});
}
//endregion
return convertView;
}// END METHOD
ViewHolder (which I got from an internet example):
public class ViewHolder {
TextView txtChamado;
TextView txtSolicitante;
TextView txtItemDeCatalogo;
ImageView imgInfo, imgEdit;
Typeface bebasKai;
Typeface london;
public ViewHolder(View view){
bebasKai = Typeface.createFromAsset(context.getAssets(), "fonts/bebaskai.otf");
london = Typeface.createFromAsset(context.getAssets(), "fonts/london.ttf");
txtChamado = (TextView) view.findViewById(R.id.txtChamado);
txtChamado.setTypeface(bebasKai);
txtSolicitante = (TextView) view.findViewById(R.id.txtSolicitante);
txtSolicitante.setTypeface(london);
txtItemDeCatalogo = (TextView) view.findViewById(R.id.txtItemDeCatalogo);
txtItemDeCatalogo.setTypeface(london);
imgEdit = (ImageView) view.findViewById(R.id.imgEdit);
imgInfo = (ImageView) view.findViewById(R.id.imgInfo);
}
}
I tried clean, rebuild, download the project from BitBucket again, reinstall the app from scratch on my phone, invalidade cache/restart and check the code.
Am I missing something obvious here?
**EDIT:**Thank you SaNtoRiaN, I knew it was something simple that I was missing.
Change app:srcCompat attribute in your ImageView to android:src to view your images properly. To know more about the difference, check this answer.
im using a listview to display a picture and a name. The problem im having is when i fling the listview it has a very choppy scroll. Im using the Android query lib to load the images. I get a lot of GC_FOR ALLOC freed in the logcat. Not sure what is causing it. Any help please?
listview xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="70dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerInParent="true"
android:src="#drawable/grey2" />
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:src="#drawable/grey2" />
</RelativeLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
getview method:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder holder;
// convertView = null;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(
R.layout.layout, parent, false);
holder = new ViewHolder();
holder.pic=(ImageView)convertView.findViewById(R.id.pic);
holder.fname=(TextView)convertView.findViewById(R.id.fname);
holder.name=(TextView)convertView.findViewById(R.id.name);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
aq2= aq.recycle(convertView);
holder.name.setText(first_name + " " + last_name );
holder.username.setText(username);
q2.id(holder.pic).image(urrl);
return convertView;
}
static public class ViewHolder {
TextView fname;
TextView name;
ImageView pic;
}
I (as many other) have problems getting smooth performance in my listview. I believe I have done everything by the book, but it still lags significantly (~300-500 ms)every time a new item is being drawn on the screen.
I'm using a viewHolder and reusing as many UI elements as possible (I think). There are two clear bottle necks.
The first is the line inflating the XML code in the newView() method. That is for some reason really slow (~300 ms) and I don't know if this is expected or not.
Secondly the bindView takes a long time to execute. This is only because of the TextView.setText() method calls which takes up all the time. Other people have had the same problem and I do understand that it can be a slow thing if Android needs to remeasure the entire UI for every listitem. However, after changing the XML file TextViews to a fixed size I still get really slow performance from .setText(). I know I could put the .setText in a Handler but that seems like a hack in this case. I would prevent the list from being laggy, but it would not add the text any faster. I believe I should be able to set the text faster than ~400 ms and if it takes this long something is wrong with my UI/logic.
But without further ado I present you with the source code: Hope to get some clever insight into what I'm doing wrong.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (ApplicationConfiguration.DEBUGGING)
Debug.startMethodTracing("getview");
View listViewItem;
EpisodeViewHolder holder;
Cursor itemCursor = (Cursor) getItem(position);
if (!itemCursor.moveToPosition(position)) {
throw new IllegalStateException("couldn't move cursor to position "
+ position);
}
mCurrentFeedItem = MyListItem.getByCursor(itemCursor);
int type = getItemViewType(position);
if (convertView == null) {
switch (type) {
case TYPE_EXPAND:
listViewItem = newView(mContext, itemCursor, parent);
break;
default:
listViewItem = newView(mContext, itemCursor, parent);
break;
}
} else {
listViewItem = convertView;
}
bindView(listViewItem, mContext, itemCursor);
switch (type) {
case TYPE_EXPAND:
getPlayerViewHolder(position, listViewItem, itemCursor,
convertView, parent);
}
if (ApplicationConfiguration.DEBUGGING)
Debug.stopMethodTracing();
return listViewItem;
}
This is my newView
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = mInflater.inflate(R.layout.episode_list, null); // here is the bottleneck
EpisodeViewHolder holder = new EpisodeViewHolder();
holder.icon = (ImageView) view.findViewById(R.id.list_image);
holder.mainTitle = (TextView) view.findViewById(R.id.title);
holder.subTitle = (TextView) view.findViewById(R.id.podcast);
holder.timeDuration = (TextView) view.findViewById(R.id.duration);
holder.currentPosition = (TextView) view.getTag(R.id.current_position);
holder.slash = (TextView) view.findViewById(R.id.time_slash);
holder.fileSize = (TextView) view.findViewById(R.id.filesize);
holder.stub = (ViewStub) view.findViewById(R.id.stub);
holder.playerView = (View) view.findViewById(R.id.stub_player);
view.setTag(holder);
return view;
}
and my bindView method is really long but I'll shorten it down a bit
#Override
public void bindView(View view, Context context, Cursor cursor) {
.....
final EpisodeViewHolder holder = (EpisodeViewHolder) view.getTag();
.....
// not a bottleneck
// Loads the image Async. The synchronous part of the call takes around 10%
// of the entire method
PicassoWrapper.load(mContext, i, holder.icon);
// This is the bottleneck
holder.mainTitle.setText(title);
}
XML UI
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_marginRight="7dip"
android:padding="0dip" >
<ImageView
android:id="#+id/list_image"
style="#style/PodcastImage" />
</LinearLayout>
<TextView
android:id="#+id/title"
style="#style/EpisodeHeaderFont"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_alignTop="#+id/thumbnail"
android:layout_toRightOf="#+id/thumbnail"
android:text="Podcast Title" />
<TextView
android:id="#+id/podcast"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_above="#+id/current_position"
android:layout_below="#id/title"
android:layout_marginTop="1dip"
android:layout_toRightOf="#+id/thumbnail"
android:ellipsize="end"
android:text="Podcast Name"
android:textSize="10dip" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/current_position"
style="#style/EpisodeMinorFont"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/thumbnail"
android:layout_toRightOf="#+id/thumbnail" />
<TextView
android:id="#+id/time_slash"
style="#style/EpisodeMinorFont"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/current_position"
android:layout_alignBottom="#+id/current_position"
android:layout_toRightOf="#+id/current_position"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:visibility="gone" />
<TextView
android:id="#+id/duration"
style="#style/EpisodeMinorFont"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/time_slash"
android:layout_alignBottom="#+id/time_slash"
android:layout_toRightOf="#+id/time_slash" />
<TextView
android:id="#+id/filesize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/duration"
android:layout_toRightOf="#+id/duration"
android:gravity="bottom|right"
android:text="0"
android:textSize="10dip"
android:textStyle="bold" />
</RelativeLayout>
<FrameLayout
android:id="#id/drag_handle"
android:layout_width="25dp"
android:layout_height="match_parent"
android:paddingBottom="30dp"
android:paddingTop="30dp" >
<LinearLayout
android:layout_width="7dp"
android:layout_height="fill_parent"
android:background="#drawable/drag_handle"
android:orientation="vertical"
android:tileMode="repeat" >
</LinearLayout>
</FrameLayout>
</LinearLayout>
<ViewStub
android:id="#+id/stub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inflatedId="#+id/stub_player"
android:layout="#layout/list_item_expanded" />
How could I create custom ListView like shown in this picture. I know I can make this happen with help of ScrollView and Layouts, but I need to do as a ListView.
Each listview item go over each other.
This may help you
List view with custom view items with partially overlaps (Android)
I have this code like your Requirement. List may overlap by reducing the dividerheight to minus value
<?xml version="1.0" encoding="utf-8"?>
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="-5dp"
android:listSelector="#drawable/list_selector" />
Then add background color to adapter according to the position.
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_row, null);
TextView title = (TextView)vi.findViewById(R.id.title); // title
TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name
TextView duration = (TextView)vi.findViewById(R.id.duration); // duration
ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
HashMap<String, String> song = new HashMap<String, String>();
if(position % 2 ==0){
vi.setBackgroundResource(R.drawable.listselector_1);
}else if(position % 3 ==0){
vi.setBackgroundResource(R.drawable.listselector_2);
}else{
vi.setBackgroundResource(R.drawable.listselector_3);
}
song = data.get(position);
// Setting all values in listview
title.setText(song.get(CustomizedListView.KEY_TITLE));
artist.setText(song.get(CustomizedListView.KEY_ARTIST));
duration.setText(song.get(CustomizedListView.KEY_DURATION));
imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), thumb_image);
return vi;
}
Otherwise you can use listselector images also as your requirement
Thank you guys for trying help me to solve this issue. I look every answer here and finally get i want. I think it will be better I put my code here so maybe someone it will be helpful. The only difference is that my created listview has also shadow in it.
Here is the code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:my_font="http://schemas.android.com/apk/res/com.Helix.android.SmartBonus"
android:orientation="vertical"
android:id="#+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="5dp"
>
<ImageView
android:id="#+id/imageview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/shadow_bg"
android:visibility="visible"/>
<RelativeLayout
android:id="#+id/secondLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:background="#drawable/drop_shadow"
android:layout_below="#id/imageview">
<ImageView
android:id="#+id/companyImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/secret_logo_small"
android:layout_marginRight="10dp"/>
<TextView
android:id="#+id/companyDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/companyImageView"
android:textColor="#android:color/white"
android:layout_marginTop="7dp"
my_font:ttf_name="300"
android:text="Салон кросаты"/>
<TextView
android:id="#+id/companyName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/companyImageView"
android:layout_below="#id/companyDescription"
android:textColor="#android:color/white"
my_font:ttf_name="300"
android:text="Secret"/>
<TextView
android:id="#+id/companyPercentText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:layout_marginTop="7dp"
android:textSize="19sp"
android:layout_marginRight="1dp"
android:layout_toLeftOf="#+id/companyPercent"
my_font:ttf_name="700"
android:text="-20"/>
<TextView
android:id="#id/companyPercent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textColor="#android:color/white"
android:layout_marginTop="7dp"
android:textSize="12sp"
my_font:ttf_name="300"
android:text="%"/>
<ImageView
android:id="#+id/companyPercentImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/companyPercent"
android:layout_marginTop="7dp"
android:textColor="#android:color/white"
android:src="#drawable/percentage_devider"/>
<TextView
android:id="#+id/companyDistance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/companyPercentImage"
android:textSize="16dp"
android:textColor="#A57F1C"
my_font:ttf_name="300"
android:text="7m"/>
<LinearLayout
android:id="#+id/checkingButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/companyDistance"
android:layout_marginTop="10dp"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:focusable="false"
android:background="#drawable/green_button_bg"
android:visibility="gone">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="16dp"
android:textColor="#android:color/white"
my_font:ttf_name="300"
android:text="Check-in"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="16dp"
android:layout_marginLeft="10dp"
android:textColor="#color/checkin_point_color"
android:layout_weight="1"
my_font:ttf_name="300"
android:text="#string/ten_point"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="#drawable/slak"
/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
adapter.
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
final Discount discount = getItem(i);
Discount prev_discount = null;
if (i > 0){
prev_discount = getItem(i-1);
}
if (view == null){
final LayoutInflater li = (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = li.inflate(R.layout.discount_data_item, viewGroup, false);
viewHolder = new ViewHolder();
viewHolder.logo = (ImageView)view.findViewById(R.id.companyImageView);
viewHolder.name = (SmartBonus_TextView)view.findViewById(R.id.companyName);
viewHolder.description = (SmartBonus_TextView)view.findViewById(R.id.companyDescription);
viewHolder.percent = (SmartBonus_TextView)view.findViewById(R.id.companyPercentText);
viewHolder.distance = (SmartBonus_TextView)view.findViewById(R.id.companyDistance);
viewHolder.mainLayout = (RelativeLayout)view.findViewById(R.id.mainLayout);
viewHolder.secondLayaout = (RelativeLayout)view.findViewById(R.id.secondLayout);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
if (i == 0){
viewHolder.mainLayout.setBackgroundColor(android.R.color.transparent);
setRoundedBackground(viewHolder.secondLayaout, Color.parseColor(discount.getBackgroundColor()));
} else {
viewHolder.mainLayout.setBackgroundColor(Color.parseColor(prev_discount.getBackgroundColor()));
setRoundedBackground(viewHolder.secondLayaout, Color.parseColor(discount.getBackgroundColor()));
}
return view;
}
private void setRoundedBackground(View view,int color){
final GradientDrawable gradientDrawable = (GradientDrawable) view.getBackground().mutate();
gradientDrawable.setColor(color);
gradientDrawable.invalidateSelf();
}
Here is the result
You should override getView method.
Sample code:
View getView(int position, View convertView, ViewGroup parent){
Object data = getItem(positon);
//usually data object should have type property
if(data.type == TYPE1){
return getLayoutInflater().inflate(R.layout.custom_xml1, null);
}else (data.type == TYPE2){
return getLayoutInflater().inflate(R.layout.custom_xml2, null);
}else{
return getLayoutInflater().inflate(R.layout.custom_xml, null);
}
};
Note:You should reuse convertView object for performance.
Ok, As I have sort of time I manage to write few steps to achieve your task.
1. Make Custom drawable using xml in drawable folder with top corners have radius..
2. Now in your Listview attributes just define negative divider height
Like, android:dividerHeight="-20dp"
3. Set the Custom Drawable as a background of List row in getView().
Also its nice if you able to set color to drawable at dynamically for list row. (In getView() of Custom Adapter).
This what I have achieved from above steps:
I'm using a gridview to present various article items consisting of two textfield. Because of different amount of text the height of each item is different and a row should be sized with the height of the biggest item of a row. The gridview uses colnum = autofit.
As the following screenshot before and after scrolling show, the behaviour is as expected since i scroll down and up again: Rows mess up and cut content of big items.
What am I missing?
Gridview before scrolling:
GridView after scrolling down and up:
Further code:
item xml code
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="14dp"
android:textColor="#040404"
android:textSize="20sp"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:textColor="#343434"
android:textSize="14sp"
android:typeface="serif" />
</LinearLayout>
My ListAdapter (recycles views if possible):
public class SearchResultListViewAdapter extends ArrayAdapter
{
Context context;
Newspaper newspaper;
List<SearchResultListItem> searchResults;
int viewStyle;
public SearchResultListViewAdapter(Context context, int resourceId, List<SearchResultListItem> searchResults, int viewStyle) {
super(context, resourceId, searchResults);
this.context = context;
this.searchResults = searchResults;
this.viewStyle = viewStyle;
}
/* private view holder class */
private class ViewHolder
{
TextView txtTitle;
TextView txtText;
}
public View getView(int position, View convertView, ViewGroup parent)
{
View gridItem;
ViewHolder holder = null;
if (convertView == null)
{
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (this.viewStyle == 0)
{
gridItem = mInflater.inflate(R.layout.searchresult_list_item, null);
} else
{
gridItem = mInflater.inflate(R.layout.searchresult_grid_item, null);
}
holder = new ViewHolder();
} else {
gridItem = convertView;
holder = (ViewHolder) gridItem.getTag();
}
holder.txtText = (TextView) gridItem.findViewById(R.id.text);
holder.txtTitle = (TextView) gridItem.findViewById(R.id.title);
gridItem.setTag(holder);
SearchResultListItem rowItem = getItem(position);
holder.txtText.setText(Html.fromHtml(rowItem.getText()), TextView.BufferType.SPANNABLE);
holder.txtTitle.setText(Html.fromHtml(rowItem.getTitle()), TextView.BufferType.SPANNABLE);
return gridItem;
}
}
And finally my activity layout containing the gridview:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent" >
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<EditText
android:id="#+id/searchPatternInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/loadSearchResults"
android:imeOptions="actionSearch"
android:inputType="text" />
<ImageButton
android:id="#+id/showExtendedSearch"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_action_filter" />
<ImageButton
android:id="#+id/loadSearchResults"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/showExtendedSearch"
android:src="#drawable/ic_action_search" />
</RelativeLayout>
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#ffffff" >
<ImageSwitcher
android:id="#+id/nothingLoaded"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#drawable/ic_nothing_loaded" >
</ImageSwitcher>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true" >
<GridView
android:id="#+id/searchResultThumbnailsGridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="160dp"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:gravity="center"
android:horizontalSpacing="4dp"
android:listSelector="#drawable/list_selector"
android:numColumns="auto_fit"
android:padding="10dp"
android:layout_weight="1"
android:stretchMode="columnWidth"
android:verticalSpacing="4dp" />
</LinearLayout>
</RelativeLayout>
</TableRow>
</TableLayout>
You seem to have confusion using View.setTag() and View.getTag() methods. Try this:
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder = null;
if (convertView == null)
{
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (this.viewStyle == 0)
{
convertView = mInflater.inflate(R.layout.searchresult_list_item, null);
} else
{
convertView = mInflater.inflate(R.layout.searchresult_grid_item, null);
}
holder = new ViewHolder();
//Always use View.findViewById() and View.setTag() here
holder.txtText = (TextView) convertView.findViewById(R.id.text);
holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
convertView.setTag(holder);
} else {
//Always use View.getTag() here
holder = (ViewHolder) convertView.getTag();
}
//Always set your data here
SearchResultListItem rowItem = getItem(position);
holder.txtText.setText(Html.fromHtml(rowItem.getText()), TextView.BufferType.SPANNABLE);
holder.txtTitle.setText(Html.fromHtml(rowItem.getTitle()), TextView.BufferType.SPANNABLE);
return convertView;
}
I moved on to use a third party component. The StaggeredGridView workes fine for me, more information can be found here.