使用片段布局

时间:2018-08-29 04:40:26

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

我是android编程的新手,我创建了一个导航抽屉活动,并添加了滑动以使用Recycler视图刷新,但是当我运行下面的应用程序时,我得到的是这样。

enter image description here

这是我的MainActivity.Java文件:

public class KworldActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
NavigationView navigationView=null;
Toolbar toolbar=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_kworld);

    RssMainFragment fragment=new RssMainFragment();
    android.support.v4.app.FragmentTransaction 
    fragmentTransaction=getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.fragment_container,fragment);
    fragmentTransaction.commit();

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
    this, drawer, toolbar, R.string.navigation_drawer_open, 
    R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) 
    findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

这是我的Fragment.Java文件:

public class RssMainFragment extends Fragment {
RecyclerView recyclerView;
public RssMainFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view =inflater.inflate(R.layout.fragment_rss_main, container, 
false);
    return view;
}

}

这是我的fragment.xml:

<FrameLayout 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"
tools:context="com.karary.university.RssMainFragment">

<android.support.v4.widget.SwipeRefreshLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/swipe_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.karary.university.RSS.Details"
    tools:showIn="@layout/fragment_rss_main">


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

</android.support.v4.widget.SwipeRefreshLayout>
</FrameLayout>

这是我的fragment_container.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.karary.university.KworldActivity">

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

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

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

1 个答案:

答案 0 :(得分:0)

您没有在FrameLayout中定义 layout_behavior

在协调器布局中,我们需要定义子视图的行为

因此在AppBarLayout下,您有一个FrameLayout充当子视图

因此,在该FrameLayout中,您应该定义

app:layout_behavior="@string/appbar_scrolling_view_behavior"

这是修改后的代码。 我测试了这个,它正在工作。让我知道您是否仍然遇到问题

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

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

<FrameLayout
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</FrameLayout>

片段xml

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_container"
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" />