views:

614

answers:

2

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 what 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.

A: 

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.

janfsd
+1  A: 

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.

Janusz
thanks =)I ended up using this method after figuring out what an LayoutInflater was and how to use it...now I just have to prettify things in xml.
Kman
Quick question...how do I make the View highlight when I click it?
Kman