View Pager is not working inside nested scroll view in android - android

Hello people I'm a newbie to android development and I'm stuck at this point where my view pager is not working as expected.
What I want to achieve is a tab layout scrolling up along with a collapsing toolbar layout. Thanks a lot :)
Without the nested scroll view the problem is that that collapsing toolbar is not collapsing.This is the code :
<?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.support.design.widget.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
app:theme="#style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
app:contentScrim="#android:color/black"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:src="#drawable/bottom_nav_home_icon"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.1" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<android.support.design.widget.TabLayout
android:id="#+id/temp_tab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:theme="#style/TabLayoutTheme" />
<android.support.v4.view.ViewPager
android:id="#+id/temp_view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
This is working well for view pager but the collapsing toolbar is not collapsing.When I tried to keep it inside a nested scroll view the collapsing toolbar layout is collapsing but view pager is not working. It is not sliding the tabs.
The java code for view pager is
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
public class Tabs extends AppCompatActivity {
TabLayout tabLayout;
ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_service_history_tabs);
tabLayout=(TabLayout)findViewById(R.id.temp_tab);
viewPager=(ViewPager)findViewById(R.id.temp_view_pager);
tabLayout.setupWithViewPager(viewPager);
viewPager.setAdapter(new PageAdapter(getSupportFragmentManager()));
}
class PageAdapter extends FragmentPagerAdapter{
public PageAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return new TabFragment1();
}
#Override
public int getCount() {
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0:return "A";
default:return "B";
}
}
}
}

Try putting your LinearLayout inside a NestedScrollView,
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:id="#+id/nsv">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="#+id/temp_tab"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v4.view.ViewPager
android:id="#+id/temp_view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
and setting android:fillViewport to true for the NestedScrollView.
NestedScrollView nestedScrollView = (NestedScrollView) findViewById (R.id.nsv);
nestedScrollView.setFillViewport (true);

i think
android:fitsSystemWindows="true" is the cause. Remove this line.May solve your issue.

Related

com.google.android.material.appbar.AppBarLayout$LayoutParams cannot be cast to androidx.coordinatorlayout.widget.CoordinatorLayout$LayoutParams

I've used CoordinatorLayout + AppBarLayout so the Toolbar could disappear while scrolling and to put Recycler View below tablayout. But after I've changed model logic and added child elements in the toolbar this fatal error keeps happening. After changing the root layout to LinearLayout or FrameLayout the error disappears, but I don't get desired behavior.
I don't call, create or change my Layout params for AppBarLayout and CoordinatorLayout in the code (MainActivity). So the stack trace does not show me any internal lines of code, just the internal classes logic.
Here is the XML:
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/container"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:contentInsetStart="0dp"
app:layout_scrollFlags="scroll|snap"
app:titleTextColor="#android:color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/toolbarBackButton"
android:layout_width="?attr/actionBarSize"
android:layout_height="?attr/actionBarSize"
android:visibility="invisible"/>
<TextView
android:id="#+id/toolbarText"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#color/colorPrimary"
android:fontFamily="sans-serif-medium"
android:gravity="center_vertical"
android:text="#string/activity_main_toolbar_title"
android:textColor="#android:color/white"
android:textSize="20sp" />
<ImageView
android:id="#+id/toolbarBucket"
android:layout_width="?attr/actionBarSize"
android:layout_height="?attr/actionBarSize" />
</LinearLayout>
</androidx.appcompat.widget.Toolbar>
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabBackground="#color/colorPrimary"
app:tabIndicatorColor="#color/colorIndicator"
app:tabSelectedTextColor="#color/colorActive"
app:tabTextColor="#color/colorInactive" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_anchor="#id/tabs"
app:layout_anchorGravity="bottom"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/add_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="#dimen/regularMargin"
android:src="#drawable/ic_baseline_add_24"
app:tint="#android:color/white"/></androidx.coordinatorlayout.widget.CoordinatorLayout>
Any clues will be much appreciated!
My main activity code
package com.example.loftmoney.screens.main;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;
import android.content.Intent;
import android.os.Bundle;
import com.example.loftmoney.R;
import com.example.loftmoney.model.Item;
import com.example.loftmoney.screens.additem.AddItemActivity;
import com.example.loftmoney.screens.balance.BalanceFragment;
import com.example.loftmoney.screens.budget.BudgetFragment;
import com.example.loftmoney.screens.main.adapter.FragmentItem;
import com.example.loftmoney.screens.main.adapter.MainPagerAdapter;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements MainClickAdapter, EditModeListener {
private ViewPager2 viewPager;
private Toolbar toolbar;
TabLayout tabs;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupFab();
setupTabs();
setupToolbar();
}
private void setupToolbar() {
toolbar = findViewById(R.id.toolbar);
}
private void setupTabs() {
List<FragmentItem> fragments = setupFragments();
tabs = findViewById(R.id.tabs);
viewPager = findViewById(R.id.viewPager);
MainPagerAdapter adapter = new MainPagerAdapter(fragments, this, 0);//behavior?
viewPager.setOffscreenPageLimit(fragments.size());
viewPager.setAdapter(adapter);
new TabLayoutMediator(tabs, viewPager,
(tab, position) -> tab.setText(
fragments.get(position).getTitle()
)
).attach();
}
private List<FragmentItem> setupFragments() {
List<FragmentItem> fragments = new ArrayList();
fragments.add(new FragmentItem(new BudgetFragment(), getString(R.string.expenses), Item.ItemType.EXPENSE));
fragments.add(new FragmentItem(new BudgetFragment(), getString(R.string.incomes), Item.ItemType.INCOME));
fragments.add(new FragmentItem(new BalanceFragment(), getString(R.string.balance)));
return fragments;
}
private Fragment getActiveFragment() {
final int activeFragmentIndex = viewPager.getCurrentItem();
Fragment activeFragment = (BudgetFragment) getSupportFragmentManager().getFragments().get(activeFragmentIndex);
return activeFragment;
}
private void setupFab() {
FloatingActionButton addFab = findViewById(R.id.add_fab);
addFab.setOnClickListener(v -> onFabClick());
}
#Override
public void onFabClick() {
BudgetFragment activeFragment = (BudgetFragment) getActiveFragment();
Intent intent = new Intent(MainActivity.this, AddItemActivity.class).putExtra("type", activeFragment.type);
activeFragment.startActivityForResult(intent, BudgetFragment.LAUNCH_ADD_ITEM);
}
#Override
public void onEditModeChangeListener(boolean status) {
toolbar.setBackgroundColor(getApplicationContext().getColor(
status ? R.color.editModeColor : R.color.colorPrimary));
}
}
This was because I've set up an anchor.
I've deleted these 2 lines and all works fine
app:layout_anchor="#id/tabs"
app:layout_anchorGravity="bottom"

Fragment content not visible on adding it to view pager of an activity?

I have put viewpager in an activity and view pager contains 3 fragments but on running app it doesn't show the fragment related data.Following is the aatached code, please help.
Its not throwing any error but the fragment content is not visible though the tab-titles are visible. I saw certain links where they ask you to add getChildFragmentManager() but after searching i got to know that its not supported in activity.
MainActivity.java
package kbg.com.kbgpos;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
DrawerLayout drawerLayout;
ActionBarDrawerToggle actionBarDrawerToggle;
TabLayout tabLayout;
ViewPager viewPager;
ViewPagerAdapter viewPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar=(Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawerLayout=(DrawerLayout) findViewById(R.id.drawer_layout);
actionBarDrawerToggle=new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.drawer_open,R.string.drawer_close);
actionBarDrawerToggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.hamburgerColor));
drawerLayout.setDrawerListener(actionBarDrawerToggle);
tabLayout=(TabLayout) findViewById(R.id.tabLayout);
viewPager=(ViewPager) findViewById(R.id.viewPager);
viewPagerAdapter=new ViewPagerAdapter(getSupportFragmentManager());
viewPagerAdapter.addFragments(new Personal_Info_Fragment(),"Personal Info");
viewPagerAdapter.addFragments(new EducationalExperience_Info_Fragment(),"Edu & Exp Info");
viewPagerAdapter.addFragments(new OtherInfo_Fragment(),"Official Info");
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
}
#Override
protected void onPostCreate(#Nullable Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
actionBarDrawerToggle.syncState();
}
}
activity_main.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="#+id/drawer_layout">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include
android:layout_height="wrap_content"
android:layout_width="match_parent"
layout="#layout/toolbar_layout"
android:id="#+id/toolbar"></include>
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tabLayout"
app:tabMode="fixed"
app:tabGravity="fill"
android:background="#b3d9ff"
app:tabTextAppearance="#style/TabLayoutStyle">
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/viewPager">
</android.support.v4.view.ViewPager>
<android.support.design.widget.NavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/navigation_view"
android:layout_gravity="start"
app:headerLayout="#layout/navigation_drawer_header"
app:itemIconTint="#006699"
app:menu="#menu/drawer_menu">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
Personal_Info_Fragment
package kbg.com.kbgpos;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Personal_Info_Fragment extends Fragment {
public Personal_Info_Fragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_personal__info_, container, false);
}
}
ViewPagerAdapter.java
package kbg.com.kbgpos;
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.ArrayList;
public class ViewPagerAdapter extends FragmentPagerAdapter {
ArrayList<Fragment> fragments=new ArrayList<>();
ArrayList<String> tabTitles=new ArrayList<>();
public void addFragments(Fragment fragments,String titles){
this.fragments.add(fragments);
this.tabTitles.add(titles);
}
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return fragments.get(position);
}
#Override
public int getCount() {
return fragments.size();
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
return tabTitles.get(position);
}
}
fragment_personal__info_.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".Personal_Info_Fragment">
<android.support.v7.widget.CardView
android:id="#+id/info_cardView"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_margin="5dp"
card_view:cardElevation="2dp"
card_view:contentPadding="5dp"
card_view:cardCornerRadius="2dp"
card_view:cardBackgroundColor="#e5c68b">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/infoIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_info_black_24dp"
android:layout_centerVertical="true"
android:layout_marginRight="2dp"/>
<TextView
android:layout_toRightOf="#id/infoIcon"
android:layout_marginLeft="4dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Please Enter Complete and Valid Information Within the Forms As This Will Be Treated As Final Info"
android:textColor="#android:color/white"
android:textSize="14sp"
android:textAllCaps="true"
android:textStyle="bold"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</FrameLayout>
Please help as how can i see the content of fragments on viewpager tabs which are shown empty at the moment.
A DrawerLayout is supposed to have 2 children, the main content and the navigation view.
In your case you have 3 children:
<DrawerLayout ...>
<AppBarLayout />
<ViewPager />
<NavigationView />
</DrawerLayout>
So try to wrap the AppBarLayout and ViewPager into a LinearLayout so that in the end the DrawerLayout to have just 2 children as expected.
<DrawerLayout ...>
<LinearLayout orientation="vertical">
<AppBarLayout />
<ViewPager />
</LinearLayout>
<NavigationView />
</DrawerLayout>

Android Fragments not showing up Text view and internal elements

I am new to android.I created a tablayout and a viewpager with two fragments.I have elements/items in my created Detail Fragment and a TextView in my measure fragment but when i run the code on my mobile,nothing is shown in my fragment.I haven't java code of my fragment.Everything works fine on the tablayout..i can switch between the tab but nothing is being shown in my fragments...why is that happening?
My Activity:
<?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="com.example.update">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
>
<!--<include android:layout_height="wrap_content"
android:layout_width="match_parent"
layout="#layout/toolbar_layout"/>-->
<include
android:id="#+id/update_screen_toolbar"
layout="#layout/toolbar_layout"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:id="#+id/name_age_gender"
android:background="#d3d3d3"
android:layout_below="#+id/update_screen_toolbar"/>
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tabLayout"
app:tabMode="fixed"
app:tabGravity="fill">
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/viewPager">
</android.support.v4.view.ViewPager>
</RelativeLayout>
JAVA code for activity
package com.example.update;
import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class UpdateDetail extends AppCompatActivity {
TabLayout tabLayout;
ViewPager viewPager;
ViewPagerAdapter viewPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
tabLayout=(TabLayout)findViewById(R.id.tabLayout);
viewPager=(ViewPager)findViewById(R.id.viewPager);
viewPagerAdapter=new ViewPagerAdapter(getSupportFragmentManager());
viewPagerAdapter.addFragments(new DetailFragment()," DETAIL");
viewPagerAdapter.addFragments(new MeasureFragment(),"MEASURE");
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
}
}
code for DetailFragment:
<FrameLayout 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.DetailFragment">
<!-- TODO: Update blank fragment layout -->
<Button
android:id="#+id/expandableButton4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#066da1"
android:drawableRight="#android:drawable/arrow_down_float"
android:onClick="expandableButton4"
android:paddingRight="10dp"
android:text="Expand/Collapse Android Example"
android:textColor="#fff" />
<com.github.aakira.expandablelayout.ExpandableRelativeLayout
android:id="#+id/expandableLayout4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/expandableButton4"
android:background="#90066da1"
android:padding="16dp"
app:ael_duration="400"
app:ael_expanded="false"
app:ael_interpolator="bounce"
app:ael_orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Implements the expand and collapse by sliding logic for a top or a bottom view in a two children view or layout or any widgets composition.
Implements the expand and collapse by sliding logic for a top or a bottom view in a two children view or layout or any widgets composition." />
</com.github.aakira.expandablelayout.ExpandableRelativeLayout>
</FrameLayout>
Try this one:
public class FriendshipParentFragment extends Fragment {
#BindView(R.id.tabs)
TabLayout tabs;
#BindView(R.id.viewpager)
ViewPager viewPager;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_friendship_parent, container, false);
ButterKnife.bind(this, view);
initTabs();
return view;
}
private void initTabs(){
viewPager.setAdapter(new TabsAdapter(getChildFragmentManager()));
tabs.setupWithViewPager(viewPager);
tabs.getTabAt(0).setText("Tab1");
tabs.getTabAt(1).setText("Tab2");
tabs.setTabTextColors(ContextCompat.getColor(getActivity(), R.color.colorAccent), ContextCompat.getColor(getActivity(), R.color.colorBottomNavigationPrimary));
tabs.setSelectedTabIndicatorColor(ContextCompat.getColor(getActivity(), R.color.white));
tabs.setSelectedTabIndicatorHeight(4);
}
public class TabsAdapter extends FragmentPagerAdapter {
public TabsAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new FollowersFragment();
case 1:
return new FollowFragment();
}
return null;
}
#Override
public int getCount() {
return 2;
}
}
}
xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="owo.owocar.driver.OrdersParentFragment">
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="48dp"
android:elevation="4dp"
app:tabGravity="fill" />
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:id="#+id/viewpager"
android:layout_height="match_parent"></android.support.v4.view.ViewPager>

Scrolling to view in CoordinatorLayout shows only partially the view

I have made an Android app containing a CoordinatorLayout with a AppBar and a NestedScrollView. In my NestedScrollView I have a list of items, and a button which scroll to an item in my NestedScrollView.
When clicking the button, the Android app scroll down to the item, but doesn't show the whole item, only partially: http://i.stack.imgur.com/8kuq0.png
I expected something like the following: http://i.stack.imgur.com/k0A5N.png
It seems like the amount the app needs to scroll is about the same size as the AppBar, in order to show the whole view. If I remove the scroll flag in my layout file below, I get the expected behavior.
What do I do wrong?
Update (12/01/2016): I've updated the pictures, so they are bigger. Furthermore as I wrote in Herry's response, I'm in doubt if View.requestRectangleOnScreen() is the right method to call, or if I should use a different layout then NestedScrollView.
My activity is coded as follows:
package com.example.sij.coordinatorlayoutbug;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView itemToNavigateTo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
itemToNavigateTo = (TextView)findViewById(R.id.itemToNavigateTo);
Button navigatorButton = (Button)findViewById(R.id.navigator_button);
navigatorButton.setOnClickListener(navigateToItemListener());
}
View.OnClickListener navigateToItemListener() {
return new View.OnClickListener() {
#Override
public void onClick(final View v) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
#Override
public void run() {
itemToNavigateTo.requestRectangleOnScreen(
new Rect(
0,
0,
itemToNavigateTo.getWidth(),
itemToNavigateTo.getHeight()));
}
});
}
};
}
}
And the layout file:
<?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.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/navigator_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/scroll_to_item" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/item1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/lorem_ipsum"
android:paddingBottom="20dp"/>
<!-- Items 2 to 5 omitted for brevity -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/item6"/>
<TextView
android:id="#+id/itemToNavigateTo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/lorem_ipsum"
android:paddingBottom="20dp"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
you need to add android:fillviewport="true" in your
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:fillViewport="true">

ViewPager doesn't swipe with FragmentPagerAdapter

I have two fragments in an Activity, In portrait mode the first one (ListFragment) is shown when user clicks in somewhere in ListView the second one is shown, while in landscape mode both the fragments are shown. However I want to use the swipe feature while in the portrait mode and that should show the next fragment like this example,
This is my main laucher Activity which extends FragmentActivity,
package com.myexample.fragmentdemoexample;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.ListFragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.ViewGroup;
public class MainFragmentActivity extends FragmentActivity {
private static int NUM_ITEMS = 4;
MyAdapter mAdapter;
ViewPager mPager;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mPager = (ViewPager) findViewById(R.id.pager);
instantiateFragments();
Log.i("MainFragmentActivity", "MainFragmentActivity is Created");
}
public void instantiateFragments() {
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this, MyListFragment.class.getName()));
fragments.add(Fragment.instantiate(this, DetailFragment.class.getName()));
mAdapter = new MyAdapter(getSupportFragmentManager(), fragments);
mPager.setAdapter(mAdapter);
mPager.setCurrentItem(0);
}
public class MyAdapter extends FragmentPagerAdapter {
private FragmentManager frmanager;
private List<Fragment> fragments;
public MyAdapter(FragmentManager fm, List<Fragment> fragList) {
super(fm);
frmanager = fm;
fragments = fragList;
}
#Override
public int getCount() {
return NUM_ITEMS;
}
#Override
public Fragment getItem(int position) {
return fragments.get(position);
}
}
}
This is my layout/main.xml file
<?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"
android:orientation="horizontal" >
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1" >
</android.support.v4.view.ViewPager>
<fragment
android:id="#+id/listFragment"
android:layout_width="150dip"
android:layout_height="match_parent"
class="com.myexample.fragmentdemoexample.MyListFragment"
android:tag="listFragment" >
</fragment>
<fragment
android:id="#+id/detailFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.myexample.fragmentdemoexample.DetailFragment"
android:tag="detailFragment" >
<!-- Preview: layout=#layout/details -->
</fragment>
</LinearLayout>
and /layout-port/main.xml file contains
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1" >
</android.support.v4.view.ViewPager>
<fragment
android:id="#+id/listFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.myexample.fragmentdemoexample.MyListFragment" />
</LinearLayout>
I don't know why View Pager is not swiping in to next fragment... How can I do that ? I am using Fragment API for the first time.. so please give some pointers to right direction.
NOTE For other files see this question
Finally after hours of harrasment the problem was solved by removing the Fragment tags from the main layout file. Now main.xml contains
<?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"
android:orientation="horizontal" >
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1" >
</android.support.v4.view.ViewPager>
</LinearLayout>
This might be helpful for others with similar problem...
Even though it doesn't answer the question directly, but it has answer on similar problem I have faced. may be it will help others having similar problem.
If you are having a layout like
<AppBarLayout>
<ToolBar />
<TabLayout />
<ViewPager />
</AppBarLayout>
The mistake here is ViewPager should not be inside AppBarLayout. So change your code to:
<AppBarLayout>
<ToolBar />
<TabLayout />
</AppBarLayout>
<ViewPager />

Categories

Resources