片段的Lis​​tView在将其加载到布局

时间:2017-01-01 23:38:22

标签: android listview android-fragments

我在Android中学习使用Fragment,当用户从导航抽屉中选择一个选项时,我希望将片段加载到MainActivity的布局中。< / p>

我尝试加载的片段有一个ListView,在CustomAdapter的帮助下填充。但加载后,只有列表中的第一项显示在应用程序中。

以下是两种XML的基本结构:

活动的XML:

<android.support.v4.widget.NestedScrollView>
<FrameLayout /> <!--This is the FrameLayout where fragment has to be loaded--> 
</android.support.v4.widget.NestedScrollView>

片段的XML:

<RelativeLayout>
<Switch />
<Switch />
<ProgressBar />
<ListView />
<Button />
</RelativeLayout>

这里是MainActivity中的Java代码:

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

Intent intent;
MainFragment mainFragment;
ProductListFragment productListFragment;

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

    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.setDrawerListener(toggle);
    toggle.syncState();

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


    homeFragment(); //Loads the fragment layout for MainActivity.
}
void homeFragment()
{
    mainFragment = new MainFragment();

    FragmentManager fragmentManager = getFragmentManager();

    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.fragment, mainFragment);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
}

//This function is the function responsible for replace the home fragment with an appropriate one after the users selects an option from the navigation drawer. 
void changeFragment(String collectionName){
    productListFragment = new ProductListFragment();
    productListFragment.getCollectionName(collectionName,getApplicationContext());
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    ProductListFragment fb = new ProductListFragment();
    fragmentTransaction.replace(R.id.fragment, fb);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
}
}

除了这些之外,Activity中还有一些方法(onNavigationItemSelected,onBackPressed,onCreateOptionsMenu,onOptionsItemSelected),但我不认为它们会有用,请告诉我是否需要发布它们

2)我可以将这个片段加载到其他Activity中,但它在MainActivity中不起作用。

3)我认为还有一件事可能很重要,我觉得这是一个白痴,不要把它包括在内。 MainActivity的整个布局包含多个XML。

以下是所有XML: -

activity_nav.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
    layout="@layout/app_bar_nav"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />


<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_nav"
    app:menu="@menu/activity_nav_drawer" />
</android.support.v4.widget.DrawerLayout>

app_bar_nav.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"
android:fitsSystemWindows="true"
tools:context="v5.sharma.com.rig_x.Activity.MainActivity">

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/app_bar_height"
    android:fitsSystemWindows="true"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        android:background="@drawable/cimage">

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

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

<include layout="@layout/content_main" />
</android.support.design.widget.CoordinatorLayout>

content_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 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:id="@+id/content_nav"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="5dp"
android:paddingRight="5dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="v5.sharma.com.rig_x.Activity.MainActivity"
tools:showIn="@layout/app_bar_nav">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/fragment"/>
</android.support.v4.widget.NestedScrollView>

更新 还没有进展,但我认为因为Fragment位于可能以某种方式创建问题的NestedScrollView内部。

0 个答案:

没有答案