我动态地将项目添加到ListView
,并希望所有项目始终可见,而无需滚动。
布局代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/recipe_inside"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="16dp"
android:src="@drawable/head_cella" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="41dp"
android:background="@drawable/body_cella"
android:gravity="center_horizontal"
android:paddingTop="5dp"
android:text="Remember to save before leaving this page or inserted/modified data will be lost."
android:textColor="#000000"
android:textSize="13dp" />
<ImageView
android:id="@+id/ImageView01"
android:layout_width="match_parent"
android:layout_height="16dp"
android:layout_marginTop="30dp"
android:src="@drawable/head_cella" />
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:background="@drawable/body_cella"
android:gravity="center_horizontal"
android:paddingTop="10dp"
android:text="Category"
android:textColor="#000000"
android:textSize="15dp" />
<Spinner
android:id="@+id/category_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/ImageView02"
android:layout_width="match_parent"
android:layout_height="16dp"
android:layout_marginTop="30dp"
android:src="@drawable/head_cella" />
<TextView
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:background="@drawable/body_cella"
android:gravity="center_horizontal"
android:paddingTop="10dp"
android:text="Name"
android:textColor="#000000"
android:textSize="15dp" />
<EditText
android:id="@+id/recipe_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Recipe title" android:lines="1"/>
<ImageView
android:id="@+id/ImageView03"
android:layout_width="match_parent"
android:layout_height="16dp"
android:layout_marginTop="30dp"
android:src="@drawable/head_cella" />
<TextView
android:id="@+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:background="@drawable/body_cella"
android:gravity="center_horizontal"
android:paddingTop="10dp"
android:text="Ingredients"
android:textColor="#000000"
android:textSize="15dp" />
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" android:cacheColorHint="#00000000" android:divider="#00000000"
android:isScrollContainer="false"
>
</ListView>
<Button
android:id="@+id/button_ing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" android:background="@drawable/add_recipe_button" android:onClick="addItems"/>
</LinearLayout>
</ScrollView></LinearLayout>
旧的OnCreate()函数代码:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.add_recipe_form);
spinner = (Spinner)myhead.findViewById(R.id.category_spinner);
Cursor cursor = mydb.getCategoriesList();
ArrayList<Category> list = new ArrayList<Category>();
list.add(new Category(0,"Choose a category"));
while (cursor.moveToNext()) {
list.add(new Category(cursor.getInt(0),cursor.getString(1)));
}
// Step 2: Create and fill an ArrayAdapter with a bunch of "State" objects
ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, list);
// Step 3: Tell the spinner about our adapter
spinner.setAdapter(spinnerArrayAdapter);
spinner.setOnItemSelectedListener(this);
setListAdapter(adapter);
ingredients.add((View)findViewById(R.layout.row_add_recipe));
adapter.notifyDataSetChanged();
}
如果我移动与使用数据库数据填充我的微调器相关的代码,它总是返回一个NullPointerException,因为它在我的布局中找不到。所以我需要把它保留在这里,将addHeaderView()添加到我的ListView的解决方案似乎没问题,但是我的头部布局还有另一个NullPointerException:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.add_recipe_form);
ListView mylistview = (ListView) findViewById(android.R.id.list);
LinearLayout myhead = (LinearLayout)findViewById(R.id.linear_form);
mylistview.addHeaderView(myhead);
spinner = (Spinner)myhead.findViewById(R.id.category_spinner);
Cursor cursor = mydb.getCategoriesList();
ArrayList<Category> list = new ArrayList<Category>();
list.add(new Category(0,"Choose a category"));
while (cursor.moveToNext()) {
list.add(new Category(cursor.getInt(0),cursor.getString(1)));
}
// Step 2: Create and fill an ArrayAdapter with a bunch of "State" objects
ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, list);
// Step 3: Tell the spinner about our adapter
spinner.setAdapter(spinnerArrayAdapter);
spinner.setOnItemSelectedListener(this);
setListAdapter(adapter);
ingredients.add((View)findViewById(R.layout.row_add_recipe));
adapter.notifyDataSetChanged();
}
答案 0 :(得分:4)
如果我理解你的话,你只需要一些可以与你的列表视图一起滚动的视图。实现它的最佳方式是在ListView()
上致电addHeaderView()
和addFooterView()
。
修改强>
它应该是这样的,我想:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_main_layout);
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View header = inflater.inflate(R.layout.your_header_layout); //R.layout, not R.id!
(ListView) list = findViewById(android.R.list);
list.addHeaderView(header);
list.setAdapter(adapter); //according to docs you should call setAdapter() after addHeaderView()
//to get the spinner
(Spinner) spinner = (Spinner) header.findViewById(R.id.spinner);
}
我是从记忆中写作并没有对它进行测试,但我认为它应该可行。等一下,试一试。
答案 1 :(得分:2)
一种解决方案是将ListView高度与内容一样大。您可以使用以下类而不是ListView来实现此效果:
public class ExpandedListView extends ListView {
private int old_count = 0;
public ExpandedListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
if (getCount() != old_count) {
old_count = getCount();
int totalHeight = 0;
int desiredWidth = MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.AT_MOST);
for (int i = 0; i < getCount(); i++) {
View listItem = getAdapter().getView(i, null, this);
listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = getLayoutParams();
params.height = totalHeight + (getDividerHeight() * (getCount() - 1));
setLayoutParams(params);
requestLayout();
}
super.onDraw(canvas);
}
}
无论如何,在ScrollView中有一个ListView是个坏主意,所以我会在你的Layout中添加其他元素作为页眉/页脚视图,方法是addHeaderView()和addFooterView(),正如MichałK所指出的那样。 p>
这里有一个关于如何使用这些方法的示例:
https://github.com/commonsguy/cw-advandroid/tree/master/ListView/HeaderFooter
答案 2 :(得分:1)
让您的ListView
身高大于内容。
如果您有 5个列表项,每个具有20sp高度将列表视图高度设置为100或更高 这样你就可以实现它