Android ListView that does not scroll?

I'm trying to make a layout that is something similar to how the android market is...where say under comments there is what appears to be a ListView but it does not scroll (the whole page scroll but not the comments). I'm not sure if its even a ListView but I want something that looks like the list view (ie. have those divider bars and what not but NOT SCROLLABLE). There are people suggesting to use a LinearLayout instead of a ListView but I also want the items to be clickable and open a new activity. Please help?

My current layout tree is like so

<LinearLayout> <ScrollView> <RelativeLayout>

I am looking to put content inside the RelativeLayout.

--------------Solutions-------------

As explained by the programmers that did the listView in this video from GoogleIo never put a ListView inside a scroll View. If your list should not scroll use a ViewGroup like a linear Layout and add all the items to this ViewGroup in a loop in your code. If you want a whole row to be clickable you have to use another ViewGroup as the root node for each row and add the OnClickListener to this View.

Sample Code:

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

for (int current = 0; current < itemCount; current++) {
View view = inflater.inflate(R.layout.layout_id, parent, false);

//initialize the view

view.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), CLASS_TO_START)
startActivity(intent);
}
});
viewGroup.addView(view);
if (current < itemCount - 1) {
inflater.inflate(R.layout.line, viewGroup);
}
}

This code will generate one View for every item that you have and put it into the viewGroup. After every item but the last it will also add a divider to the viewGroup.

I had the same issue. I simply extended the default LinearLayout with a setAdapter method:

public class LinearListView extends LinearLayout
{
Adapter adapter;
Observer observer = new Observer(this);

public LinearListView(Context context)
{
super(context);
}

public LinearListView(Context context, AttributeSet attrs)
{
super(context, attrs);
}

public LinearListView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}

public void setAdapter(Adapter adapter)
{
if (this.adapter != null)
this.adapter.unregisterDataSetObserver(observer);

this.adapter = adapter;
adapter.registerDataSetObserver(observer);
observer.onChanged();
}

private class Observer extends DataSetObserver
{
LinearListView context;

public Observer(LinearListView context)
{
this.context = context;
}

@Override
public void onChanged()
{
List<View> oldViews = new ArrayList<View>(context.getChildCount());

for (int i = 0; i < context.getChildCount(); i++)
oldViews.add(context.getChildAt(i));

Iterator<View> iter = oldViews.iterator();

context.removeAllViews();

for (int i = 0; i < context.adapter.getCount(); i++)
{
View convertView = iter.hasNext() ? iter.next() : null;
context.addView(context.adapter.getView(i, convertView, context));
}
super.onChanged();
}

@Override
public void onInvalidated()
{
context.removeAllViews();
super.onInvalidated();
}
}
}

Hope this helps!

I dont have enough reputation points to comment but for those only having 1 item appearing in Nappy's solution make sure you set the following in your layout file:

android:orientation="vertical"

Else it will appear that only one item is being added but in reality they are just being added horizontally...hope this helps someone.

You could make one ListView and put all inside it, so the whole page will scroll. You could roll out your own adapter implementation, but I recommend using CommonsWare's excellent MergeAdapter You could add the labels or divider bars with addView() and the lists with addAdapter(). Check out the page for more information on usage, and the demo project.

Category:android Time:2010-07-07 Views:1

Related post

  • Android listview (array adapter) custom scrolling 2011-02-14

    I would like to change the default behaviour of my listview, so when im scrolling to the last item, the list will keep scrolling untill the last item is at the top of the list. default behaviour stops scrolling when the last item is fully in view. An

  • android listview update list when scrolling past the top 2011-12-25

    I'm wondering how to achieve the nice scroll past top functionality, that then updates the contents of the listview. Do I have to do a custom ListView or is there some kind of built-in functionality? Thanks --------------Solutions------------- There

  • Android Listview reset changes when scroll 2012-02-24

    I'm using a ListFragment with a custom Adapter. My adapter have a textview and a hide imageview with a arrow. When user select a item, the arrows shows up and the background color change. But, when user scroll the list, all the changes get back to de

  • Android listview images disappear when scrolling 2010-12-12

    When scrolling my listview with images. The images will disappear then reappear a second or two later. Any help on the matter would be appreciated! This is in my getView which calls the below code: image_main.setImageBitmap(null); if (curr == 0 &

  • Android listview produces black highlighting over text when scrolling. How to stop this? 2010-05-08

    Duplicate: Background ListView becomes black when scrolling I have a listview within which each item contains black text on a white background. When testing on a Nexus One, when scrolling down to read through the text, a black highlight appears over

  • Android ListView scrolling to top 2010-05-22

    I have a ListView with custom rows. When any of these rows is clicked, the ListView's data is regenerated. I'd like the list to scroll back to the top when this happens. I initially tried using setSelection(0) in each row's OnClickListener to achieve

  • Minimum scroll amount when programmatically selecting ListItem in android ListView 2010-06-14

    refering to this question: http://stackoverflow.com/questions/2949994/how-does-scrolling-in-android-listview-work on change from state 2 to 3 the clicked listitem is increased in its size to show more information. if the clicked item was the last one

  • Android ListView and Scroll problem 2011-02-01

    I have a LinearLayout with a number of TextView followed by a ListView at the bottom - see code below. The problem is the TextView/CheckBox's take around 75% of the screen - ListView then has a scroll inside it at the bottom, not massively usable - h

  • Android: ListViews inside Gallery - how to get scrolling, clicking and swiping like in GTalk? 2011-04-12

    I am writing an Android application, and I use ListViews inside a Gallery. The ListViews represent chat windows. I would really like to be able to swipe between the chat windows in the Gallery, while also being able to scroll in the ListView or click

  • Improving Scroll Smoothness in an Android ListView 2011-06-09

    I have a an Android ListView that has small (say, 1-5 frame) stutters as it is scrolling, about every second or so. I realize that many Android phones have these problems with animation smoothness, however, on my phone (Motorola A855 running Android

  • Android ListView scrolling 2011-07-04

    I have an Android ListView with very tall rows and it like a vertical gallery of pictures. When I fling the view I would like it to precisely stop at the start of each view. Currently it will go over the next view and stop betwen 2 views which is not

  • Android listView find the amount of pixels scrolled 2011-12-12

    I have a listView. When I scroll and stops in a particular place. How can I get the amount of pixels I scrolled(from top)? I have tried using get listView.getScrollY(), but it returns 0. --------------Solutions------------- I had the same problem. I

  • Android :EditText loses content on scroll in ListView? 2012-02-17

    I have list item with EditText in it, I dont know how many items there will be. I have a problem when I enter some text in EditText, and then scroll down a ListView, after I've scroll up again there is no text in my first EditText, or there is some t

  • ensure visible on android listview? 2010-01-01

    Is there a way that I can makle sure a given item in an android listview is entirely visible? I'd like to be able to programmatically scroll to a specific item, like when I press a button for example. --------------Solutions------------- ListView.set

  • Android ListView: get data index of visible item 2010-01-04

    I have an Android ListView created with a SimpleAdapter that has more items in it than fit in the screen. After the list has been scrolled, I need to get the position in the data model of the first visible item in the list. Basically I want a functio

  • How to refresh Android listview? 2010-02-12

    I want to refresh an Android ListView after adding/deleting dynamic data. Can any one tell me how to achieve this? --------------Solutions------------- Call notifyDataSetChanged() on your Adapter object once you've modified the data in that adapter.

  • Mutiple listviews with one only scrolls 2010-08-13

    I need to put several listviews inner a LinearLayout. The Listviews should be wrap content to adapt the listview to their items. I need have one only scroll with which I can scroll all the listviews. I know that i can't use a scrollview because the l

  • Problem with Android ListView after Items are removed 2010-11-08

    I seem to be having a problem with Android ListView. I'm trying to delete the selected item from ListView. The delete method is working and will allow me to delete as many items from my list as needed, but whenever I scroll to the bottom of the list

  • Making android listview layout scrollable 2010-11-30

    I have an xml file that has the layout in ASCII form: --------------- | ImageView -------------- | TextView -------------- | List... -------------- <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.co

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.771 (s). 13 q(s)