在listview中删除标题的背景

时间:2014-02-10 10:04:22

标签: java android

我有一个listview,我添加了一个标题,但我不希望listview背景围绕标题。就像它们是两个不同的视图(我不想将listview放在scrollview中)

列表视图:

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

标题:

<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类: -

  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;
    }
}

适配器:

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

2 个答案:

答案 0 :(得分:0)

您可以创建具有单独背景的header.xml,然后将其添加到listview

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

参见示例here

答案 1 :(得分: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>

在您的活动中写下: -

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