Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 543
Kman Avatar asked Jul 07 '10 07:07

Kman


People also ask

How do I make my list view not scrollable?

You can make the ListView widget never scrollable by setting physics property to NeverScrollableScrollPhysics().

Is a default scrollable which does not use other scroll view?

Is a default scrollable which does not use other scroll view? Android supports vertical scroll view as default scroll view. Vertical ScrollView scrolls elements vertically.


1 Answers

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.

like image 69
Janusz Avatar answered Sep 19 '22 14:09

Janusz