【问题标题】:How to Using inflater to get a list view from fragment in Android Studio如何在 Android Studio 中使用 inflater 从片段中获取列表视图
【发布时间】:2017-09-11 12:54:18
【问题描述】:

我正在尝试从另一个 xml 文件 (fragment_friends.xml) 获取列表视图 ID,而我在 activity_main.xml 布局中。我尝试了 2 种方法,但它们都无法正常工作。

下面的代码将导致应用程序立即崩溃,因为我正在尝试获取此布局中不存在的 id (activity_main)

原始代码

    ListView friendList = (ListView) findViewById(R.id.friend_listView);
    friendList.setAdapter(friendAdaptor);

以下代码有效,但我无法将任何数据插入或显示到我的数据库中。

尝试 1 个代码:

    View inflatedView = getLayoutInflater().inflate(R.layout.fragment_friend, null);
    ListView friendList = (ListView) inflatedView.findViewById(R.id.friend_listView);
    friendList.setAdapter(friendAdaptor);

下面的代码数据库和listView可以工作,但是我的导航栏抽屉不见了,当我尝试返回activity_main时,应用程序崩溃了

尝试 2 个代码

    setContentView(R.layout.fragment_friend);
    ListView friendList = (ListView) inflatedView.findViewById(R.id.friend_listView);
    friendList.setAdapter(friendAdaptor);

app_bar_main.xml

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <!--<include layout="@layout/content_main" />-->
    <FrameLayout
        android:id="@+id/flContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

【问题讨论】:

  • 我将删除我原来的答案并提供一个新答案,希望能解决这里发生的所有问题。
  • 好的,我也更新了app_bar main的代码

标签: android listview layout-inflater findviewbyid setcontentview


【解决方案1】:

我相信您的问题的根源在于不熟悉一般的 Android Fragment API。没关系;每个人开始时都是新的。但这意味着您的问题很难简洁地回答(除了“阅读有关片段”)。

当然,您应该从阅读 Fragments 开始:https://developer.android.com/guide/components/fragments.html

不过,我认为我们可以在这里简要介绍一下。

所有片段都必须托管在一个活动中。执行此操作的常规方法是向活动的 xml 布局添加 &lt;fragment&gt; 标签,或者在活动的 xml 布局中包含 &lt;FrameLayout&gt;然后 使用 FragmentTransaction 动态添加/replace/remove 来自该帧的片段。

在您发布的代码中,在 app_bar_main.xml 内,我看到了:

<FrameLayout
    android:id="@+id/flContent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="?attr/actionBarSize"/>

这显然是您的片段最终将存在的地方,但您必须自己在 Java 代码中将它们添加到框架中。您实际上已经有一个方法可以做到这一点:

private void displaySelectedScreen(int id){
    Fragment fragment = ...

    if(fragment != null){
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.flContent, fragment);
        ft.commit();
    }

    ...
}

看起来这是在您的用户单击导航抽屉中的项目时使用的,但概念是相同的。如果您希望您的应用始终以显示的“朋友”片段启动,您可以将此代码添加到 Activity 的 onCreate() 方法中:

if (savedInstanceState == null) {
    getSupportFragmentManager()
        .beginTransaction()
        .replace(R.id.flContent, new friend())
        .commit();
}

现在您将拥有一个可以使用的 friend 片段!

但是,片段事务不能保证立即执行,因此即使(作为用户)您在应用启动时总是会看到这个 friend 片段,但您不一定能够进行交互在编写此代码后立即使用它(作为程序员)。因此,您还必须将呼叫从您的 onCreate() 方法转移到 populateFriendList()

相反,您应该从friend 片段本身填充好友列表!执行此操作的正确位置可能是片段的 onActivityCreated() 方法内。

要从Fragment 子类中查找视图,您不能像在Activity 中那样只写findViewById()。但是,您可以改为:getView().findViewById()... 只要您在您的 onCreateView() 方法返回后执行此操作。

无论如何,即使我尽量保持简短,这个答案也会变得非常长。我希望这些信息足以让您启动并运行您的应用程序。祝你好运。

【讨论】:

  • 如果您不想在片段中移动populateFriendList(),有一个活动生命周期回调onResumeFragments(),您可以确保您的friend片段已添加、附加和运行.您可以从那里调用populateFriendList()(尽管您必须接受每次活动恢复时调用此方法或编写一些代码以确保它仅在您的活动第一次恢复时运行)。
猜你喜欢
  • 1970-01-01
  • 2019-01-17
  • 2013-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-19
  • 2016-07-24
  • 1970-01-01
相关资源
最近更新 更多