【问题标题】:Android How to reuse a header layout as an empty view in a ListViewAndroid 如何在 ListView 中将标题布局重用为空视图
【发布时间】:2011-06-15 11:09:42
【问题描述】:

我在项目的整个生命周期中一直在努力解决这个问题。我的项目中有很多列表,其中大多数都有标题。我一直在制作一个单独的布局文件并使用 addHeaderView() 将其添加到列表中。问题是当数据(ArrayList,在我的例子中)为空时,标题不会出现。经过数小时寻找将标题布局作为空视图重用的方法后,我放弃并决定在代码中复制布局并使用 setEmptyView() 将其分配为空视图。这样做的问题是很多标题都包含可点击的视图,因此我必须为每个重复的视图加倍所有可点击的逻辑。我之前尝试包含标题但失败了,主要是因为我仍然不太了解布局如何膨胀等。

最后,我想出了一个解决方案,我想与社区分享,以防其他人遇到类似问题。我不知道这是否是解决此问题的最佳方法,任何反馈或建议肯定会受到欢迎。

这是包含列表列表视图的布局代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
>
    <ListView
        android:id="@+id/lv_my_list"
        style="@style/ListStyle"
        android:headerDividersEnabled="false"
    />
    <include
        layout="@layout/my_list_header"
        android:id="@+id/my_list_empty"
    />
</LinearLayout>

样式定义了布局的宽度和高度。

现在我有了包含标题视图的布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/my_list_header"
>
    <Button 
        android:id="@+id/my_list_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Click Me"
    />
</LinearLayout>

我知道 LinearLayout 效率不高,我正在尝试使用合并或其他效率措施,但这是第一个有效的版本,所以我现在就使用它。最后是代码:

// set up the header
myListView = (ListView)findViewById(R.id.lv_my_list);
View header = View.inflate(this, R.layout.my_list_header, null);
Button b = (Button)header.findViewById(R.id.my_list_button);
b.setOnClickListener(this);
myListView.addHeaderView(header);

// set up the empty view        
LinearLayout ll = (LinearLayout)findViewById(R.id.my_list_empty);
b = (Button)ll.findViewById(R.id.my_list_button);
b.setOnClickListener(this);
meLocalListView.setEmptyView(ll);

我将按钮包装在布局中的原因是因为我可以将按钮的每个实例设置为将其用作 OnClickListener,然后在我的 onClick 方法中使用一个 id 引用它们:R.id.my_list_button。我需要对此进行更多测试,但它现在似乎有效。我还没有在我的所有列表上实现它,只是一个,所以它可能不适用于所有情况。如果您有任何建议,请告诉我,因为这对我来说已经困扰了很长时间。一个问题可能是,如果您想从 ID 实例化按钮,您可能必须再次完成整个过程才能访问正确的 ID?

【问题讨论】:

标签: android listview header reusability


【解决方案1】:

如果您想在列表为空时显示ListView 的标题,您真正想要做的是显示列表本身而不是单独的空视图。因此,最简单的方法是检查您的列表是否为空,如果还没有,则将列表设置为可见:

getListView().setVisibility(View.VISIBLE);

您可以使用以下代码非常轻松地对此进行测试:

public class TestListActivity extends ListActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_listview);

        // create the empty grid item mapping
        String[] from = new String[] {};
        int[] to = new int[] {};

        // prepare the empty list
        List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();

        // fill in the grid_item layout
        SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.test_listitem, from, to);

        View listHeader = 
                ((LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE))
                .inflate(R.layout.test_listheader, null, false);

        getListView().addHeaderView(listHeader);
        getListView().setAdapter(adapter);
    }
}

列表为空,但标题可见。

【讨论】:

  • 您在代码示例中的何处设置列表视图可见性?
【解决方案2】:

我的解决方案是创建一个带有标题布局的视图,但将背景设置为与列表项相同。 android:background="@android:drawable/list_selector_background"

但如果我使用“包含”从另一个布局文件嵌入标题布局,我的解决方案将不起作用。不知道为什么。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 2016-04-05
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-01
    相关资源
    最近更新 更多