【问题标题】:Remove the header's background in listview删除列表视图中标题的背景
【发布时间】:2014-02-10 10:04:21
【问题描述】:

我有一个添加标题的列表视图,但我不希望列表视图背景出现在标题周围。就像它们是两个不同的视图(我不想将列表视图放在滚动视图中)

列表视图:

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

【问题讨论】:

  • 在你的适配器上处理,
  • 你能进一步解释一下吗?
  • 在位置等于 0 时在 getView 方法上更改视图的背景,并在位置不等于 0 时设置另一个背景
  • 没什么,实际上header不是适配器的一部分,所以我无法移除header的背景
  • 你是否为页眉采取了另一种布局????

标签: java android


【解决方案1】:

您可以创建具有单独背景的 header.xml,然后将其添加到列表视图中

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

参见示例here

【讨论】:

  • 这就是我所做的,但它仍然需要我的列表视图的背景
  • 您是否将标题背景设置为其他颜色?
  • 不,我没有,只有列表视图背景
  • 所以在头xml文件中添加背景并检查一次
  • 有背景没问题,但后面还有listview的背景。
【解决方案2】:

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

【讨论】:

  • 它改变了标题背景,但我想要的是删除它。即使我使用其他背景,我也可以在后面看到列表视图的背景
  • 你真正想要什么??
  • 就像标题所说:我想删除标题的背景,因为它似乎嵌入在列表视图中
猜你喜欢
  • 1970-01-01
  • 2019-11-13
  • 1970-01-01
  • 1970-01-01
  • 2011-05-02
  • 1970-01-01
  • 2015-07-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多