【问题标题】:Custom Layout of ListFragment is not workingListFragment 的自定义布局不起作用
【发布时间】:2014-05-18 13:11:58
【问题描述】:

我尝试使用具有自定义布局的 ListFragment:简单静态标题,在 SimpleCursorAdapter 提供的 ListContent 下方。列表本身也有自定义布局。 SimpleCursorAdapter 有效(查询很好,列表的自定义布局有效)。只要我不对 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 没有连接到片段布局?

问候

毛毛虫

问题的澄清 我有类似的问题,如Android + ListFragment with a custom view hierarchy 但是我没有将内容堆叠在一起,我只在屏幕的上角看到 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>

我还没有考虑过里面的fragment,不知道是否需要这个。昨天我尝试了一些功能来详细了解我的问题。我使用 getView().isShown() 并返回 FALSE - 这对我来说似乎是错误的,但我不知道这里有什么问题......

【问题讨论】:

  • 你在onCreate中定义了ListView吗?我没有看到这样的定义?您需要将您的适配器设置为列表视图和超级!
  • 你不能用默认的android名称覆盖id名称。
  • 你能澄清你的意思吗?在 onActivityCreated 函数中,我执行 super.setListAdapter(dBadapter);
  • 据我了解,Fragments 没有 onCreate 方法,只有我已经返回自定义布局的 onCreateView 方法。 SetContentView 不适用于片段,至少我不知道我必须导入什么才能访问它。
  • 您可以使用getView() 访问片段的运行活动。

标签: android android-layout android-fragments


【解决方案1】:

您的 ListView id 必须是 ListFragment 中的 @android:id/list。 也许您还应该将 super.setListAdapter 替换为 setListAdapter。

【讨论】:

  • 没用。布局 id 和 super 的任何组合都改变了一些东西。
  • 对不起,不是——我的错。使用自定义片段布局时,我仍然看不到列表内容。
【解决方案2】:

嘿,这是一个正确使用 CustomAdapter 的 sn-p:

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

}

【讨论】:

  • 嘿,谢谢。我今晚会用你的代码试试。读到这里,我的脑海中突然冒出一个问题:我是否让我的问题可以理解?我对适配器没有问题 - 它工作正常并以 viewlayout.xml 样式显示内容。我的问题是,我希望片段显示两件事-> 一个文本视图(last_update)和来自适配器的列表。只有当我使用这个自定义的Fragment布局(viewfragment)时,我才能看到Adapter的数据。
  • 我试图实现这一点,但有些常量我不知道它们应该具有什么值。我在上面的文字中做了澄清——你认为这会解决问题吗?我的适配器似乎工作正常,只是没有连接到布局......感谢您的帮助!你认为我的 Activity 中有什么地方有问题吗?
【解决方案3】:

审核后 Blank ListView with SimpleCursorAdapter 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

毛毛虫

【讨论】:

    猜你喜欢
    • 2019-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-11
    • 2012-12-24
    • 2012-12-09
    相关资源
    最近更新 更多