我有一个ListView
和一个小部件。我希望小部件始终位于顶部ListView
,并且它应该能够随着项目滚动,但是当适配器中没有项目时它应该仍然可见。这是它不滚动的方式:
布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn_togle_empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="toggle empty"
/>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1">
<ListView
android:id="@+id/list_test"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<ViewStub
android:id="@+id/empty_layout"
android:layout="@layout/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</FrameLayout>
</LinearLayout>
活动代码:
public class MyActivity extends Activity {
private MyAdapter adapter;
private ListView v;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
v = (ListView) findViewById(R.id.list_test);
View empty = findViewById(R.id.empty_layout);
v.setEmptyView(empty);
final MyAdapter adapter = new MyAdapter();
v.setAdapter(adapter);
Button btn = (Button) findViewById(R.id.btn_togle_empty);
btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
adapter.togleEmpty();
}
});
}
private class MyAdapter extends BaseAdapter {
boolean empty = false;
@Override
public int getCount() {
return empty ? 0 : 50;
}
@Override
public Object getItem(int i) {
return new Object();
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
TextView v = new TextView(MyActivity.this);
v.setText("STRING");
return v;
}
public void togleEmpty() {
empty = !empty;
notifyDataSetChanged();
}
}
}
我还有一个想法,将小部件添加为标题,但是当它ListView
为空时它会消失。我怎样才能达到我想要的结果?