我正在使用幾個Fragments
的paginator。如何使用setEmptyView()與ListFragment中的自定義列表佈局
我想在列表中沒有條目時顯示一條消息(Fragment
)。
我試圖使用Textview
與android:id="@id/android:empty
但它不起作用。
自定義列表佈局代碼:
<?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>
列表適配器類:
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;
}
}
名單解析方法:
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
方法:
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;
}
是否有任何理由將在'noItems()'方法中創建的'TextView'的可見性設置爲** View.GONE **? – Luksprog
我從這裏複製這段代碼http://stackoverflow.com/questions/4088711/android-listview-default-text-when-no-items。 順便說一下,我試圖刪除這條線,並設置爲可見,但仍然是相同的。 – burjulius
你可以嘗試在ListFragment中只設置一次空的'TextView'(例如'onActivityCreated'),然後簡單地更新它的文本(用'findViewById()'查找'TextView'(當然添加一個id(像'android.R.id.empty')))? – Luksprog