Добрый день всем, я использую левую панель навигации и использую фрагмент для активности в панели навигации. Но проблема в том, что как только я нажимаю на активность, фрагмент не может появиться. Я не уверен, в чем проблема.
Основная деятельность
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nav_layout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
initCollapsingToolbar();
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
informationList = new ArrayList<>();
adapter = new AlbumsAdapter(this, informationList);
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(this,
2);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.addItemDecoration(new GridSpacingItemDecoration(2,
dpToPx(10), true));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
prepareAlbums();
//Cover Pic
try {
Glide.with(this).load(R.drawable.project1_cover).into((ImageView)
findViewById(R.id.backdrop));
} catch (Exception e) {
e.printStackTrace();
}
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);
setupDrawerContent(navigationView);
}
public void selectedItemDrawer(MenuItem menuItem){
Fragment myFragment = null;
Class fragment = null;
switch(menuItem.getItemId()){
case R.id.checkIn:
Toast.makeText(getApplicationContext(), "Check-In",
Toast.LENGTH_SHORT).show();
break;
case R.id.checkOut:
Toast.makeText(getApplicationContext(), "Check-Out",
Toast.LENGTH_SHORT).show();
break;
case R.id.applyOff:
break;
case R.id.reportBug:
break;
case R.id.manageProfile:
fragment = fragment_manageProfile.class;
break;
case R.id.logout:
break;
default:
break;
}
try{
myFragment = (Fragment)fragment.newInstance();
}catch(Exception e){
e.printStackTrace();
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frameLayout,
myFragment).addToBackStack(null).commit();
menuItem.setChecked(true);
drawer.closeDrawers();
}
content_main xml File
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:background="@color/viewBg"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="info.androidhive.kopilim.MainActivity"
tools:showIn="@layout/activity_main">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:scrollbars="vertical" />
</RelativeLayout>
файл app_bar_main 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=".MainActivity">
<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" />
</android.support.design.widget.CoordinatorLayout>
x_ файл file_main
<?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:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="@dimen/detail_backdrop_height"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:expandedTitleTextAppearance="@android:color/transparent"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/backdrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />
</RelativeLayout>
<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/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frameLayout"
android:background="@drawable/login_background">
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
PS fragLayout в данный момент находится в моем файле activity_main.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="info.androidhive.kopilim.fragment_manageProfile">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="35dp"
android:textSize="22dp"
android:textColor="#10019f"
android:textStyle="bold"
android:text="@string/manageProfile"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/empEmail"
android:text="@string/email"
android:layout_margin="10dp"
android:textSize="18dp"
android:gravity="center_vertical"
android:textColor="@color/colorAccent"
/>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="@color/colorAccent"
android:layout_margin="5dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/empID"
android:hint="@string/profileID"
android:textColor="@color/colorAccent"
/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="@color/colorAccent"
android:layout_margin="5dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/empName"
android:hint="@string/profileName"
android:textColor="@color/colorAccent"
/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="@color/colorAccent"
android:layout_margin="5dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/empIC"
android:hint="@string/profileIC"
android:textColor="@color/colorAccent"
/>
</android.support.design.widget.TextInputLayout>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/radioGroup"
android:layout_margin="5dp"
android:orientation="horizontal"
android:weightSum="1"
>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/male"
android:id="@+id/maleRadio"
android:textColor="@color/colorAccent"
android:layout_weight="0.3"
android:checked="true" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/female"
android:id="@+id/femaleRadio"
android:textColor="@color/colorAccent"
android:layout_weight="0.3"/>
</RadioGroup>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="@color/colorAccent"
android:layout_margin="5dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/empPh"
android:hint="@string/profileHp"
android:textColor="@color/colorAccent"
/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="@color/colorAccent"
android:layout_margin="5dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/empAddress"
android:hint="@string/profileAddress"
android:textColor="@color/colorAccent"
/>
</android.support.design.widget.TextInputLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:id="@+id/profileBtn"
android:text="@string/updateProfileBtn"
android:background="@color/buttonLogin"/>
</LinearLayout>
</ScrollView>
</FrameLayout>
фрагмент java класса
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_manage_profile, container,
false);
....
....
//my activity
return v;
}