Shane
Shane

Reputation: 758

List view with header

I am creating a list view with header. I have created header.xml in which I have an image. But when I scroll the list header also scrolls. It should be always at the top.`public class CustomListActivity extends ListActivity {

    static final String[] Actress = 
               //new String[] { "Katrina Kaif", "Aishwarya", "Genelia", "Vidya", "Khalid", "Khalid", "Khalid", "Khalid", "Khalid", "Khalid", "Khalid", "Khalid", "Khalid", "Khalid"};
    new String[] { "Aishwarya", "Genelia","Katrina Kaif",   "Khalid", "Khalid", "Khalid", "Khalid", "Khalid", "Khalid", "Khalid", "Khalid", "Khalid", "Khalid", "Vidya",};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ListView lv = getListView();
        View header = getLayoutInflater().inflate(R.layout.header, null);
        lv.addHeaderView(header);
        setListAdapter(new MobileArrayAdapter(getApplicationContext(), Actress));


        lv.setTextFilterEnabled(true);


    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {

        //get selected items
        String selectedValue = (String) getListAdapter().getItem(position);
        Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();

    }

}`

Upvotes: 0

Views: 248

Answers (4)

slybloty
slybloty

Reputation: 6506

Try this:

LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup)inflater.inflate(R.layout.header, lv, false);
lv.addHeaderView(header, null, false);

Upvotes: 0

Ika
Ika

Reputation: 1738

If you simply put whatever views you have in header.xml on top of the ListView in your ListActivity layout file it shouldn't scroll

Upvotes: 0

SJuan76
SJuan76

Reputation: 24885

Instead of putting the header as another cell, create a custom component. Something like (grosso modo):

<LinearLayout orientation="vertical">
  <Header/>
  <ScrollablePanel>
    <ListView/>
  </ScrollablePanel>
</LinearLayout>

Upvotes: 1

Ferdau
Ferdau

Reputation: 1558

Just place a view above your listview, no need to work with headers?

Upvotes: 2

Related Questions