0

我试图通过布局 XML 使用嵌入在父片段中的列表片段(我也通过片段管理器完成了没有更改)。当我自己嵌入 listfragment 时,我可以从光标中看到内容。当我覆盖父片段 (ShoppingListActivity) 的 onCreateView 并返回自定义视图时,我不再能够看到我的列表片段内容。

在列表片段上,我使用 CursorLoader 和 onLoadFinished 始终使用具有内容的游标调用,因此当我在父片段上使用自定义布局时,这不是数据不存在的问题。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 布局)时,我得到

按钮 1 按钮 2

没有

当我直接调用 listview 片段时,我得到

列表项 1
列表项 2

列表视图片段有一个从 CursorAdapter 扩展的自定义适配器,当我从 onCreateView 返回上述指定的 XML 布局时,不会调用适配器的 newView 和 bindView。

我还尝试在父片段 XML 中包含一个 listview 项(而不是包含片段本身),并给它列表的标准 android ID,我仍然得到相同的结果..如果我给列表视图一个唯一的 ID并在列表上调用 setAdapter 然后在 cursorloader 的 onLoadFinished 之后我也没有得到任何结果(即适配器的 bindView 和 newView 永远不会被调用)---编辑

现在我可以上传图片了:)

失踪名单

显示列表


这是包含 ListFragment 和 CursorAdapter 的代码

列表片段

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;
    }
}
4

2 回答 2

1

一旦我将列表视图片段使用的布局 XML 从 RelativeLayout 更改为 LinearLayout,我就能够看到这些项目。

于 2012-08-22T19:35:11.097 回答
0

在您的布局中,您已将片段的高度设置为零,因此它不显示任何内容,请将 layout_height 设置为其他值。(查看片段标签中的属性)

于 2012-08-18T02:20:36.980 回答