ListView with Title - android

Is there a way to insert another View above the List in a ListView? For example, I want a Title Bar (TextView) that sits on top of the List, and scrolls out of view as the List scrolls. I can think of two ways so far of doing this, but both are hacks.
Idea #1 - Use a LinearLayout that pretends to be a ListView. But the problem is you are unable to take advantage of the "smart" loading/unloading of views that an adapter provides.
<ScrollView>
<LinearLayout>
<TextView/>
<LinearLayout
android:orientation="vertical" /> # add ListItems here
</LinearLayout>
</ScrollView>
Idea #2 - Use a hacky ArrayAdapter, with a getView method similar to this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(position == 0)
View view = inflater.inflate(R.layout.special_list_item, null);
else
View view = inflater.inflate(R.layout.regular_list_item, null);
...
return vi;
}

Simple. Just add a header view to your ListView
TextView textView = new TextView(context);
textView.setText("Hello. I'm a header view");
listView.addHeaderView(textView);

Related

Hiding list item seperator for few list items

I have many navigation drawers (mostly in Google Apps) where few of the items are having no List item seperators, while some list items are having list seperators.
I want to achieve the same functionality for my app.
can anybody please help me understand the implementation?
How can i hide list seperator for few of the list items while others have it?
regards,
Rajan
For each list item row use this layout:
<LinearLayout>
(...)
<View
android:id="#+id/viewSeparator1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#646464"/>
</LinearLayout>
Then on your adapter use this
public class DrawerListAdapter extends BaseAdapter{
(...)
public View getView(int position, View convertView, ViewGroup parent){
(...)
View mViewSeparator = convertView.findViewByID(R.id.viewSeparator1);
//I dont know when you want to show a separator so replace this line with the apropriate check: for example: if(position == 0) etc
if(hasSeparator)
mViewSeparator.setVisibility(View.VISIBLE);
else
mViewSeparator.setVisibility(View.GONE);
return convertView;
}
}

Android customized row in listview can't recieve onItemClick

because I need to display something more than just a list in a Fragment.
So I choose Fragment rather than ListFragment, and my layout is something looks like
<linearlayout...>
<TextView...>...<TextView/>
<Button...>...<Button/>
<ListView android:id = "#"+id/mylist" ...></ListView>
</linearylayout>
And I implemnt "MyAdapter" extend BaseAdapter, which has getView like following
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
if(position == 0)
{
return categroyView("Team leader");
}
else if (position == 2)
{
return categroyView("Team memebers");
}
else
{
LayoutInflater inflater = context.getLayoutInflater();
View v = inflater.inflate(R.layout.row_group, null, false);
return v;
}
}
protected View categroyView(String text)
{
TextView txtView = new TextView(context);
txtView.setText(text);
return txtView;
}
It turns out that I can receive onItemClick when its position is 0 or 2 (which as you can see I dynamically generate textView.
Meanwhile I can't receive onItemClick when its position is not 0 or 2 (which I return inflate view from XML)
I've seen some discussion about if customized row layout has some clickable item (like button), this situation will happen, but even my row layout has only one textView, it still failed to receive onItemClick.
p.s.
Also, I select Fragment rather than Activity for other other design issue.
I know I can alternatively add v.setOnClickListene in getView to help this issue, but then still the item won't highlight if I pressed on it.
What is in the position two view? If that thing might be able to take focus sit will do it instead of the list item if you don't want the inards to be clickabke then disable that it's click and it will the be passed to the item
Also are these long lists? You will run into trouble if you inflate a lot

Custom list adapter repeats entries

I am trying to create a ListView that will be populated with the entries from an array.
So this is my item layout:
<?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="60dip" >
<ImageView android:id="#+id/list_item_image"
android:layout_height="wrap_content"
android:padding="2dip"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_width="50dip"/>
<TextView android:id="#+id/list_item"
android:layout_height="fill_parent"
android:textSize="25sp"
android:layout_width="fill_parent"
android:ellipsize="marquee"
android:gravity="center_vertical"
android:padding="5dip" >
</TextView>
</LinearLayout>
I tried changing the layout_height of the LinearLayout but I ran into some problems. If I keep the height at wrap_content, my list is displayed with the correct entries -- Item 1, Item 2, Item 3, and so on until Item 12. However if I change the height to 60dip, the entries repeat after the sixth entry (I get Item 1, Item 2, Item 3, Item 4, Item 5, Item 6, Item 1, Item 2, Item 3...). If I keep on making it larger, the entries repeat more frequently.
This is a snippet from the ListAdapter where I set the list entries:
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout layout;
if (convertView == null){
layout = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.items_list_item, parent, false);
TextView title = (TextView) layout.findViewById(R.id.list_item);
title.setText(menuItems[position]);
ImageView icon = (ImageView) layout.findViewById(R.id.list_item_image);
int logo = getResources().getIdentifier(menuIcons[position], "drawable", getPackageName());
icon.setImageResource(logo);
} else {
layout = (LinearLayout) convertView;
}
return layout;
}
Anybody else encountered this problem? I do not understand what is going on since I thought it should be straight-forward grabbing from the array.
EDIT: included the whole of my getView() method. Pardon the ugly way of getting the icons, I haven't figured it out yet,
You didn't post enough code, but in your Adapter's getView(...) try to make use of the convertView.
public View getView(int position, View convertView, ViewGroup parent){
if(convertView == null){
convertView = mInflater.inflate(R.layout.my_listitem_row, parent, false);
}
//...fill the TextViews on your layout
return convertView;
}
Fetching the icons should be as easy as
icon.setImageResource(R.drawable.my_icon); //the res/drawable folder has the my_icon.png file
From the little snippet of code I'm going to guess that it's something to do with the views being reused and the text not getting updated. I'm not certain though without seeing all of the code for the ListAdapter.
Take a look at this session from Google I/O 2010 for loads of really helpful information on how to use ListViews (and by extension adapters). It contains lots of tips and advice on the best way of using them. If you have time watch the video, if not the slides are avaliable.
Good Luck :)

can we bind 3 controls into a single control in android?

I have 2 textviews and one imageview and i want to bind them into a single control so that i can implement horizontalScrollView on them...
Is there a way to merge different controls so that we can use them???
Or is there a way to implement horizontalScrollView on multiple controls simulataneously??
Thanks in advance
You can build an xml layout that you inflate into a GaleryView. Android allows you to build any mashup of controls into a layout and then use that layout for each row in a listview or each item in a sliding galery.
in a ListAdapter you can inflate the layout and set all of the properties.
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater factory = LayoutInflater.from(context);
View row = factory.inflate(R.layout.list_row, null);
TextView main = (TextView)row.findViewById(R.id.labelMain);
TextView details = (TextView)row.findViewById(R.id.labelDetails);
ImageView icon = (ImageView)row.findViewById(R.id.imgIcon);
main.setText(_items.get(position).Title);
details.setText(_items.get(position).Description);
icon.setImageResource(R.drawable.pin);
return row;
}

Android: How do I make the background color of a ListView show up behind the selection?

I have a ListView powered by a custom adapter that dynamically changes the background color of each row based on its contents. The getView looks like this:
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
Power power = powers.get(position);
if(row == null)
{
row = getLayoutInflater().inflate(android.R.layout.simple_list_item_2, parent, false);
}
TextView text1 = (TextView)row.findViewById(android.R.id.text1);
TextView text2 = (TextView)row.findViewById(android.R.id.text2);
text1.setText(power.name);
text2.setText(power.getAttackLine());
row.setBackgroundColor(0xFF0000FF);
return row;
}
All that works peachy, except that whenever I select an item from the list, the standard red/orange highlight only shows up behind the row, only visible for a few pixels on each side of it. Is there a way to get the selection to show up on top of the background?
Try android:drawSelectorOnTop="true" in the <ListView> element in your layout.
I think the problem is that you're using simple_list_item_2, which has some padding/margin built into it. I recommend making your own version of simple_list_item_2, wherein you have both the height and the width set to fill_parent. You can see the latest simple_list_item_2 here.

Categories

Resources