如何在ListFragment自定义列表布局中使用setEmptyView() [英] How to use setEmptyView() with custom list layout in ListFragment

查看:202
本文介绍了如何在ListFragment自定义列表布局中使用setEmptyView()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个分页程序有几个片段

我要显示一个消息时有列表中没有条目(片段)。

我试图用的TextView 机器人:ID =@ ID /安卓:空,但它不将不起作用。

自定义列表布局code:

 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:填充=@扪/ padding_medium>    <的TextView
        机器人:ID =@ + ID / label_value
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:layout_centerVertical =真
        机器人:layout_marginLeft =@扪/ padding_medium
        机器人:文字=@字符串/ label_value
        机器人:文字颜色=@机器人:彩色/白
        机器人:TEXTSIZE =18sp/>    <的TextView
        机器人:ID =@ + ID / label_date
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentRight =真
        机器人:layout_centerVertical =真
        机器人:layout_marginRight =@扪/ padding_medium
        机器人:文字=@字符串/ title_time
        机器人:文字颜色=@机器人:彩色/白
        机器人:TEXTSIZE =16SP/>< / RelativeLayout的>

列表适配器类:

 公共类JournalAdapter扩展ArrayAdapter<串GT; {    私人最终上下文的背景下;
    私人最终名单<串GT;值;
    私人最终名单<串GT;日期;    公共JournalAdapter(上下文的背景下,列表与LT;弦乐>的价值观,
            清单<串GT;日期) {
        超(背景下,R.layout.layout_journal_list,价值观);        this.context =背景;
        this.values​​ =值;
        this.dates =日期;
    }    @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup){
        LayoutInflater吹气=(LayoutInflater)上下文
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);        查看rowView = inflater.inflate(R.layout.layout_journal_list,父母,假);        TextView中值=(TextView中)rowView.findViewById(R.id.label_glucose);
        TextView的日期=(TextView中)rowView.findViewById(R.id.label_date);        value.setText(values​​.get(位置));
        date.setText(dates.get(位置));        返回rowView;
    }}

清单分析方法:

 私人无效initListView(){
        值= dataSource.getAllValues​​();        清单<串GT;值=新的ArrayList<串GT;();
        清单<串GT;日期=新的ArrayList<串GT;();        对(价值值:值){
            values​​.add(Double.toString(value.getValue()));
            dates.add(secondsToDate(value.getDate()));
        }        setListAdapter(新JournalAdapter(上下文,值,日期));        ListView控件= getListView();
        listView.setEmptyView(noItems(没有项目));
        listView.setOnItemClickListener(本);
    }

noItems 方法:

 私人TextView的noItems(字符串文本){
        TextView的emptyView =新的TextView(背景);
        emptyView.setLayoutParams(新的LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.FILL_PARENT));
        emptyView.setText(文本);
        emptyView.setTextColor(Color.WHITE);
        emptyView.setTextSize(20);
        emptyView.setVisibility(View.GONE);
        emptyView.setGravity(Gravity.CENTER_VERTICAL
                | Gravity.CENTER_HORIZONTAL);        返回emptyView;
    }


解决方案

这为我工作(接到的这个问题通过威廉勒马克勒)问。你可能已经错过了是的添加创建的TextView 的ListView 布局父(第二从底部code线的 noItems 下面方法):

在我的 ListFragment 我用下面的方法来获取空观点一个观点:

 私人TextView的noItems(字符串文本){
    TextView的emptyView =新的TextView(getActivity());
    //确保导入android.widget.LinearLayout.LayoutParams;
    emptyView.setLayoutParams(新的LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT));
    //相反,这里路过资源ID的我通过色彩解析
    //即,getResources()。的getColor((R.color.gray_dark))
    emptyView.setTextColor(getResources()的getColor(R.color.gray_dark));
    emptyView.setText(文本);
    emptyView.setTextSize(12);
    emptyView.setVisibility(View.GONE);
    emptyView.setGravity(Gravity.CENTER_VERTICAL
            | Gravity.CENTER_HORIZONTAL);    //视图添加到列表视图。这可能是你缺少什么
    (。(ViewGroup中)getListView()的getParent())addView(emptyView)。    返回emptyView;
}

然后在的在onStart()方法的 ListFragment 设置空观点:

  @覆盖
公共无效调用onStart(){
    super.onStart();
    getListView()。setEmptyView(
            noItems(getResources()的getString(R.string.widget_empty)。));
}

I'm using a paginator with a few Fragments.

I want to show a message when there are no entries in the list (Fragment).

I tried to use Textview with android:id="@id/android:empty but it doesn't work.

Custom list layout code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="@dimen/padding_medium" >

    <TextView
        android:id="@+id/label_value"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="@dimen/padding_medium"
        android:text="@string/label_value"
        android:textColor="@android:color/white"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/label_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="@dimen/padding_medium"
        android:text="@string/title_time"
        android:textColor="@android:color/white"
        android:textSize="16sp" />

</RelativeLayout>

List adapter class:

public class JournalAdapter extends ArrayAdapter<String> {

    private final Context context;
    private final List<String> values;
    private final List<String> dates;

    public JournalAdapter(Context context, List<String> values,
            List<String> dates) {
        super(context, R.layout.layout_journal_list, values);

        this.context = context;
        this.values = values;
        this.dates = dates;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.layout_journal_list, parent, false);

        TextView value = (TextView) rowView.findViewById(R.id.label_glucose);
        TextView date = (TextView) rowView.findViewById(R.id.label_date);

        value.setText(values.get(position));
        date.setText(dates.get(position));

        return rowView;
    }

}

List parsing method:

private void initListView() {
        values = dataSource.getAllValues();

        List<String> values = new ArrayList<String>();
        List<String> dates = new ArrayList<String>();

        for (Value value : values) {
            values.add(Double.toString(value.getValue()));
            dates.add(secondsToDate(value.getDate()));
        }

        setListAdapter(new JournalAdapter(context, values, dates));

        listView = getListView();
        listView.setEmptyView(noItems("NO ITEMS"));
        listView.setOnItemClickListener(this);
    }

noItems method:

private TextView noItems(String text) {
        TextView emptyView = new TextView(context);
        emptyView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.FILL_PARENT));
        emptyView.setText(text);
        emptyView.setTextColor(Color.WHITE);
        emptyView.setTextSize(20);
        emptyView.setVisibility(View.GONE);
        emptyView.setGravity(Gravity.CENTER_VERTICAL
                | Gravity.CENTER_HORIZONTAL);

        return emptyView;
    }

解决方案

This worked for me (got the solution from this question asked by William Remacle). What you might have missed is adding the created TextView to the ListView layout parent (second line of code from the bottom of the noItems method below):

In my ListFragment I used the following method to get a view for the empty view:

private TextView noItems(String text) {
    TextView emptyView = new TextView(getActivity());
    //Make sure you import android.widget.LinearLayout.LayoutParams;
    emptyView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT));
    //Instead of passing resource id here I passed resolved color 
    //That is, getResources().getColor((R.color.gray_dark))
    emptyView.setTextColor(getResources().getColor(R.color.gray_dark));
    emptyView.setText(text);
    emptyView.setTextSize(12);
    emptyView.setVisibility(View.GONE);
    emptyView.setGravity(Gravity.CENTER_VERTICAL
            | Gravity.CENTER_HORIZONTAL);

    //Add the view to the list view. This might be what you are missing
    ((ViewGroup) getListView().getParent()).addView(emptyView);

    return emptyView;
}

Then in the onStart() method of the the ListFragment set the the empty view:

@Override
public void onStart() {
    super.onStart();
    getListView().setEmptyView(
            noItems(getResources().getString(R.string.widget_empty)));
}

这篇关于如何在ListFragment自定义列表布局中使用setEmptyView()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆