java - 在基于 fragment 的主从流程中自定义 ListView?

标签 java android android-layout android-fragments master-detail

我发现 ADT 为 Master/Detail Flow Activity 生成的模板相当迟钝,而且我不喜欢 ListView 未在布局文件中声明的事实,这迫使我以编程方式使用它。例如,我不是仅仅在布局文件中的 ListView 上设置 android:background,而是被迫在 fragment 的 onCreateView(...) 中通过 findViewById 找到 ListView > 方法,然后调用 setBackground(...)。为了可维护性和一般可读性,我想通过布局文件做同样的事情。

相反,我尝试在“master” fragment 的 onCreateView 方法中扩展自定义布局:

public class InboxFragment extends ListFragment {

public InboxFragment() {}

private ListView listView = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {
    listView = (ListView) inflater.inflate(R.layout.inbox_fragment_layout, 
            null);
    return listView;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    // the following doesn't work
    listView.setAdapter(new ArrayAdapter<DummyContent.DummyItem>(getActivity(), 
            android.R.layout.simple_list_item_activated_1, 
            android.R.id.text1, 
            DummyContent.ITEMS));
    // nor does this
    //setListAdapter(new ArrayAdapter<DummyContent.DummyItem>(getActivity(), 
    //      android.R.layout.simple_list_item_activated_1, 
    //      android.R.id.text1, 
    //      DummyContent.ITEMS));
}

}

但是,调用 setListAdapter(甚至 setAdapter)似乎没有做任何事情,即使我已经为 ListView 指定了 android 的 id: R.layout.inbox_fragment_layout 中的 id="@id/android:list":

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#D3D3D3" />

我的“主” Activity 只调用 fragment 布局:

public class InboxActivity extends FragmentActivity {

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.inbox_phone_layout);
    }

}

inbox_phone_layout.xml:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragmentPhoneInbox"
    android:name="com.justin.inbox.InboxFragment"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1" />

相反,迎接我的是一个空白页面,而且看起来根本没有加载 ListView。我在这里做错了什么? Sample project on GitHub .

最佳答案

我通过将 inbox_phone_layout.xml 更改为以下内容解决了您的问题:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragmentPhoneInbox"
    android:name="com.justin.inbox.InboxFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

关于java - 在基于 fragment 的主从流程中自定义 ListView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16290220/

相关文章:

java - 在 'mvn install' 之后仍然找不到包

java - XPATH 查询不返回结果

Android - 谷歌地图 API key 不起作用

android - float Action 按钮意外 anchor 重力变化

android - 如何使对象位于 ScrollView 的底部

java - 在 Android 中查看动态文本

java - 使用 LinkedHashMap java 创建 Trigram

android 隐藏来自用户的特殊短信通知并将其从收件箱中删除

android - 几个 Textviews 下的按钮

android - 使用 LayoutParams 将自定义 View 在relativelayout中居中