android listview without scrolling - android

I've got a LinearLayout which includes among others two ListViews. Each ListView has it's own ArrayAdapter. Now the Scrolling shouldn't be in the ListViews, the user should see the whole Lists and scroll the whole view. Sourrounding the LinearLayout with a ScrollView doesn't work because of the inerhit Scrolling Views... .
How can I expand the ListViews and let the user scroll only the outer view?

You should not use a ListView this way.
List Views are meant to recycle views, which it cannot do if its not the view that is scrolling.
You could simply use a LinearLayout and add every single view to the layout. This would be better than using a ListView.
(This does not mean it is the best solution)

This can be done easily in RecyclerView. But in this case you are using ListViews so try listView.setScrollContainer(false);
may be it works for you

You should inflate Views Like this.
// from content View of activity or fragment
listView = (LinearLayout) findViewById(R.id.sos_list_view);
listView.removeAllViews();
List<SosObject> sosList = batabase.getAllItems();
for(SosObject t: sosList) {
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Item layout
View view = inflater.inflate(R.layout.sos_prewivew_item, null);
TextView comment = (TextView) view.findViewById(R.id.sos_comment_text);
TextView date = (TextView) view.findViewById(R.id.sos_date_text);
TextView id = (TextView) view.findViewById(R.id.sos_answer_id);
TextView tittle = (TextView) view.findViewById(R.id.answer_tittle);
listView.addView(view);
}
}
Your xml Should look like:
<ScrollView>
<LinearLayout>
</LinearLayout > // Fist list
</LinearLayout > // Second list
</LinearLayout>
</ScrollView>

ListViews cannot be overriden to lose the scrolling ability. They seem to be designed as top-level objects that occupy the whole screen.
So basically you have 2 options here:
Convert all your elements into ListView's items
use LinearLayouts instead of ListViews and wrap all elements in a ScrollView

Related

How to add a LinearLayout in the same activity which has Listview?

I have an Activity where there is a Linear Layout which occupies nearly half of the screen and below that I need to keep a listview which has hold any number of items. Now initially I was doing like this
<ScrollView>
<LinearLayout>
</LinearLayout>
<ListView>
</ListView>
</ScrollView>
But reading all the threads I think it is not really a good idea keeping listview inside Scrollview. Also I am facing a lot of issues like the height of the Listview is not proper.
So how do I keep the layout and listview inside the same activity without using ScrollView?
It is not recommended to include a scrolling View (ListView) inside a ScrollView. What you want to achieve can be done by adding a HeaderView which can be inflated from another XML.
LinearLayout ll = inflater.inflate(R.layout.my_layout, null);
listView.addHeaderView(ll);

Append new item to LinearLayout programmatically?

I'm trying to create something like editable UITableView on iPhone.
I added a button to add new items at the end of the list which would be the equivalent of the "+" button on iphone, this takes user to a list of items to add. My problem is that when they select one I don't know how to add it to the list.
<LinearLayout
android:key="application_preferences"
android:title="Applications">
</LinearLayout>
How can I append a view inside the LinearLayout programatically?
You can use addView to add a child view to a ViewGroup, but also consider just using ListView instead of a LinearLayout.
Your question is not very clear but if you try to add a view to the linear layout programmatically just use Layout Inflater
LayoutInflater localLayoutInflater =(LayoutInflater)getBaseContext().getSystemService("layout_inflater");
View myView = localLayoutInflater.inflate(R.layout.custom_layout, null);
final LinearLayout ll=(LinearLayout)findViewById(R.id.linearlayoutID);
ll.addView(myView);

Remove header from listView

I'm having some problems when trying to remove the header from a listView. At first I use addHeaderView() to add it, but when I change to another layout I want it to disappear but removeHeaderView() doesn't work...
I also tried setting visibility to GONE and it doesn't disappear...
What can I do?
Thanks in advance
Try the approach mentioned below..
Android ListView#addHeaderView and ListView#addFooterView methods are strange: you have to add the header and footer Views before you set the ListView's adapter so the ListView can take the headers and footers into consideration -- you get an exception otherwise. Here we add a ProgressBar (spinner) as the headerView:
// spinner is a ProgressBar
listView.addHeaderView(spinner);
We'd like to be able to show and hide that spinner at will, but removing it outright is dangerous because we'd never be able to add it again without destroying the ListView -- remember, we can't addHeaderView after we've it's adapter:
listView.removeHeaderView(spinner); //dangerous!
So let's hide it! Turns out that's hard, too. Just hiding the spinner view itself results in an empty, but still visible, header area.
Now try to hide the spinner:
spinner.setVisibility(View.GONE);
Result: header area still visible with an ugly space:
The solution is to put the progress bar in a LinearLayout that wraps it's content, and hiding the content. That way the wrapping LinearLayout will collapse when its content is hidden, resulting in a headerView that is technically still present, but 0dip high:
<LinearLayout
xmlns:a="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<!-- simplified -->
<ProgressBar
android:id="#+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Then, set the layout as the header:
spinnerLayout = getLayoutInflater().inflate(R.layout.header_view_spinner, null);
listView.addHeaderView(spinnerLayout);
And when we need to hide it, hide the layout's content, not the layout:
spinnerLayout.findViewById(R.id.spinner).setVisibility(View.GONE);
Now the header disappears from view. No more ugly space at the top!
Most people don't like to use AddHeaderView, however I sometimes found it very convenient, to avoid modifying complex adapters, or if the headers are not very related to them.
With this easy trick you will be able to seamlessly remove/add headers:
I add an empty LinearLayout with orientation vertical, and height wrap_content, as the only Header View (let mListView be the target listView):
LinearLayout mCustomHeaders=new LinearLayout(context);
mCustomHeaders.setOrientation(LinearLayout.VERTICAL);
mListView.addHeaderView(mCustomHeaders);
mListView.setAdapter (.......)
Thenafter, I can add random stuff, anywhere, to the list as header, even when the list is full:
mCustomHeaders.add(myHeaderView1);
mCustomHeaders.add(myHeaderView2);
mCustomHeaders.add(myHeaderView3);
You can also delete all headers, anytime:
mCustomHeaders.removeAllViews(); // will erase all headers
You get the idea .... Hope it helps !
The problem is that you are always creating a new object when you do:
View headerView = someView
So the new view is not the same as the view already added as listView header, try this:
View headerView = inflater.inflate(R.layout.adapter_datatable_saleitem_header, null, false);
headerView.setTag(this.getClass().getSimpleName() + "header");
if (listView.getHeaderViewsCount() > 0) {
View oldView = listView.findViewWithTag(this.getClass().getSimpleName() + "header");
if (oldView != null) {
listView.removeHeaderView(oldView);
}
}
You can check if header count > 0 then remove the header and add it again.
its works fine for me.
View _headerView;
private void function HandleHeaderView(){
if(listView.getHeaderViewsCount() > 0)
{
listView.removeHeaderView(_headerView);
}
/* Add list view header */
_headerView = GetHeaderView();
listView.addHeaderView(_headerView);
}
private View GetHeaderView()
{
View header = getLayoutInflater().inflate(R.layout.header_layout, null);
// TODO: ...
return header
}
Where drawerLogoView is my headerview, here's what I do:
drawerLogoView = mInflater.inflate(R.layout.navigation_drawer_custom_layout, null);
mDrawerList.addHeaderView(drawerLogoView,null,false);
LinearLayout layOut = ((LinearLayout)drawerLogoView.findViewById(R.id.NavProfilePreviewLayout));
layOut.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 0));
That way, it becomes invisible :D
To show it back, you can use this:
LinearLayout layOut = ((LinearLayout)drawerLogoView.findViewById(R.id.NavProfilePreviewLayout));
layOut.setLayoutParams(newRelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
If you are using addHeaderView(), you can't delete your header after that.
So, don't use addHeaderView(). Rather, create your own adapter that
blends your main roster of Views with your header. While my
MergeAdapter will not handle your specific case, you can use it to see
the concept of blending multiple row sources:
https://github.com/commonsguy/cwac-merge
I encountered this problem in a slightly disguised scenario: the ListView I was dealing with came from PreferenceFragment, and the header represents a PreferenceCategory. So, my freedom of setting up the ListView was severely limited. But there were two approaches (partly inspired by other answers on this page). One was to add a custom layout to my PreferenceCategory (using a class that extends android.preference.PreferenceCategory, see Custom PreferenceCategory Headings). But I found an easier workaround: for the first Preference in this PreferenceCategory, I override onCreateView():
#Override public View onCreateView(ViewGroup parent) {
parent.setTop(-parent.getChildAt(0).getTop());
return super.onCreateView(parent);
}

Scrollable Layout with a list embedded

I'm trying to create a scrollable layout with an embedded list, similar to the Spotify application for Android one shown here (One image at the top followed by a list and the whole layout scrolls). I'm aware that the Android developer guidelines state that you shouldn't put a ListView inside of a ScrollView layout so I'm wondering how this is accomplished. Do I need to make each list item manually using layouts and TextViews?
Yes as the List view has default scroll able functionality . If you place a listview inside the scroll view . The list scroll will hamper.
To Avoid such thing , list view has the concepts of header and footer views.
You can add n number of header and footers to the list.
Below is a sample code snippet how to add header/footer by inflating any xml layout to it
LayoutInflater inflater = activity.getLayoutInflater();
LinearLayout listFooterView = (LinearLayout)inflater.inflate(
R.layout.footer_layout, null);
list.addFooterView(listFooterView);
LinearLayout listHeaderView = (LinearLayout)inflater.inflate(
R.layout.header_layout, null);
list.addHeaderView(listHeaderView);

android nested listview

is it possible/advisable to have a nested listview?
i.e. a listView that's contained within a row of another listview?
an example would be where my main list is displaying blog posts, and then in each row, you'd have another list view for the comments for each post (that would be collapsible)
I had the same problem today, so this is what I did to solve it:
I have a ListView, with a CustomAdapter, and on the getView of the customAdapter, I have something like this:
LinearLayout list = (LinearLayout) myView.findViewById(R.id.list_musics);
list.removeAllViews();
for (Music music : albums.get(position).musics) {
View line = li.inflate(R.layout.inside_row, null);
/* nested list's stuff */
list.addView(line);
}
So, resuming, It's not possible to nest to ListViews, but you can create a list inside a row using LinearLayout and populating it with code.
Is what you're looking for the ExpandableListView? Of course, that's limited to only two levels of listings (but that sounds like it would work for your needs).
This sound like what you're looking for? If you're not, or if this doesn't work, I would suggest having two list views: one of, say, blog posts, and the second of comments, and an action on a blog post item takes you to the second view, populated with the relevant comments.
you can do it like this :
inside the parent listview row xml layout add the following table layout
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/table_show"
android:background="#beb4b4">
</TableLayout>
then you have to make a layout for the child list with name reply_row.xml
<?xml version="1.0" encoding="utf-8"?>
<TableRow android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tv_reply_row"
android:textColor="#000"/>
</TableRow>
in your parent listview adapter getview method add the following code :
TableLayout replyContainer = (TableLayout)
// vi is your parent listview inflated view
vi.findViewById(R.id.table_show);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//child listview contents list
String [] replys = {"a","b","c","d"};
for (int i=0;i<replys.length;i++)
{
final View comments = inflater.inflate(R.layout.reply_row, null);
TextView reply_row = (TextView) comments.findViewById(R.id.tv_reply_row) ;
reply_row.setText(replys[i]);
//for changing your tablelayout parameters
TableLayout.LayoutParams tableRowParams=new TableLayout.LayoutParams
(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
int leftMargin=3;
int topMargin=2;
int rightMargin=3;
int bottomMargin=2;
tableRowParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
comments.setLayoutParams(tableRowParams);
TableRow tr = (TableRow) comments;
replyContainer.addView(tr);
}
You'd better use one ListView, not nested. Nesting ListView is an inefficient way. Your ListView may not scroll smoothly and take up more memory.
You could organize your data structure to show nested data in one ListView. Or you can use this project PreOrderTreeAdapter.
It is convenient to show nested data in ListView or RecyclerView. It can be used to make ListView or RecyclerView collapsible, just change the way you provide your data than notify the adapter.

Categories

Resources