Android custom list view? - android

In my layout I need a custom list view like a box type.
How can do that ?
My XML:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="317dp"
android:gravity="top"
android:orientation="vertical"
android:background="#android:color/transparent"
>
<ListView android:id="#+id/slistview"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:divider="#2867EA"
android:dividerHeight="1dp"
android:cacheColorHint="#00000000"
>
</ListView>
</LinearLayout>

You should create custom list adapter and supply xml layout for each row.
here's a comprehensive tutorial that fits your needs:
Link

You can use customBaseadapter and viewHolder ...

Create a xml for row of the list view and set the background of the row and create a custom adapter and set adapter to list view.
custom list view

Related

How to change ListView Item programmatically in Android?

I have a ListView and I want to change the listitem of it programmatically. Is there any way to do this? Thank you in advance.
This is my ListView XML code.
<ListView
android:id="#+id/foodsListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#android:color/transparent"
android:stackFromBottom="false"
android:transcriptMode="disabled"
tools:listitem="#layout/item_food">
</ListView>
And here is what I want to show instead of item_food:
"R.layout.item_message"
Use RecyclerView instead of ListView. It was create for this t

ReUse in a ListView inside a ListView (or and adapter inside an adapter)

I have heard in stackover that it is possible to put a listview inside a listview or a listview inside a scrollview and therefore an adapter inside an adapter . The inner scrollable element will lose the scroll property.
But What about reuse?
If it is a listview inside a listview and both have their own listadapter or a derived base... do they both are able to reuse their own items? or just one of them?
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:text="Artists"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="visible" />
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:text="Albums"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:text="Tracks"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:text="Playlists"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ListView>
</FrameLayout>
I guess what you need is implemented by commonsguy's Mergeadapter: https://github.com/commonsguy/cwac-merge.
This takes Adapters and Views and displays them as one merged Listview.
MergeAdapter accepts a mix of Adapters and Views and presents them as
one contiguous whole to whatever ListView it is poured into. This is
good for cases where you have multiple data sources, or if you have a
handful of ordinary Views to mix in with lists of data, or the like.
Simply create a MergeAdapter and call addAdapter(), addView(), or
addViews() (latter accepting a List), then attach your adapter
to the ListView.
There is also MergeSpinnerAdapter for use with Spinner widgets.
This is which I've chosen for my case, that I need a horizontal RecyclerView inside my ListView.
The idea is to treat the inner view (e.g.RecyclerView) as one of your ListView items:
create your RecyclerView in ViewHolder
findViewById() & set its LinearLayoutManager in getView(), if (convertView == null)
set RecyclerView's Adapter in getView()
control your RecyclerView in its adapter.
Hope it helps you.

Adding Expandable List View and a normal list View in android

I want to add a Expandable List View and a normal list view in this XML layout when I tried to show the both Expandable List View and a normal list only showing the expandable listview.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity"
android:orientation="vertical" >
<ExpandableListView
android:id="#+id/expandableListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ExpandableListView>
<ListView
android:id="#+id/list_evevnt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
I am afraid you can't use both lists like that ... The way I see it, you have 2 options:
Either you'll add the second listview items in the first expandablelistview by extending the adapter class: for each item in this expandable that belongs to second listview, you'll have to set child count to zero so you will not have anything to expand. In ExpandableListAdapter#getGroupView() you can return the View from second listview adapter getView
There is a project created by Mark Murphy (aka CommonsWare), CWAC MergeAdapter that allows merging different listview and views in a single UI component. Since ExpandableListView is a ListView, then in theory it should work. I used this project successfully but without ExpandableListViews... If it works with these, please let me know
I hope it helps!
Try this......
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
androidrientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ExpandableListView
android:id="#+id/expandableListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</ExpandableListView>
</LinearLayout>
<ListView
android:id="#+id/list_evevnt"
android:layout_width="match_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
Use This
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:weigthSum="1" >
<LinearLayout android layout_weight=0.5
android:layout_height="0dp"
android:layout_width="fill_parent">
<ExpandableListView
android:id="#+id/expandableListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ExpandableListView>
</LinearLayout>
<LinearLayout android layout_weight=0.5
android:layout_height="0dp"
android:layout_width="fill_parent">
<ListView
android:id="#+id/list_evevnt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<LinearLayout>
</LinearLayout>
you can't put 2 list views like that because when you scroll only the first list view will be scrolled .try changing your layout_height of expandablelistview to a specific dip it may help.
Try assigning a android:layout_weight to the ListView element as well. I think you have to have one since you set one for the ExpandableListView.
I used this tutorial for expandable list view, may this will helpful for you. good luck.
I think you should assign
android:layout_width="0px" for both views and assign
android:layout_weight for listview as well.
layout_weight won't work properly without layout_width set to 0px

Multiple ListViews with Custom Adapters in a Single Layout

I created a Custom Tab Navigator and now I need to know how can I use multiple ListViews which will receive Custom Adapters.
When I needed to use a Custom Adapter I created a ListView with id=android:list and set the class to Extends ListActivity. But now I think I can't do that...
To have multiple listViews on a single activity, don't need to extend ListActivity. Just add normal ListViews to the xml lauyout file and the reference them on the activity and set the adapters you want.
Example: xmlfile
<ListView android:id="#+id/list_view1" android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
<ListView android:id="#+id/list_view2" android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
On the activity:
setContentView(R.layout.xmlfile)...
ListView lv1 = (ListView) findViewById(R.id.list_view1);
ListView lv2 = (ListView) findViewById(R.id.list_view2);
lv1.setAdaper(new CustomAdapter1());
lv2.setAdaper(new CustomAdapter2());
#Nuno Gonçalves
A small mistake/optimisation in your XML-file:
In case of ListViews, it is better to define the layout_height and layout_width attributes both as fill_parent and scale them using the layout_gravity attributes. Setting the layout_height of a ListView to wrap_content is not optimal and can cause errors or performance problems.
But your solution will work in this case :)
Example:
<ListView android:id="#+id/list_view1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="1">
</ListView>
<ListView android:id="#+id/list_view2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="1">
</ListView>

Android ListView is not expanding as intended

My scenario is like this:
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical"
android:background="#ffffeb">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp">
// Other stuff in between here..
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="12dp"
android:paddingRight="12dp">
<Button
android:id="#+id/insert_ad_ad_information_category_and_type_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:text="#string/insert_ad_ad_information_category_and_type_button"/>
<ListView
android:id="#+id/insert_ad_ad_information_parameters_frame"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffeb"
android:cacheColorHint="#00000000"
android:divider="#00000000"
android:dividerHeight="0dp"
android:scrollbars="none"
android:listSelector="#android:color/transparent"/>
The problem I have is that I can't get my ListView to get the right height that I want.
I'll add a couple of components with my custom BaseAdapter class and everything there works as intended.
But when I then debug my application I can only see 1,5 out of 3 components in the list and the rest is hidden further down in the ListView.
But how can I make my ListView calculate how many components I have and get it to show all my components directly without having too scroll down?
Another thought is if I could populate any other kind of View with my BaseAdapter? Because the only reason I am using ListView is because of the setAdapter() method.
Appreciate all thoughts and comments :)
You cannot have a ListView in a ScrollView. Thats where the issue is.
As has been said you can't use ListView inside ScrollView.
If you want your listview have different types of items and scroll those with "normal" items
you do in fact need custom adapter. You can base it on BaseAdapter if you like
Adapter has to support your item types, and inflate appropriate layouts,

Categories

Resources