[英]How to integrate a custom adapter and custom layout into ListFragment?
[英]Custom Layout of ListFragment is not working
我嘗試使用具有自定義布局的ListFragment:簡單靜態標題,在SimpleCursorAdapter給出的ListContent下方。 列表本身也具有自定義布局。 SimpleCursorAdapter有效(查詢很好,List的“自定義布局”有效)。 只要我不對Fragment本身使用自定義布局,一切都可以正常工作。 如果添加Fragment的布局,則只有標題(Textview)有效,列表保持空白。
列表的自定義布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp" >
</TextView>
<TextView
android:id="@+id/value"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_gravity="right"
android:textSize="20sp" >
</TextView>
片段的自定義布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/last_update"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</TextView>
<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
<TextView android:id="@id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="No data">
</TextView>
我評論了一些東西-值得一試,因為我讀到它有所幫助-對我來說沒有幫助。
最后一個機器人特別是片段代碼:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.viewfragment, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
TableDataGateway tableGate = new TableDataGateway(getActivity());
tableGate.UpdateUIRecords();
TextView tv = (TextView) getView().findViewById(R.id.last_update);
tv.setText(tableGate.getLastUpdateDate());
Cursor cursor = tableGate.selectUIRecords();
dBadapter = new SimpleCursorAdapter(getActivity(), R.layout.viewlayout, cursor, new String[] { DB_COL_GUI_LABEL, DB_COL_LAST_KNOWN_VALUE }, new int[] { R.id.label, R.id.value }, 0);
super.setListAdapter(dBadapter);
}
問題是:為什么ListAdapter不與片段布局連接?
問候
izz
問題的澄清我有一個類似的問題,例如具有自定義視圖層次結構的Android + ListFragment,但是我沒有將內容相互堆疊,我只在屏幕的上角以及應在列表下方的位置看到TextView內容。只是一個白色背景。 我很確定已經按照Google的指示做了任何事情( http://developer.android.com/reference/android/app/ListFragment.html關鍵字屏幕布局)
更新:我不確定是否必須更改main_activity.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
</RelativeLayout>
我沒有考慮其中的片段,不知道是否需要。 昨天我嘗試了一些功能來詳細了解我的問題。 我使用了getView()。isShown()並獲得了返回FALSE-這對我來說似乎是錯誤的,但是我不知道這里有什么問題...
您的ListView ID必須為@android:id / listFragment中的list。 也許您也應該將super.setListAdapter替換為setListAdapter。
嘿,這是正確使用CustomAdapter的代碼段:
private class MyCustomAdapter extends BaseAdapter {
private ArrayList<String> mData = new ArrayList<String>();
private LayoutInflater mInflater;
public MyCustomAdapter() {
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(final String item) {
mData.add(item);
notifyDataSetChanged();
}
@Override
public int getItemViewType(int position) {
if(position < LIST_ITEM_TYPE_1_COUNT)
return LIST_ITEM_TYPE_1;
else
return LIST_ITEM_TYPE_2;
}
@Override
public int getViewTypeCount() {
return LIST_ITEM_TYPE_COUNT;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public String getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int type = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch(type) {
case LIST_ITEM_TYPE_1:
convertView = mInflater.inflate(R.layout.list_item_type1, null);
holder.textView = (TextView)convertView.findViewById(R.id.list_item_type1_text_view);
break;
case LIST_ITEM_TYPE_2:
convertView = mInflater.inflate(R.layout.list_item_type2, null);
holder.textView = (TextView)convertView.findViewById(R.id.list_item_type2_button);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
holder.textView.setText(mData.get(position));
return convertView;
}
}
public static class ViewHolder {
public TextView textView;
}
}
在使用SimpleCursorAdapter查看空白ListView之后, http: //developer.android.com/samples/CustomChoiceList/src/com.example.android.customchoicelist/MainActivity.html http://developer.android.com/samples/CustomChoiceList/res/layout /sample_main.html
我找到了解決方案-到目前為止一切正常。 我只是弄亂了布局文件:作為TextView的兩個內容ListView的值都為“ match_parent”-因此,其他組件的布局空間不足。 布局文件中排在最前面的組件將覆蓋所有其他內容。 我將布局文件調整為:
<?xml version="1.0" encoding="utf-8"?>
<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="@android:id/list"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1">
</ListView>
<TextView android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#FF0000"
android:text="No data">
</TextView>
<TextView
android:id="@+id/last_update"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TextView>
</LinearLayout>
一切正常。 不要使用片段布局為活動-它應該有自己的布局,否則會導致錯誤。 謝謝大家的幫助。
Rgds
izz
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.