我试图通过布局XML使用嵌入在父片段中的列表片段(我也通过片段管理器完成,没有任何变化)。当我自己嵌入listfragment时,我能够看到光标中的内容。当我覆盖父片段(ShoppingListActivity)的onCreateView并返回自定义视图时,我不再能够看到我的列表片段内容。
在列表片段中,我正在使用CursorLoader和onLoadFinished,总是使用具有内容的游标调用,因此当我在父级fragement上使用自定义布局时,不存在数据不存在的问题。 listfragment没有自定义布局(我最初是通过覆盖列表片段的onCreateView来添加父片段中的按钮,但产生了相同的结果)
我最初也开始使用片段的支持库,但现在使用原生片段。
<?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="match_parent" >
<Button
android:id="@+id/btn_shopping_list_add_item_manually"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/btn_shopping_list_add_item_barcode"
android:onClick="addItemManually"
android:text="TYPE"
/>
<Button
android:id="@+id/btn_shopping_list_add_item_barcode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:onClick="addItemFromBarcode"
android:text="SCAN"
/>
<fragment android:name="com.ainesophaur.shopping.assistant.fragments.ShoppingListFragment"
android:id="@+id/list"
android:layout_height="0dp"
android:layout_below="@+id/btn_shopping_list_add_item_manually"
android:layout_alignParentLeft="true"
android:layout_width="fill_parent" />
---编辑----
由于我无法上传图片,我会画出发生的事情
当onCreateView被覆盖并且提供了自定义视图时(上面的XML布局)我得到了
没有
当我直接调用listview片段时,我得到了
列出项目1 列出项目2
列表视图片段有一个从CursorAdapter扩展的自定义适配器,当我从onCreateView返回上面指定的XML布局时,我的newView和适配器的bindView不会被调用。
我还试图在父片段XML中包含一个listview项(而不是包含片段本身),并给它标准的列表的android ID,我仍然得到相同的结果..如果我给列表视图一个唯一的ID和调用setAdapter列表然后我也得不到结果(即,适配器的bindView和newView永远不会被调用)onLoadFinished的cursorloader --- EDIT
现在我可以上传图片了:)
这是包含ListFragment和CursorAdapter
的代码ListFragment
public class ShoppingListFragment extends ListFragment implements
LoaderManager.LoaderCallbacks<Cursor> {
private int mStoreID;
private long mListID;
private String mStoreName;
private ShoppingListItemsAdapter adapter;
private Context mContext;
private Cursor mCursor;
private static final int SHOPPING_LIST_LOADER = 0x04;
CursorLoader cursorLoader;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = getActivity();
Bundle args = getArguments();
mListID = 4; //args.getLong("listid");
//mStoreName = args.getString("storename");
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mContext = getActivity();
adapter = new ShoppingListItemsAdapter(mContext, mCursor, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
setListAdapter(adapter);
//setListAdapter(adapter);
getLoaderManager().initLoader(SHOPPING_LIST_LOADER, null, this);
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
cursorLoader = new CursorLoader(getActivity(),
Uri.withAppendedPath(ShoppingListProvider.CONTENT_LIST_ITEMS_URI, String.valueOf(mListID)), ShoppingDBAdapter.joinitemProj, null, null, null);
return cursorLoader;
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
adapter.swapCursor(cursor);
//setListShown(true);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
adapter.swapCursor(null);
}
private OnClickListener addManual = new OnClickListener() {
@Override
public void onClick(View v) {
Fragment fragment = new AddItemFragment();
Bundle args = new Bundle();
args.putLong("listid", mListID);
fragment.setArguments(args);
getFragmentManager().beginTransaction()
.replace(R.id.container, fragment).addToBackStack(null)
.commit();
return;
}
};
private static class ShoppingListItemViewHolder {
public TextView separator;
public TextView itemQuantity;
public TextView itemName;
public TextView itemPrice;
public CharArrayBuffer quantityObtainedBuffer = new CharArrayBuffer(128);
public CharArrayBuffer quantityNeededBuffer = new CharArrayBuffer(128);
public CharArrayBuffer titleBuffer = new CharArrayBuffer(128);
public CharArrayBuffer priceBuffer = new CharArrayBuffer(128);
public CharArrayBuffer itemCategoryBuffer = new CharArrayBuffer(128);
}
public void addItemFromBarcode(View v)
{
return;
}
扩展的CursorAdapter
private static class ShoppingListItemsAdapter extends CursorAdapter
{
private static final int STATE_UNKNOWN = 0;
private static final int STATE_SECTIONED_CELL = 1;
private static final int STATE_REGULAR_CELL = 2;
String LAST_CATEGORYNAME = "";
private final CharArrayBuffer mBuffer = new CharArrayBuffer(128);
private int[] mCellStates;
public ShoppingListItemsAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
mCellStates = (c == null) ? null : new int[c.getCount()];
}
@Override
public void changeCursor(Cursor cursor) {
super.changeCursor(cursor);
mCellStates = (cursor == null) ? null : new int[cursor.getCount()];
}
@Override
public Cursor swapCursor(Cursor cursor) {
Cursor oldCursor = super.swapCursor(cursor);
mCellStates = (cursor == null) ? null : new int[cursor.getCount()];
return oldCursor;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
final ShoppingListItemViewHolder holder = (ShoppingListItemViewHolder) view.getTag();
final int COLUMN_NAME_INDEX = cursor.getColumnIndex(ShoppingDBAdapter.KEY_NAME);
final int COLUMN_ITEMCATEGORY_INDEX = cursor.getColumnIndex(ShoppingDBAdapter.KEY_CATEGORYID);
final int COLUMN_PRICE_INDEX = cursor.getColumnIndex(ShoppingDBAdapter.KEY_PRICE);
final int COLUMN_OBTAINED_INDEX = cursor.getColumnIndex(ShoppingDBAdapter.KEY_OBTAINED);
final int COLUMN_NEEDED_INDEX = cursor.getColumnIndex(ShoppingDBAdapter.KEY_NEEDED);
boolean needSeparator = false;
final int position = cursor.getPosition();
cursor.copyStringToBuffer(COLUMN_NAME_INDEX, holder.titleBuffer);
cursor.copyStringToBuffer(COLUMN_PRICE_INDEX, holder.priceBuffer);
cursor.copyStringToBuffer(COLUMN_NEEDED_INDEX, holder.quantityNeededBuffer);
cursor.copyStringToBuffer(COLUMN_ITEMCATEGORY_INDEX, holder.itemCategoryBuffer);
switch (mCellStates[position])
{
case STATE_SECTIONED_CELL:
needSeparator = true;
break;
case STATE_REGULAR_CELL:
needSeparator = false;
break;
case STATE_UNKNOWN:
default:
if (position == 0) {
if(holder.itemCategoryBuffer.sizeCopied > 0)
{
needSeparator = true;
int holderStoreNameSize = holder.itemCategoryBuffer.sizeCopied;
String holderStoreName = "";
for(int i = 0; i < holderStoreNameSize; i++)
{
holderStoreName += holder.itemCategoryBuffer.data[i];
}
LAST_CATEGORYNAME = holderStoreName;
}
}
else
{
cursor.moveToPosition(position - 1);
cursor.copyStringToBuffer(COLUMN_ITEMCATEGORY_INDEX, mBuffer);
if (mBuffer.sizeCopied > 0 && holder.itemCategoryBuffer.sizeCopied > 0)
{
int mBufferSize = mBuffer.sizeCopied;
int holderStoreNameSize = holder.itemCategoryBuffer.sizeCopied;
String mBufferName = "";
String holderStoreName = "";
for(int i = 0; i < holderStoreNameSize; i++)
{
holderStoreName += holder.itemCategoryBuffer.data[i];
}
if(!LAST_CATEGORYNAME.equals(holderStoreName))
{
needSeparator = true;
LAST_CATEGORYNAME = holderStoreName;
}
}
cursor.moveToPosition(position);
}
mCellStates[position] = needSeparator ? STATE_SECTIONED_CELL : STATE_REGULAR_CELL;
break;
}
if (needSeparator) {
holder.separator.setText(holder.itemCategoryBuffer.data, 0 , holder.itemCategoryBuffer.sizeCopied);
holder.separator.setVisibility(View.VISIBLE);
} else {
holder.separator.setVisibility(View.GONE);
}
holder.itemName.setText(holder.titleBuffer.data, 0, holder.titleBuffer.sizeCopied);
holder.itemPrice.setText(holder.priceBuffer.data, 0, holder.priceBuffer.sizeCopied);
holder.itemQuantity.setText(holder.quantityNeededBuffer.data, 0, holder.quantityNeededBuffer.sizeCopied);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = LayoutInflater.from(context).inflate(R.layout.fragment_shopping_list_items_row, parent, false);
ShoppingListItemViewHolder holder = new ShoppingListItemViewHolder();
holder.separator = (TextView)v.findViewById(R.id.separator);
holder.itemName = (TextView)v.findViewById(R.id.item_name);
holder.itemPrice = (TextView)v.findViewById(R.id.item_price);
holder.itemQuantity = (TextView)v.findViewById(R.id.item_quantity);
v.setTag(holder);
return v;
}
}
答案 0 :(得分:1)
一旦我将列表视图片段使用的布局XML从RelativeLayout更改为LinearLayout,我就能看到这些项目了。
答案 1 :(得分:0)
在您的布局中,您已将片段的高度设置为零,因此它不会显示任何内容,请将layout_height设置为其他内容。 (请参阅片段标记中的属性)