gpt4 book ai didi

java - 带有 ListView 标题的 ListView

转载 作者:行者123 更新时间:2023-12-02 08:00:46 25 4
gpt4 key购买 nike

我在将 ListView 添加为 ListView 中的标题时遇到问题(去冗余)。该代码正在运行,但是只有第一个项目显示在标题中。身材看起来不错。

listView = (ListView) findViewById(R.id.default_list_view);
header = (ListView) getLayoutInflater().inflate(R.layout.savings_overview_header_list, null, false);
HeaderAdapter hAdapter = new HeaderAdapter(getLayoutInflater());

hAdapter.addItem("1");
hAdapter.addItem("2");
hAdapter.addItem("3");

header.setAdapter(hAdapter);

for (Policy p : saving.getPolicies()) {
adapter.addItem(p);
}

listView.addHeaderView(header);
listView.setAdapter(adapter);

标题 View :

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/default_list_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:divider="@drawable/list_divider"
android:dividerHeight="2px"
android:fadingEdge="none"
android:fitsSystemWindows="true"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:listSelector="@drawable/list_item_background_selected"
>
</ListView>

我尝试将 ListView 包装在垂直的 LinearLayout 中,但那里也没有雪茄:(

h适配器

private static class HeaderAdapter extends BaseAdapter {

private final LayoutInflater inflater;

protected ArrayList<String> data = new ArrayList<String>();

public HeaderAdapter(LayoutInflater inflater) {
this.inflater = inflater;
}

public void addItem(String s) {
data.add(s);
}

public View getView(int position, View convertView, ViewGroup parent) {

final PolicyViewHolder holder;

if (convertView == null) {
convertView = inflater.inflate(R.layout.savings_overview_header_item, parent, false);
holder = new PolicyViewHolder();

holder.header = (TextView) convertView.findViewById(R.id.list_item_header);
holder.subHeader = (TextView) convertView.findViewById(R.id.list_item_subheader);
holder.img = (ImageView) convertView.findViewById(R.id.list_item_img);
convertView.setTag(holder);
} else {
holder = (PolicyViewHolder) convertView.getTag();
}

String s = data.get(position);

holder.header.setText(s);
holder.subHeader.setText(s);
holder.img.setImageResId(R.drawable.test);

return convertView;
}

public int getCount() {
return data.size();
}

public Object getItem(int position) {
return data.get(position);
}

public long getItemId(int position) {
return position;
}

private static class PolicyViewHolder {
TextView header;
TextView subHeader;
ImageView img;
}
}

header_item

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp" >

<ImageView
android:id="@+id/list_item_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:src="@drawable/ic_launcher"/>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:layout_marginLeft="5dp"
android:layout_toLeftOf="@+id/list_item_value"
android:layout_toRightOf="@+id/header_image"
android:orientation="vertical" >

<TextView
android:id="@+id/list_item_header"
style="@style/header_list_header"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/list_item_subheader"
style="@style/list_header_sub_header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="sub header" />
</LinearLayout>

</RelativeLayout>

我也尝试在这个周围包裹一个 Linearlayout="vertical"

最佳答案

您必须告诉父 View 如何堆叠您的元素。这对于像 LinearLayout 这样的父级来说是必要的。如果您要拥有这样的适配器,则需要有一个父级来包含这些内容,除非它是单个 View 。

更新

Whenever you want to do processing with the views in a ListView you need to create a custom adapter that will handle your logic implementation and pass that information to the views as necessary.

A custom adater would inflate the views piece by piece, this can be dynamic of fixed.

关于java - 带有 ListView 标题的 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8953542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com