我正在使用这个水平列表视图,如果我从 listView 适配器中的任何项目开始,则 emptyView 显示,如果我从 listView 适配器中的项目开始,它会隐藏 emptyView,但我的问题是如果我将永远不会显示空视图从项目开始并从 listView 适配器中删除所有项目,如果我从没有项目开始并尝试将项目添加到 listview 适配器,则会留在那里
主要活动
@Override
protected void onCreate(Bundle savedInstanceState) {
listView = (HorizontalListView) findViewById(R.id.listView1);
imageAdapter = new ImageAdapter(this, products);
listView.setAdapter(imageAdapter);
listView.setEmptyView(findViewById(R.id.empty_list_view));
...
private void loadProduct(Intent data) {
Bundle extras = data.getExtras();
Product p = (Product)extras.getParcelable(PASSED_PRODUCT);
imageAdapter.add(p);
}
...图像适配器类
public class ImageAdapter extends BaseAdapter {
...
public ImageAdapter(Context context, List<Product> products) {
this.context = context;
this.products = products;
}
// getView that displays the data at the specified position in the data set.
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// create a new LayoutInflater
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
gridView = null;
convertView = null;// avoids recycling of grid view
if (convertView == null) {
gridView = new View(context);
// inflating grid view item
gridView = inflater.inflate(R.layout.list_view_item, null);
Product p = products.get(position);
...
public void add(Product p) {
products.add(p);
// notify the list that the underlying model has changed
System.out.println("product added");
notifyDataSetChanged();
}
public void remove(int position) {
products.remove(position);
System.out.println("product removed");
notifyDataSetChanged();
}