【问题标题】:Fragmant not showing in frame layout片段未在框架布局中显示
【发布时间】:2019-12-04 18:21:40
【问题描述】:

我的框架布局没有替换片段。我认为我的布局有一些问题。如果我在 linearlayout 中添加 recyclerview,我可以看到片段变化。但是recyclerview的立场是变化的。请帮忙

主页布局:

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_layout"

android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/container_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"/>



    </LinearLayout>
    <FrameLayout
        android:id="@+id/content_frameone"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


</LinearLayout>

<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#FFFFFF"
    android:choiceMode="singleChoice"
    android:divider="@android:color/darker_gray"
    android:dividerHeight="1dp" />


    </android.support.v4.widget.DrawerLayout>

片段提交代码:

   private void selectItem(int position) {

    Fragment fragment = null;

    switch (position) {
        case 0:
            fragment = new TopNewsFragment();
            break;
        case 1:
            fragment = new FixturesFragment();
            break;
        case 2:
            fragment = new TableFragment();
            break;


        default:
            break;
    }

    if (fragment != null) {

       FrameLayout frameLayout = (FrameLayout) 
        findViewById(R.id.content_frameone);
        frameLayout.removeAllViews();
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.addToBackStack(null);
        ft.replace(R.id.content_frameone, fragment);
        ft.commit();



        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(mNavigationDrawerItemTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);

    } else {
        Log.e("RssfeedActivity", "Error in creating fragment");
    }
}

片段页面代码

  import android.os.Bundle;
 import android.support.v4.app.Fragment;
 import android.view.LayoutInflater;
 import android.view.View;
import android.view.ViewGroup;

 public class TopNewsFragment extends Fragment {

public TopNewsFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
   Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_top_news, container, 
  false);

    return rootView;
}

}

Igf 我在线性布局中添加了 recyclerview,我得到了这样的结果。

【问题讨论】:

  • 尝试将FrameLayout的高度设置为wrap_content
  • 不工作..试过了
  • 片段的布局是什么样的?能否提供 XML 代码?
  • schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" >
  • 它只是空的。只有一个文本视图是错误的。如果我点击我的导航抽屉片段而不是在框架布局中替换

标签: android android-fragments layout android-recyclerview android-framelayout


【解决方案1】:

试试这个

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/container_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <include
                android:id="@+id/toolbar"
                layout="@layout/toolbar"
                android:layout_width="match_parent"
                android:layout_height="56dp" />

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="wrap_content"
                android:layout_height="150dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp" />

        </LinearLayout>

        <FrameLayout
            android:id="@+id/content_frameone"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

    </LinearLayout>

【讨论】:

  • 我试过了。然后 Recyclerview 非常适合。但是我的片段没有替换内部框架布局
  • 你能在这里分享你的片段 xml 吗?
  • schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" >
  • 目前我的片段xml中只有一个文本视图
  • 在片段 xml 父级中添加背景颜色
猜你喜欢
  • 1970-01-01
  • 2020-04-01
  • 1970-01-01
  • 2016-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多