Not working scrolling of RecyclerView in layout extended from base activity - android

I need some help with my android application. The problem I had faced is that I had created Base activity, that controls Drawer Layout and toolbar. When I am extending it in activity for Relative Layout and nested Recycle View, I get a problem that a can't scroll it (But, when activity is starting, I can catch the moment and do it, but one time). Another situation - I opened an application without extending Base activity in my activity for RecycleView and it works okay.
RecyclerView:
<?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"
tools:context=".EntryList"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:id="#+id/entry_list">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:padding="4dp"
android:scrollbars="vertical"
app:layoutManager="android.support.v7.widget.LinearLayoutManager" />
</RelativeLayout>
Main activity (Drawer):
<?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_main"
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_main"
app:menu="#menu/activity_main_drawer" />
AppBar included to Drawer:
<?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:id="#+id/coordinator_main"
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="match_parent"
android:background="#color/colorWhite"
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="#drawable/gradient_color"
app:popupTheme="#style/AppTheme.PopupOverlay" />
<FrameLayout
android:id="#+id/activity_content"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
Base Activity:
public abstract class BaseActivity extends AppCompatActivity {
protected Context context;
protected Toolbar toolbar;
private DrawerLayout drawer;
private ProgressBar loadingProgressBar;
private NavigationView navigationView;
private ActionBarDrawerToggle toggle;
#Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
context = BaseActivity.this;
setContentView(R.layout.activity_main);
}
#Override
public void setContentView(int layoutResID) {
DrawerLayout fullView = (DrawerLayout)getLayoutInflater().inflate(R.layout.activity_main, null);
FrameLayout activityContainer = fullView.findViewById(R.id.activity_content);
getLayoutInflater().inflate(layoutResID, activityContainer, true);
super.setContentView(fullView);
initToolBar();
}
private void initToolBar() {
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
#Override
protected void onPostCreate(#Nullable Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
setUpNavigation();
toggle.syncState();
}
private void setUpNavigation() {
drawer = findViewById(R.id.drawer_layout);
toggle = new ActionBarDrawerToggle(BaseActivity.this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
ActionBar actionBar = getSupportActionBar();
if (actionBar == null) {
throw new NullPointerException("ActionBar is null");
}
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
// Handle navigation view item clicks here.
if (item.isChecked())
item.setChecked(false);
else
item.setChecked(true);
drawer.closeDrawers();
Intent intent;
switch (item.getItemId()) {
case R.id.nav_home:
intent = new Intent(BaseActivity.this, EntryList.class);
startActivity(intent);
return true;
}
return false;
}
});
toggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
toggle.onConfigurationChanged(newConfig);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (toggle.onOptionsItemSelected(item))
return true;
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
Adapter:
public class EntryListAdapter extends RecyclerView.Adapter<EntryListAdapter.EntryListViewHolder> {
private List<ExampleEntryItem> items;
public static class EntryListViewHolder extends RecyclerView.ViewHolder {
private TextView specieNameRuTextView;
private TextView entryDateTextView;
private TextView specieNameLatTextView;
private TextView entryPlaceTextView;
public EntryListViewHolder(#NonNull View itemView) {
super(itemView);
specieNameRuTextView = itemView.findViewById(R.id.specie_name_ru);
entryDateTextView = itemView.findViewById(R.id.entry_date);
specieNameLatTextView = itemView.findViewById(R.id.specie_name_lat);
entryPlaceTextView = itemView.findViewById(R.id.entry_place);
}
}
public EntryListAdapter(List<ExampleEntryItem> items) {
this.items = items;
}
#NonNull
#Override
public EntryListViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.example_entry_item, parent, false);
return new EntryListViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull EntryListViewHolder entryListViewHolder, int position) {
ExampleEntryItem item = this.items.get(position); // TODO: 06.05.2019 test for null
entryListViewHolder.entryPlaceTextView.setText(item.getEntryPlace());
entryListViewHolder.entryDateTextView.setText(item.getEntryDate());
entryListViewHolder.specieNameRuTextView.setText(item.getSpecieNameRu());
entryListViewHolder.specieNameLatTextView.setText(item.getSpecieNameLat());
}
#Override
public int getItemCount() {
return this.items.size();
}
RecycleView control class:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.entry_list_content_main);
List<ExampleEntryItem> items = new ArrayList<>();
adding items...
RecyclerView recyclerView = findViewById(R.id.recycler_view);
RecyclerView.Adapter adapter = new EntryListAdapter(items);
adapter.notifyDataSetChanged();
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
}
Thanks, everybody, who will help me

You first need to remove
adapter.notifyDataSetChanged()
After this if you use recycler view inside nested scrolling
You need to add a single line
recyclerView.setNestwdScrollingenable(false)

I resolved the problem. I replace my logic for recyclerview in frament, and mainly change, that I do is - in layout with appbar, I replaced <include layout="#layout/content"/> to outside of AppBarLayout and change line:
android:layout_height="match_parent"
android:background="#color/colorWhite"
to this:
android:layout_height="wrap_content"

Related

Action Bar title and navigation drawer icon missing

In MainPage, it has NavigationDrawer and BottomNavigationBar. When I clicked Home, the navigation drawer icon and action bar title missing.
MainPage
public class MainPage extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_page);
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);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
navigation.setSelectedItemId(R.id.navigation_home);
}
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment;
switch (item.getItemId()) {
case R.id.navigation_home:
fragment = new HomePage();
loadFragment(fragment);
return true;
case R.id.navigation_dashboard:
return true;
case R.id.navigation_notifications:
return true;
}
return false;
}
};
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the main; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private void loadFragment(Fragment fragment) {
// load fragment
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.rl, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
HomePage
public class HomePage extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.app_bar_main, container, false);
RecyclerView mRecyclerView = view.findViewById(R.id.recycleView);
GridLayoutManager mGridLayoutManager = new GridLayoutManager(getActivity(), 2);
mRecyclerView.setLayoutManager(mGridLayoutManager);
mRecyclerView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
List <Book> bookList= new ArrayList<>();
Book book = new Book();
book = new Book("Introduction to Java Programming", 15.00, "4.5", Uri.parse("android.resource://com.example.seng.sechandapp/drawable/book").toString());
bookList.add(book);
book = new Book("Java", 15.00, "4.5", Uri.parse("android.resource://com.example.seng.sechandapp/drawable/book").toString());
bookList.add(book);
HomePageAdapter homePageAdapter = new HomePageAdapter(getActivity(), bookList);
mRecyclerView.setAdapter(homePageAdapter);
return view;
}
}
app_bar_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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.seng.sechandapp.MainPage">
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
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="#color/blueviolet"
app:popupTheme="#style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/home_page"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
home_page
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recycleView">
</android.support.v7.widget.RecyclerView>
</FrameLayout>
Try this
Make below Changes in you code
First Change
Use this in your HomePage Fragment
View view = inflater.inflate(R.layout.home_page, container, false);
instead of
View view = inflater.inflate(R.layout.app_bar_main, container, false);
Second Change
Now in your home_page add android:layout_marginTop="?actionBarSize" in your FrameLayout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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_marginTop="?actionBarSize"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycleView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
Some Other suggestion
Use implementation instead of compile in your build.gradle
because
Configuration compile is obsolete and has been replaced with implementation.
It will be removed at the end of 2018
You are inflating app_bar_main.xml instead of home_page.xml inside the Fragment. Replace View view = inflater.inflate(R.layout.app_bar_main, container, false); with View view = inflater.inflate(R.layout.home_page, container, false);. And where is the view with ID R.id.rl?

When I am adding fragment to my view pager (in nested scroll view), I can not exit from application with device back button

This is my activity_main code:
<?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"
android:layoutDirection="rtl"
tools:context=".MainActivity"
tools:openDrawer="start">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:paddingTop="#dimen/appbar_padding_top"
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"
android:layoutDirection="rtl"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/AppTheme.PopupOverlay"
app:title="#string/app_name">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layoutDirection="rtl" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<com.duolingo.open.rtlviewpager.RtlViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layoutDirection="rtl"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layoutDirection="rtl"
app:headerLayout="#layout/drawer_header"
app:menu="#menu/menu_drawer" />
</android.support.v4.widget.DrawerLayout>
I want to add some fragments to ViewPager in NestedScrollView.
Without adding fragments to ViewPager everything is ok and I can exit from app by device back button, but when I am adding fragment(s) to ViewPager I can not exit from app by pressing device back button.(Back button is working and for example can close soft keyboard)
this is my fragment code:
(empty fragment!)
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable
ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_jadvalyab,container,false);
}
and this is my adapter:
public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
private ArrayList<Fragment> fragmentsList = new ArrayList<>();
private ArrayList<String> fragsTitleList = new ArrayList<>();
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
public void addFragment(Fragment fragment,String title){
fragmentsList.add(fragment);
fragsTitleList.add(title);
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
return fragsTitleList.get(position);
}
#Override
public Fragment getItem(int position) {
return fragmentsList.get(position);
}
#Override
public int getCount() {
return fragmentsList.size();
}
}
Does anyone have an idea to solving this problem?
Thanks.
Edit:
Problem solved by removing NestedScrollingView from coordinator layout in activity_main and putting that in fragment layout (inside viewpager).
But still I don't know why device back button did not worked in former state.
I think the problem is occurring because you have used a third party library for ViewPager.
Just copy and paste the following code in the place of your "activity_main.xml" file.
<?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">
<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=".Main2Activity">
<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.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main2" />
<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>
<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/drawer_header"
app:menu="#menu/menu_drawer" />
</android.support.v4.widget.DrawerLayout>
I have initialized fragment to ViewPager like this
ViewPager viewPager2 = findViewById(R.id.viewpager);
MyFragmentPagerAdapter pagerAdapter2 = new MyFragmentPagerAdapter(getSupportFragmentManager());
pagerAdapter2.addFragment(TestFragment.createInstance(),"Home");
viewPager2.setAdapter(pagerAdapter2);
TabLayout tabLayout2 = findViewById(R.id.tabs);
tabLayout2.setupWithViewPager(viewPager2);
Here is the Testfragment.java class
public class TestFragment extends Fragment {
public static TestFragment createInstance() {
TestFragment profileFragment = new TestFragment();
return profileFragment;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_jadvalyab, container, false);
return rootView;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
}
}
And the full code of "Main2Activity.java" class is here
public class Main2Activity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
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);
initViewPager();
}
private void initViewPager() {
ViewPager viewPager2 = findViewById(R.id.viewpager);
MyFragmentPagerAdapter pagerAdapter2 = new MyFragmentPagerAdapter(getSupportFragmentManager());
pagerAdapter2.addFragment(TestFragment.createInstance(),"Home");
viewPager2.setAdapter(pagerAdapter2);
TabLayout tabLayout2 = findViewById(R.id.tabs);
tabLayout2.setupWithViewPager(viewPager2);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main2, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
Edit:
Code of "content_main2.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:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.v4.widget.NestedScrollView>
Code for "fragment_jadvalyab.xml" is in te following
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Best Wishes"
android:gravity="center"/>
</LinearLayout>
Try this
#Override
public void onBackPressed() {
//Your Action
}
Override onBackPress(){} event in your MainActivity which holds your above xml layout.
#Override
public void onBackPressed() {
finish();
}
override OnBackPressed() method , or you can write your adapter as this.
public class Adapter extends FragmentStatePagerAdapter {
public Adapter (FragmentManager fm) {
super(fm);
}
// you can add fragments according to your requirement
#Override
public Fragment getItem(int position) {
Fragment fragment= null;
switch (position){
case 0:
fragment= new fragmentDay();
break;
case 1:
fragment= new fragmentWeek();
break;
case 2:
fragment= new fragmentMonth();
break;
}
return fragment;
}
#Override
public int getCount() {
return 3;
}
}
and inside activity pass fragment manager to adapter constructor.
fragmentManager= getSupportFragmentManager();
adapter= new Adapter(fragmentManager);
viewPager.setAdapter(adapter);
I have used tablayout with viewpager
tabLayout.setupWithViewPager(viewPager);
tabLayout.setTabsFromPagerAdapter(adapter);
tabLayout.getTabAt(0).setText("Day");
tabLayout.getTabAt(1).setText("Week");
tabLayout.getTabAt(2).setText("Month");
tabLayout.getTabAt(1).select();
tabLayout.setSelectedTabIndicatorColor(Color.WHITE);

How to make Navigation bar above the fragment page [duplicate]

im new in android im creating an app and i have a problem with a listfragment, because the list its showing but its overlapping the title bar(i had to add margin top to change that), and also im using a drawer and when im trying to open the list it shows over the options drawer too, letme paste you the code and the image so you can understan more:
The xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<ListView
android:layout_width="match_parent"
android:layout_height="420dp"
android:id="#android:id/list"
android:layout_weight="0.82"
android:layout_marginTop="60dp" />
</LinearLayout>
The listFregment code:
public class FragmentoPrincipalChofer extends ListFragment {
private List<ParseObject> mViaje;
private ListView mLista;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View x = inflater.inflate(R.layout.fragmento_principal_chofer, container, false);
mLista = (ListView)x.findViewById(android.R.id.list);
return x;
}
#Override
public void onResume() {
super.onResume();
//obtener pedido de taxi
ParseQuery<ParseObject> query = ParseQuery.getQuery("Viaje");
query.whereEqualTo("chofer", "prueba3");
query.addDescendingOrder("createdAt");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> viajes, com.parse.ParseException e) {
if (e == null) {
mViaje=viajes;
String[] nombreUsuarios = new String[mViaje.size()];
int i=0;
for (ParseObject viaje : mViaje){
nombreUsuarios[i] = viaje.getString("cliente");
i++;
}
ArrayAdapter<String> adaptador = new ArrayAdapter<String>(getListView().getContext(),android.R.layout.simple_list_item_1,nombreUsuarios);
mLista.setAdapter(adaptador);
} else {
}
}
});
}
}
the main drawer code:
public class DrawerPrincipal extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener,FragmentoPrincipalUsuario.OnFragmentInteractionListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer_principal);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//cargar fragment principal usuario
FragmentoPrincipalChofer fragmento=(FragmentoPrincipalChofer) getSupportFragmentManager().findFragmentByTag("fragmento");
if (fragmento==null){
fragmento=new FragmentoPrincipalChofer();
android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(android.R.id.content,fragmento,"fragmento");
transaction.commit();
}
/*
FragmentoPrincipalUsuario principal = new FragmentoPrincipalUsuario();
android.support.v4.app.FragmentTransaction FragmentTransaction =
getSupportFragmentManager().beginTransaction();
FragmentTransaction.replace(R.id.contenedor_principal,principal);
FragmentTransaction.commit();
*/
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();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.pantalla_principal_usuario, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.cuenta) {
// Handle the camera action
} else if (id == R.id.historial_viajes) {
} else if (id == R.id.contacto) {
} else if (id == R.id.compartir) {
} else if (id == R.id.version) {
} else if (id == R.id.salir) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
#Override
public void onFragmentInteraction(Uri uri) {
}
}
list fragment overlapping
here its the activity_drawer_principal.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_drawer_principal"
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_drawer_principal"
app:menu="#menu/activity_pantalla_principal_usuario_drawer" />
</android.support.v4.widget.DrawerLayout>
here its the app_bar_drawer_principal:
<?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=".DrawerPrincipal">
<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" />
<FrameLayout
android:id="#+id/contenedor_principal"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<include layout="#layout/content_drawer_principal" />
</android.support.design.widget.CoordinatorLayout>
your problem is your setting your list fragment to the whole view Id with this line
// android.R.id.content is the WHOLE screen of your Activity
transaction.add(android.R.id.content,fragmento,"fragmento");
transaction.commit();
Create a FrameLayout in your content_drawer_principal.xml:
<FrameLayout android:id="#+id/list_content"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
then do:
transaction.add(R.id.list_content,fragmento,"fragmento");
transaction.commit();
UPDATE
The real problem here is that your telling your FragmentTransaction to load your FragmentoPrincipalChofer into android.R.id.content which is a reserved id
android.R.id.content gives you the root element of a view, without having to know its actual name/type/ID.
Revers the order of your activity layout, put list view brfore any other main content

Navigation Drawer: setNavigationItemSelectedListener is not working.

Here, This is my implementation in java:
import static com.example.krsnv.kooc.R.id.nav_gro;
public class Displayprofile extends AppCompatActivity {
private FirebaseRecyclerAdapter<User, userholder> mAdapter;
RecyclerView mRecyclerView;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.displayprofile);
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawerlayout) ;
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
// getSupportActionBar().setDisplayHomeAsUpEnabled(true);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener(){
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
int id = menuItem.getItemId();
if (id == R.id.nav_account) {
Toast.makeText(Displayprofile.this, "my account",Toast.LENGTH_LONG).show();
}
else if (id == R.id.nav_settings)
{
Toast.makeText(Displayprofile.this,"settings",Toast.LENGTH_SHORT).show();
}
else if (id == R.id.nav_logout)
{
Toast.makeText(Displayprofile.this,"logout",Toast.LENGTH_SHORT).show();
}
else if (id == nav_gro)
{
Toast.makeText(Displayprofile.this,"groceries",Toast.LENGTH_SHORT).show();
}
return true;
}
} );
DatabaseReference mRef = FirebaseDatabase.getInstance().getReference().child("workers");
mRecyclerView = (RecyclerView) findViewById(R.id.userrecycler);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mAdapter = new FirebaseRecyclerAdapter<User, userholder>(User.class, android.R.layout.two_line_list_item, userholder.class, mRef) {
#Override
public void populateViewHolder(userholder userViewHolder, User users, int position) {
userViewHolder.setName(users.getName());
userViewHolder.setText(users.getPhone());
}
};
mRecyclerView.setAdapter(mAdapter);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mToggle.onOptionsItemSelected(item))
{
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onDestroy() {
super.onDestroy();
mAdapter.cleanup();
}
public static class userholder extends RecyclerView.ViewHolder {
private final TextView mNameField;
private final TextView mTextField;
public userholder(View itemView) {
super(itemView);
mNameField = (TextView) itemView.findViewById(android.R.id.text1);
mTextField = (TextView) itemView.findViewById(android.R.id.text2);
}
public void setName(String name) {
mNameField.setText(name);
}
public void setText(String text) {
mTextField.setText(text);
}
}
}
The xml code is given below:
>
<ScrollView
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"
android:background="#cecece"
>
<LinearLayout 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"
android:padding="40dp"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="3"
android:layout_gravity="center"
android:gravity="center"
android:textSize="18sp"
android:text="find chef's near you\n"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/userrecycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:padding="16dp"
/>
</LinearLayout>
</ScrollView>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="#menu/navigation_menu"
android:layout_gravity="start"
app:headerLayout="#layout/navigation_header"
android:id="#+id/nav_view">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
When I click an item in the navigation drawer it doesn't show the Toast message. Here, setNavigationItemSelectedListener is not working So, what can be done to solve this issue ?
In order to use this setNavigationItemSelectedListener you should have menu as give below in res->menu folder
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_payment"
android:checked="false"
android:icon="#drawable/ic_action_payment"
android:title="#string/menu_payment" />
<item
android:id="#+id/menu_history"
android:checked="false"
android:icon="#drawable/ic_action_history"
android:title="#string/menu_history" />
</menu>
Add this
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.navigation_menu, menu);
return true;
}

listfragment overlapping my main drawer

im new in android im creating an app and i have a problem with a listfragment, because the list its showing but its overlapping the title bar(i had to add margin top to change that), and also im using a drawer and when im trying to open the list it shows over the options drawer too, letme paste you the code and the image so you can understan more:
The xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<ListView
android:layout_width="match_parent"
android:layout_height="420dp"
android:id="#android:id/list"
android:layout_weight="0.82"
android:layout_marginTop="60dp" />
</LinearLayout>
The listFregment code:
public class FragmentoPrincipalChofer extends ListFragment {
private List<ParseObject> mViaje;
private ListView mLista;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View x = inflater.inflate(R.layout.fragmento_principal_chofer, container, false);
mLista = (ListView)x.findViewById(android.R.id.list);
return x;
}
#Override
public void onResume() {
super.onResume();
//obtener pedido de taxi
ParseQuery<ParseObject> query = ParseQuery.getQuery("Viaje");
query.whereEqualTo("chofer", "prueba3");
query.addDescendingOrder("createdAt");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> viajes, com.parse.ParseException e) {
if (e == null) {
mViaje=viajes;
String[] nombreUsuarios = new String[mViaje.size()];
int i=0;
for (ParseObject viaje : mViaje){
nombreUsuarios[i] = viaje.getString("cliente");
i++;
}
ArrayAdapter<String> adaptador = new ArrayAdapter<String>(getListView().getContext(),android.R.layout.simple_list_item_1,nombreUsuarios);
mLista.setAdapter(adaptador);
} else {
}
}
});
}
}
the main drawer code:
public class DrawerPrincipal extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener,FragmentoPrincipalUsuario.OnFragmentInteractionListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer_principal);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//cargar fragment principal usuario
FragmentoPrincipalChofer fragmento=(FragmentoPrincipalChofer) getSupportFragmentManager().findFragmentByTag("fragmento");
if (fragmento==null){
fragmento=new FragmentoPrincipalChofer();
android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(android.R.id.content,fragmento,"fragmento");
transaction.commit();
}
/*
FragmentoPrincipalUsuario principal = new FragmentoPrincipalUsuario();
android.support.v4.app.FragmentTransaction FragmentTransaction =
getSupportFragmentManager().beginTransaction();
FragmentTransaction.replace(R.id.contenedor_principal,principal);
FragmentTransaction.commit();
*/
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();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.pantalla_principal_usuario, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.cuenta) {
// Handle the camera action
} else if (id == R.id.historial_viajes) {
} else if (id == R.id.contacto) {
} else if (id == R.id.compartir) {
} else if (id == R.id.version) {
} else if (id == R.id.salir) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
#Override
public void onFragmentInteraction(Uri uri) {
}
}
list fragment overlapping
here its the activity_drawer_principal.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_drawer_principal"
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_drawer_principal"
app:menu="#menu/activity_pantalla_principal_usuario_drawer" />
</android.support.v4.widget.DrawerLayout>
here its the app_bar_drawer_principal:
<?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=".DrawerPrincipal">
<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" />
<FrameLayout
android:id="#+id/contenedor_principal"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<include layout="#layout/content_drawer_principal" />
</android.support.design.widget.CoordinatorLayout>
your problem is your setting your list fragment to the whole view Id with this line
// android.R.id.content is the WHOLE screen of your Activity
transaction.add(android.R.id.content,fragmento,"fragmento");
transaction.commit();
Create a FrameLayout in your content_drawer_principal.xml:
<FrameLayout android:id="#+id/list_content"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
then do:
transaction.add(R.id.list_content,fragmento,"fragmento");
transaction.commit();
UPDATE
The real problem here is that your telling your FragmentTransaction to load your FragmentoPrincipalChofer into android.R.id.content which is a reserved id
android.R.id.content gives you the root element of a view, without having to know its actual name/type/ID.
Revers the order of your activity layout, put list view brfore any other main content

Categories

Resources