2

I have a listview that i added an header, but i don't want the listview background to come around the header. Like they are two different views (I don't want to put the listview inside the scrollview)

The listview :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<com.example.tuto.customs.CustomHoverView
    android:id="@+id/chv_activity_list_with_header"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ListView
        android:id="@+id/lv_activity_list_with_header"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/carreblanc"
        android:cacheColorHint="#00000000"
         >
    </ListView>
</com.example.tuto.customs.CustomHoverView>

</LinearLayout>

The header :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" 
>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Header" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:src="@drawable/ic_launcher" />

</LinearLayout>

BaseAdapter class:-

  private class MyAdapter extends BaseAdapter{
    private LayoutInflater inflater;
    private List<String> strings;
    public MyAdapter(Context context, List<String> strings) {
        inflater =  (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.strings = strings;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return strings.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return strings.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View v = convertView;
        ViewHolder h;
        if(v == null){
            v = inflater.inflate(R.layout.adapter_strings, null);
            h = new ViewHolder();
            h.tv = (TextView) v.findViewById(R.id.tv_adapter_strings);
            v.setTag(h);
        }else{
            h = (ViewHolder) v.getTag();
        }
        if(position == 0){
                    // here it changes the first item, but not the header
            v.setBackgroundColor(Color.RED);
        }
        h.tv.setText(""+getItem(position));
        return v;
    }

    private class ViewHolder{
        TextView tv;
    }
}

The adapter :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/tv_adapter_strings"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</LinearLayout>
Tsunaze
  • 3,204
  • 7
  • 44
  • 81

2 Answers2

0

you can create header.xml with separate background and then add it to the listview

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

see example here

Community
  • 1
  • 1
Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
0

header.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" 
android:background="@android:color/holo_blue_bright"
>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Header" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:src="@drawable/ic_launcher" />

</LinearLayout>

Write this in your Activity:-

 LayoutInflater inflater = getLayoutInflater();
            ViewGroup mTop = (ViewGroup) inflater.inflate(R.layout.header,listView,false);
            listView.addHeaderView(mTop, null, false);
Namrata
  • 1,683
  • 1
  • 17
  • 28