setEmptyView在ListFragment定制listLayout [英] setEmptyView with custom listLayout in ListFragment

查看:140
本文介绍了setEmptyView在ListFragment定制listLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我要显示一个消息,当有未上榜(片段)。

在任何条目

我试图用的TextView与Android:ID =@ ID / Android的:空,但它不能正常工作

自定义列表布局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<字符串> {

    私人最终上下文的背景下;
    私人最终名单,其中,字符串>值;
    私人最终名单,其中,字符串>日期;

    公共JournalAdapter(上下文的背景下,列表与LT;字符串>的值,
            名单<字符串>日期){
        超(背景下,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​​();

        名单<字符串>值=新的ArrayList<字符串>();
        名单<字符串>日期=新的ArrayList<字符串>();

        对(价值值:值){
            values​​.add(Double.toString(value.getValue()));
            dates.add(secondsToDate(value.getDate()));
        }

        setListAdapter(新JournalAdapter(背景下,价值观,日期));

        ListView控件= getListView();
        listView.setEmptyView(noItems(没有项目));
        listView.setOnItemClickListener(本);
    }
 

noItmes方式:

 私人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 of fragments.

I want show a message when there are not any entries on 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);
    }

noItmes 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)));
}

这篇关于setEmptyView在ListFragment定制listLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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