以编程方式创建ListView的EmptyView

时间:2015-06-20 16:20:03

标签: java android listview textview

我尝试将EmptyView应用于以编程方式创建的ListView。看起来像这样:

ListView lv = new ListView(getActivity());

CustomAdapter ca = new CustomAdapter(getActivity(),data);

lv.setAdapter(ca);

lv.setEmptyView(createEmptyView("TEST TEXT"));



private TextView createEmptyView(String text) {
        TextView emptyView = new TextView(getActivity());
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        emptyView.setLayoutParams(lp);
        emptyView.setPadding(20, 0, 20, 0);
        emptyView.setText(text);
        emptyView.setTextSize(16);
        emptyView.setTextColor(Color.BLACK);
        emptyView.setGravity(Gravity.CENTER);
        emptyView.setTypeface(null, Typeface.BOLD);

        return emptyView;
}

ListView显示在ViewPager中。当列表(data = ArrayList)并且我不知道原因时,不显示EmptyView。任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

好的我的诀窍如下:

我将EmptyView和ListView添加到一个额外的RelativeLayout,然后将此RelativeLayout添加到ViewPager。这非常有效:)

RelativeLayout rlVP = new RelativeLayout(getActivity());
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
rlVP.setLayoutParams(params2);

TextView emptyView = createEmptyView("Blabla test");
rlVP.addView(emptyView);
lv.setEmptyView(emptyView);
rlVP.addView(lv);