fragment transaction duplication - android

So to start things of i have a activity and inside that activity i have a tabbedlayout with 3 tabs which are my fragments, now my problem is when i try to pass a value from the activity to lets call it fragmentA the value gets passed correctly BUT the 3 fragments in my tabbedlayout get duplicated. This has been an issue for me for quite some time.
thanks in advance
Here is my activity.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:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".aktivnosti.ActivityTask">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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:layout_weight="1"
android:background="?attr/colorPrimary"
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"
app:tabGravity="fill">
<android.support.design.widget.TabItem
android:id="#+id/tabItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tab_text_1">
</android.support.design.widget.TabItem>
<android.support.design.widget.TabItem
android:id="#+id/tabItem2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tab_text_2" />
<android.support.design.widget.TabItem
android:id="#+id/tabItem3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tab_text_3" />
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
And the code i used to pass my value from activity -> fragmentA, inside activity.java
TodoFragment fragment = new TodoFragment();
final android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragment.setArguments(value);
fragmentTransaction.replace(R.id.container, fragment,"a");
fragmentTransaction.addToBackStack("a");
fragmentTransaction.commit();
viewpager.java
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new TodoFragment();
case 1:
return new DoingFragment();
case 2:
return new KoncanoFragment();
default:
return null;
}
}
FragmentA.xml
<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/coordlayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/list"
android:name="com.example.matic.projectplan.aktivnostiFragment"
android:layout_width="match_parent"
android:layout_height="500dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
tools:context=".fragmenti.TodoFragment"
tools:listitem="#layout/fragment_todo" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="#dimen/fab_margin"
app:srcCompat="#android:drawable/ic_menu_add" />
</android.support.design.widget.CoordinatorLayout>
ANOTHER QUESTION EDIT
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
final Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_todo_list, container, false);
FloatingActionButton floatingActionButton = view.findViewById(R.id.fab);
recyclerView = (RecyclerView) view.findViewById(R.id.list);
bundle = this.getArguments();
if(bundle != null) {
fk_id = bundle.getLong("fk");
Toast.makeText(getContext(), "iz fragmenta" + fk_id, Toast.LENGTH_SHORT).show();
floatingActionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
fk_id = bundle.getLong("fk");
Toast.makeText(v.getContext(), "iz fragmenta" + fk_id, Toast.LENGTH_SHORT).show();
LayoutInflater li = LayoutInflater.from(getActivity());
View popupView = li.inflate(R.layout.popup_layout, null);
final EditText editText = popupView.findViewById(R.id.userInput);
AlertDialog.Builder adb = new AlertDialog.Builder(getContext());
adb.setView(popupView);
adb.setCancelable(false)
.setPositiveButton("Dodaj", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
String naziv = editText.getText().toString();
Aktivnost_ ak = new Aktivnost_(naziv, "15-jun", fk_id, "kajetan", "todo");
dodajAktivnost(ak);
array.add(ak);
Toast.makeText(getContext(), "dodano", Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("Prekliči", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
Toast.makeText(getContext(), "Preklical sem", Toast.LENGTH_LONG).show();
}
});
AlertDialog alertDialog = adb.create();
alertDialog.setCancelable(true);
alertDialog.show();
}
});
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.addItemDecoration(new DividerItemDecoration(getContext(), LinearLayoutManager.VERTICAL));
mmAdapter = new ToDoRecyclerViewAdapter(listAktivnosti(fk_id), getContext(), mListener);
mmAdapter.setOnItemClickListner(new ToDoRecyclerViewAdapter.onItemClickListner() {
#Override
public void onClick(long i) {
Intent intent = new Intent(getActivity(), PodrobnostiActivity.class);
intent.putExtra("key_id", i);
startActivity(intent);
Toast.makeText(getContext(), "" + i, Toast.LENGTH_SHORT).show();
}
});
mmAdapter.setOnLongClick(new ToDoRecyclerViewAdapter.OnLongClickListener_() {
#Override
public void onLongClick(long i, String item) {
if (item.equals("doing")) {
boolean update_1 = db.updateList(i, item);
if (update_1) {
//NAREDI SE LEPE ANIMACIJE
android.support.v4.app.FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(TodoFragment.this).attach(TodoFragment.this).commit();
Toast.makeText(getContext(), "Dodano v bazo.!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getContext(), "Prislo je do napake!", Toast.LENGTH_SHORT).show();
}
}
}
});
recyclerView.setAdapter(mmAdapter);
}
return view;
}

Sorry for my poor English, first of all create fragment adapter like this
named MainFragmentAdapter.java
public class MainFragmentAdapter extends FragmentPagerAdapter {
private List<BaseFragment> fragments;
private List<String> titles;
public MainFragmentAdapter(FragmentManager fm) {
super(fm);
fragments = new ArrayList<>();
titles = new ArrayList<>();
}
#Override
public BaseFragment getItem(int position) {
return fragments.get(position);
}
#Override
public int getCount() {
return fragments.size();
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
return titles.get(position);
}
public void addFragment(String title, BaseFragment fragment) {
titles.add(title);
fragments.add(fragment);
}
}
now create a method in your activity class like this and call it in activity's onCreate method
private void setupViewPager() {
MainFragmentAdapter fragmentAdapter = new MainFragmentAdapter(getSupportFragmentManager());
fragmentAdapter.addFragment(getString(R.string.fragment_a), new FragmentA());
fragmentAdapter.addFragment(getString(R.string.fragment_b), new FragmentB());
fragmentAdapter.addFragment(getString(R.string.fragment_c), new FragmentC());
ViewPager mViewPager = findViewById(R.id.view_pager);
mViewPager.setAdapter(fragmentAdapter);
TabLayout mTabLayout = findViewById(R.id.tab_layout);
mTabLayout.setupWithViewPager(mViewPager);
mViewPager.setOffscreenPageLimit(fragmentAdapter.getCount() - 1);
mViewPager.setCurrentItem(0);
}
and your activity's xml is like this.
<?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:background="#color/activity_background"
tools:context=".activities.DrawingListActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="#color/colorAccent"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="#color/white"
app:tabGravity="center"
app:tabIndicatorHeight="2dip"
app:tabMode="scrollable"
app:tabSelectedTextColor="#color/colorAccent"
app:tabTextColor="#5000BFA5" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
hope this can help you now. :-)

Related

AccelerateInterpolator sync state

im new in android developer world and need help, i need access state of pressed on toolbar from any button which will be seen after press on Toolbar, i want close it or hide open view on button press, Please advise
My code :
#Override
public View onCreateView(
#NonNull LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
// Inflate the layout for this fragment with the ProductGrid theme
View view = inflater.inflate(R.layout.product_grid_fragment, container, false);
// Set up the tool bar
setUpToolbar(view);
esas_btn = (Button)view.findViewById(R.id.esas_btn);
mq = (Button)view.findViewById(R.id.mq_btn);
sur = (Button)view.findViewById(R.id.sure_btn);
da = (Button)view.findViewById(R.id.dua_btn);
esas_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Begin the transaction
setFragment(new EsasFragment());
}
}); mq.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Begin the transaction
setFragment(new MQMaterialFragment());
}
}); sur.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Begin the transaction
setFragment(new SurelerFragment());
}
}); da.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Begin the transaction
setFragment(new DualarFragment());
}
});
return view;
}
private void setFragment(Fragment fragment){
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment, "visible_fragment");
ft.addToBackStack(null);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit(); toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.shr_branded_menu));
}
private void setUpToolbar(View view) {
toolbar = view.findViewById(R.id.app_bar);
AppCompatActivity activity = (AppCompatActivity) getActivity();
if (activity != null) {
activity.setSupportActionBar(toolbar);
}
toolbar.setNavigationOnClickListener(new NavigationIconClickListener(
getContext(),
view.findViewById(R.id.product_grid),
new AccelerateInterpolator(),
getContext().getResources().getDrawable(R.drawable.bran_menu),
getContext().getResources().getDrawable(R.drawable.close_menu))); // Menu close icon
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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:clipChildren="false"
android:clipToPadding="false"
tools:context=".SelectionGridFragment">
<LinearLayout
style="#style/Widget.Shrine.Backdrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingTop="88dp">
<include layout="#layout/shr_backdrop" />
</LinearLayout>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:elevation="0dp">
<android.support.v7.widget.Toolbar
android:id="#+id/app_bar"
style="#style/Widget.Shrine.Toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:paddingStart="12dp"
android:paddingLeft="12dp"
android:paddingEnd="12dp"
android:paddingRight="12dp"
app:contentInsetStart="0dp"
app:navigationIcon="#drawable/shr_branded_menu"
app:title="#string/app_name" />
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:id="#+id/product_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="56dp"
android:background="#color/productGridBackgroundColor"
android:elevation="8dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<FrameLayout
android:id="#+id/content_frame"
android:layout_marginLeft="30dp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

Collapsing AppBarLayout not scrolling with TabLayout and NestedScrollView

I have an issue with scrolling Collapsing AppBar, when I am trying to scroll it touching the AppBarLayout part. And also it sometimes scrolling not smoothly.
Here is short (1m 30s) video of issue: https://www.youtube.com/watch?v=n32N9Z4S3SA&feature=youtu.be
Here is link to simple project (only this issue on github): https://github.com/yozhik/Reviews/tree/master/app/src/main/java/com/ylet/sr/review
I'm using: com.android.support:appcompat-v7:27.1.1
There is issue on offsite: https://issuetracker.google.com/issues/37050152
How it is: https://www.youtube.com/watch?v=xWadOVEaTSY&feature=youtu.be
How it should be: https://www.youtube.com/watch?v=J8ITp6RusZo&feature=youtu.be
Does anybody know how to fix this issue you saw on video? I created absolutely simple layouts to avoid any side effects, but bug still reproduced. Thanks in advance.
Description:
CollapsingActivity - activity with Collapsing AppBarLayout. Which loads one or two fragments into "fragment_content_holder" and it has TabLayout
to switch between fragments in view pager.
In activity method onCreate() - I'm just simulating request to server (loadData), and when some fake data is loaded - I am showing fragments in view pager, on first call - I am creating new TabMenuAdapter extends FragmentPagerAdapter, populate it with fragments and save links to instances. On the next call -
I don't create fragments from scratch and just populate them with fresh data.
MenuFragment1, MenuFragment1 - two fragments.
MenuFragment1 - has method public void setupData(SomeCustomData data), to set new data, not recreating fragment on network reconnect.
NetworkStateReceiver - listens to network change and send notifications.
TabMenuAdapter - just simple class to hold fragments.
Next is just copy/paste of code:
public class CollapsingActivity extends AppCompatActivity implements ChangeNetworkNotification {
private static int dataReloadIteration = 0;
private SomeCustomData dummyDataFromServer;
#BindView(R.id.root_layout)
CoordinatorLayout root_layout;
#BindView(R.id.app_bar_layout)
AppBarLayout app_bar_layout;
#BindView(R.id.collapsing_toolbar_layout)
CollapsingToolbarLayout collapsing_toolbar_layout;
#BindView(R.id.view_pager_layout)
ViewPager viewPager;
#BindView(R.id.tab_layout)
TabLayout tabLayout;
#BindView(R.id.collapsing_data_1_txt)
TextView collapsing_data_1_txt;
private NetworkStateReceiver networkStateReceiver;
private boolean isConnected;
protected Fragment currentFragment;
protected Fragment previousFragment;
protected FragmentManager fragmentManager;
private boolean dataLoading = false;
private boolean isCreated = false;
private MenuFragment1 menu1Fragment1;
private MenuFragment2 menu1Fragment2;
private TabMenuAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("TEST", "CollapsingActivity.onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_collapsing);
ButterKnife.bind(this);
fragmentManager = getSupportFragmentManager();
networkStateReceiver = new NetworkStateReceiver();
networkStateReceiver.setNetworkReceiver(this);
IntentFilter intentFilterForNetwork = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
registerReceiver(networkStateReceiver, intentFilterForNetwork);
initToolbar();
loadData();
}
#Override
protected void onStart() {
Log.d("TEST", "CollapsingActivity.onStart");
super.onStart();
IntentFilter intentFilterForNetwork = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
registerReceiver(networkStateReceiver, intentFilterForNetwork);
}
private void initToolbar() {
Log.d("TEST", "CollapsingActivity.initToolbar");
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_layout);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
private void loadData() {
Log.d("TEST", "CollapsingActivity.loadData");
dataLoading = true;
Thread t = new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(CollapsingActivity.this, "Data loaded.", Toast.LENGTH_SHORT).show();
Log.d("TEST", "CollapsingActivity.Data loaded.");
dataReloadIteration++;
dummyDataFromServer = getDummyObjectFromServer();
collapsing_data_1_txt.setText(dummyDataFromServer.Name); //Set data from server in collapsing part of Activity
boolean showOneView = false;
if (showOneView) {
initSingleView();
} else {
if (!isCreated) {
initTabView();
} else {
menu1Fragment1.setupData(dummyDataFromServer); //Set the data from server to fragment, not reloading it, just updating data
}
}
dataLoading = false;
}
});
}
});
t.start();
}
private SomeCustomData getDummyObjectFromServer() {
SomeCustomData dto = new SomeCustomData();
dto.Age = dataReloadIteration;
dto.Name = "Name " + dataReloadIteration;
return dto;
}
private void initSingleView() {
Log.d("TEST", "CollapsingActivity.initSingleView");
tabLayout.setVisibility(View.GONE);
viewPager.setVisibility(View.GONE);
showFragmentWithoutBackStack(R.id.fragment_content_holder, new MenuFragment1());
}
private void initTabView() {
if (!isCreated) {
Log.d("TEST", "CollapsingActivity.initTabView");
tabLayout.setVisibility(View.VISIBLE);
viewPager.setVisibility(View.VISIBLE);
setupViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
isCreated = true;
}
}
private void setupViewPager(ViewPager viewPager) {
Log.d("TEST", "CollapsingActivity.setupViewPager");
menu1Fragment1 = MenuFragment1.newInstance(dummyDataFromServer);
menu1Fragment2 = MenuFragment2.newInstance();
adapter = new TabMenuAdapter(getSupportFragmentManager());
adapter.addFragment(menu1Fragment1, "Menu 1");
adapter.addFragment(menu1Fragment2, "Menu 2");
viewPager.setAdapter(adapter);
}
#Override
protected void onDestroy() {
Log.d("TEST", "CollapsingActivity.onDestroy");
super.onDestroy();
unregisterReceiver(networkStateReceiver);
}
#Override
public void networkStateIsChanged(boolean isConnected) {
this.isConnected = isConnected;
Log.d("TEST", "CollapsingActivity.networkStateIsChanged.isConnected: " + isConnected);
if (isConnected) {
Toast.makeText(CollapsingActivity.this, "Connection received.", Toast.LENGTH_SHORT).show();
if (!dataLoading) {
loadData();
}
} else {
Toast.makeText(CollapsingActivity.this, "Connection lost.", Toast.LENGTH_SHORT).show();
}
}
protected void showFragmentWithoutBackStack(int containerViewId, Fragment fragment) {
Log.d("TEST", "CollapsingActivity.showFragmentWithoutBackStack");
previousFragment = currentFragment;
currentFragment = fragment;
String fragmentTag = fragment.getClass().getSimpleName();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
if (previousFragment != null) {
fragmentTransaction.remove(previousFragment);
}
fragmentTransaction.add(containerViewId, fragment, fragmentTag)
.commitNowAllowingStateLoss();
}
}
activity's layout:
<?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/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="false"
android:background="#color/white"
android:stateListAnimator="#drawable/appbar_shadow"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/green"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:title=""
app:titleEnabled="false">
<include
layout="#layout/appbar_collapsing_part"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="?attr/actionBarSize"
app:layout_scrollFlags="scroll|exitUntilCollapsed" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/green"
android:stateListAnimator="#drawable/appbar_shadow"
app:layout_collapseMode="pin"
app:popupTheme="#style/AppTheme.PopupOverlay"
app:theme="#style/ToolbarMenuItemsBackGroundTheme">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/title_txt"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_toStartOf="#+id/star_img"
android:ellipsize="end"
android:fontFamily="sans-serif-medium"
android:gravity="center_vertical"
android:maxLines="1"
android:textColor="#color/colorPrimaryDark"
android:textSize="19sp" />
<ImageView
android:id="#+id/star_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:layout_marginEnd="24dp"
android:padding="10dp"
android:src="#drawable/ic_favorite_filled" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:background="#45b0b2"
android:visibility="gone"
app:tabBackground="#drawable/backgr_blue_transparent_selector"
app:tabGravity="center"
app:tabIndicatorColor="#color/colorPrimaryDark"
app:tabIndicatorHeight="2dp"
app:tabMinWidth="500dp"
app:tabMode="fixed"
app:tabSelectedTextColor="#color/colorPrimaryDark"
app:tabTextAppearance="#style/CustomTabLayout"
app:tabTextColor="#color/green" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/fragment_content_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/blue"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<android.support.v4.view.ViewPager
android:id="#+id/view_pager_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
Fragment 1:
public class MenuFragment1 extends Fragment {
public SomeCustomData transferedDataFromActivity;
private TextView data_1_txt;
public static MenuFragment1 newInstance(SomeCustomData data) {
Log.d("TEST", "MenuFragment1.newInstance");
MenuFragment1 fragment = new MenuFragment1();
Bundle args = new Bundle();
args.putSerializable("DATA_FROM_ACTIVITY", data);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("TEST", "MenuFragment1.onCreate");
if (getArguments() != null) {
this.transferedDataFromActivity = (SomeCustomData) getArguments().getSerializable("DATA_FROM_ACTIVITY");
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d("TEST", "MenuFragment1.onCreateView");
View v = inflater.inflate(R.layout.menu_fragment_1, container, false);
data_1_txt = (TextView) v.findViewById(R.id.data_1_txt);
setupInOnCreateView();
return v;
}
protected void setupInOnCreateView() {
Log.d("TEST", "MenuFragment1.setupInOnCreateView");
//initialization of all view elements of layout with data is happens here.
setupData(transferedDataFromActivity);
}
public void setupData(SomeCustomData data) {
Log.d("TEST", "MenuFragment1.setupData");
this.transferedDataFromActivity = data;
if (transferedDataFromActivity != null) {
data_1_txt.setText(transferedDataFromActivity.Name);
}
}
}
Fragment 1 layout:
<?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:background="#color/green"
android:orientation="vertical">
<TextView
android:id="#+id/data_1_txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/yellow"
android:text="Test"
android:textSize="20sp" />
<include layout="#layout/description_layout" />
</LinearLayout>
Fragment 2:
public class MenuFragment2 extends Fragment {
public static MenuFragment2 newInstance() {
Log.d("TEST", "MenuFragment2.newInstance");
MenuFragment2 fragment = new MenuFragment2();
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d("TEST", "MenuFragment2.onCreateView");
View v = inflater.inflate(R.layout.menu_fragment_2, container, false);
setupInOnCreateView();
return v;
}
protected void setupInOnCreateView() {
Log.d("TEST", "MenuFragment2.setupInOnCreateView");
//initialization of all view elements of layout with data is happens here.
}
}
Fragment 2 layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/comments_scrollable_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible">
<LinearLayout
android:id="#+id/comments_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#color/yellow" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#color/blue" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#color/yellow" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
TabMenuAdapter:
public class TabMenuAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragments = new ArrayList<>();
private final List<String> mFragmentTitles = new ArrayList<>();
public TabMenuAdapter(FragmentManager fm) {
super(fm);
}
public void addFragment(Fragment fragment, String title) {
mFragments.add(fragment);
mFragmentTitles.add(title);
}
#Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
#Override
public int getCount() {
return mFragments.size();
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitles.get(position);
}
}
Try this
Activity layout
<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">
<android.support.design.widget.AppBarLayout
android:id="#+id/appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:title=""
app:titleEnabled="false">
<ImageView
android:layout_width="match_parent"
android:layout_height="256dp"
android:scaleType="fitXY"
android:src="#drawable/abc"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.CollapsingToolbarLayout>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
app:layout_anchor="#id/appBar"
app:tabGravity="fill"
app:tabTextColor="#FFFFFF"
app:tabSelectedTextColor="#ff00"
app:tabMode="scrollable"
app:layout_anchorGravity="bottom" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
Activity code
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new BlankFragment(), "TAB-ONE");
adapter.addFragment(new BlankFragment(), "TAB-TWO");
adapter.addFragment(new BlankFragment(), "TAB-THREE");
viewPager.setAdapter(adapter);
}
}
Fragment Code
public class BlankFragment extends Fragment {
public BlankFragment() {
// 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_blank, container, false);
}
}
Fragment Layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 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"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".BlankFragment">
<ImageView
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_margin="5dp"
android:scaleType="centerCrop"
android:src="#drawable/kid_goku" />
<ImageView
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_margin="5dp"
android:scaleType="centerCrop"
android:src="#drawable/kid_goku" />
<ImageView
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_margin="5dp"
android:scaleType="centerCrop"
android:src="#drawable/kid_goku" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
ViewPagerAdapter code
public class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
You can see the video here CollapsingToolbarLayout WITH TabLayout
You download the complete project from here CollapsingToolbarLayout WITH TabLayout
Try the below layout foryour activity
<?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/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="false"
android:background="#color/white"
android:stateListAnimator="#drawable/appbar_shadow"
android:theme="#style/AppTheme.AppBarOverlay"
tools:targetApi="lollipop">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/green"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:title=""
app:titleEnabled="false">
<include
layout="#layout/appbar_collapsing_part"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="?attr/actionBarSize"
app:layout_scrollFlags="scroll|exitUntilCollapsed" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/green"
android:stateListAnimator="#drawable/appbar_shadow"
app:layout_collapseMode="pin"
app:popupTheme="#style/AppTheme.PopupOverlay"
app:theme="#style/ToolbarMenuItemsBackGroundTheme"
tools:targetApi="lollipop">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/title_txt"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_toStartOf="#+id/star_img"
android:ellipsize="end"
android:fontFamily="sans-serif-medium"
android:gravity="center_vertical"
android:maxLines="1"
android:textColor="#color/colorPrimaryDark"
android:textSize="19sp" />
<ImageView
android:id="#+id/star_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:layout_marginEnd="24dp"
android:padding="10dp"
android:src="#drawable/ic_favorite_filled" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:background="#45b0b2"
android:visibility="gone"
app:tabBackground="#drawable/backgr_blue_transparent_selector"
app:tabGravity="center"
app:tabIndicatorColor="#color/colorPrimaryDark"
app:tabIndicatorHeight="2dp"
app:tabMinWidth="500dp"
app:tabMode="fixed"
app:tabSelectedTextColor="#color/colorPrimaryDark"
app:tabTextAppearance="#style/CustomTabLayout"
app:tabTextColor="#color/green" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/fragment_content_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<android.support.v4.view.ViewPager
android:id="#+id/view_pager_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
Try scrolling to top in the fragment in the view pager by calling scrollView.fullScroll(View.FOCUS_UP) whenever you click on a tab. If your fragment inside the viewpager is scrolled up then your scrolling should work correctly.
You have done it almost correctly. But need some minor tweaks and modifications.
Step 1 : Set some height to your appBar. Say 224dp.
Step 2 : Set match_parent property to CollapsingToolbarLayout height.
Step 3 : Set following scroll flags to the CollapsingToolbarLayout
`app:layout_scrollFlags = "scroll|enterAlways|enterAlwaysCollapsed"`
Step 4 : Set the toolbar collapsemode to none
Check if it works fine. Let me know.
I have spend a lot of time investigating my issue with AppBar + Collapsing Layouts. And what I did find out is that it's a bug in google support library. So I created the issue for google and they assigned it to one of developers: https://issuetracker.google.com/issues/78686882
But to solve this issue right now I made a workaround - if users will get stuck with scrolling on collapsing part of the layout - they will probably try to click on one of tabs in my TabLayout. So I added code to auto collapse with animation collapsing part of layout: appBarLayout.setExpanded(false, true);
Here is the code:
tabLayout.addOnTabSelectedListener(new ViewPagerOnTabSelectedListener(viewPagerLayout) {
#Override
public void onTabSelected(TabLayout.Tab tab) {
appBarLayout.setExpanded(false, true);
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
//no ui effects
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
appBarLayout.setExpanded(false, true);
}
});
To solve this issue, there is a workaround to do on the AppBarLayout :
class CustomAppBarLayout #JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : AppBarLayout(context, attrs, defStyleAttr) {
override fun getBehavior(): CoordinatorLayout.Behavior<AppBarLayout> {
return Behavior().also {
it.setDragCallback(object : Behavior.DragCallback() {
override fun canDrag(appBarLayout: AppBarLayout) = true
})
}
}
}
Found here : https://github.com/material-components/material-components-android/issues/1878

scrollview is not working while using google maps and tabs in android

activity_main
<?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/layout_bg_color"
tools:context="com.example.vickey.medicalcompanies.DoctorInfo">
<android.support.design.widget.AppBarLayout
android:id="#+id/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="wrap_content"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="title"
android:textSize="20sp" />
<TextView
android:id="#+id/speciality"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="spec"
android:textSize="15sp" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:layout_below="#id/appbarlayout"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:id="#+id/scrollview">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<fragment
android:id="#+id/doctor_map"
android:name="com.example.vickey.medicalcompanies.WorkaroundMapFragment"
android:layout_width="match_parent"
android:layout_height="200dp"
>
</fragment>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignBottom="#+id/doctor_map"
android:layout_marginTop="10dp"
android:background="#805A6051"
android:orientation="horizontal">
<TextView
android:id="#+id/review"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:gravity="center"
android:text="1247 reviews"
android:textColor="#color/text_color"
android:textSize="20sp" />
<RatingBar
android:id="#+id/ratingBar"
style="?android:attr/ratingBarStyleIndicator"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_marginLeft="60dp"
android:isIndicator="false"
android:numStars="5"
android:scaleY="0.7"
android:scaleX="0.7"
android:rating="5"
android:stepSize="0.2"
android:theme="#style/RatingBar" />
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="#+id/tab_ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/doctor_map"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
app:tabGravity="fill"
app:tabIndicatorColor="#color/colorAccent"
app:tabMode="fixed"
app:tabSelectedTextColor="#color/lightGrey"
app:tabTextAppearance="#android:style/TextAppearance.Widget.TabWidget"
app:tabTextColor="#color/layout_bg_color" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="130dp"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/tab_ll2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/tab_ll"
android:layout_marginTop="10dp"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.design.widget.TabLayout
android:id="#+id/tabs2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed"
android:background="#color/tab.location.bg"
app:tabTextAppearance="#android:style/TextAppearance.Widget.TabWidget"
app:tabTextColor="#color/text_color" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager2"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:tabTextAppearance="#android:style/TextAppearance.Widget.TabWidget"
/>
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
activity file
public class DoctorInfo extends AppCompatActivity implements OnMapReadyCallback {
TabLayout tabLayout, tabLayout2;
ViewPager viewPager, viewPager2;
Toolbar toolbar;
GoogleMap gMap = null;
TextView title, specility;
ScrollView scrollView;
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_doctor_info);
toolbar = (Toolbar) findViewById(R.id.toolbar);
title = (TextView) toolbar.findViewById(R.id.title);
specility = (TextView) toolbar.findViewById(R.id.speciality);
setSupportActionBar(toolbar);
title.setText("Dr. Regis Ketelers");
specility.setText("Cardiologue");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
/* SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.doctor_map);
supportMapFragment.getMapAsync(this);
*/
mMap = ((WorkaroundMapFragment) getSupportFragmentManager().findFragmentById(R.id.doctor_map)).getMap();
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mMap.getUiSettings().setZoomControlsEnabled(true);
scrollView = (ScrollView) findViewById(R.id.scrollview); //parent scrollview in xml, give your scrollview id value
((WorkaroundMapFragment) getSupportFragmentManager().findFragmentById(R.id.doctor_map))
.setListener(new WorkaroundMapFragment.OnTouchListener() {
#Override
public void onTouch() {
scrollView.requestDisallowInterceptTouchEvent(true);
}
});
//tab1
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
//tab2
viewPager2 = (ViewPager) findViewById(R.id.viewpager2);
setupViewPager2(viewPager2);
tabLayout2 = (TabLayout) findViewById(R.id.tabs2);
tabLayout2.setupWithViewPager(viewPager2);
}
public void onMapReady(GoogleMap map) {
gMap = map;
LatLng TutorialsPoint = new LatLng(21, 57);
gMap.addMarker(new
MarkerOptions().position(TutorialsPoint).title("Tutorialspoint.com"));
gMap.moveCamera(CameraUpdateFactory.newLatLng(TutorialsPoint));
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new MapandDetails2(), "Services");
adapter.addFragment(new MapandDetails2(), "Map & Details");
adapter.addFragment(new MapandDetails2(), "Galery");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
private void setupViewPager2(ViewPager viewPager) {
ViewPagerAdapter2 adapter = new ViewPagerAdapter2(getSupportFragmentManager());
adapter.addFragment(new Location2Fragment(), "Location1");
adapter.addFragment(new Location2Fragment(), "Location2");
adapter.addFragment(new Location2Fragment(), "Location3");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter2 extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter2(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent intent = new Intent(DoctorInfo.this, NavigationDrawer.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onBackPressed() {
Intent intent = new Intent(DoctorInfo.this, NavigationDrawer.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.navigation_drawer, menu);
return true;
}
}
//workAroundMapFragment class
public class WorkaroundMapFragment extends SupportMapFragment {
private OnTouchListener mListener;
#Override
public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle savedInstance) {
View layout = super.onCreateView(layoutInflater, viewGroup, savedInstance);
TouchableWrapper frameLayout = new TouchableWrapper(getActivity());
frameLayout.setBackgroundColor(getResources().getColor(android.R.color.transparent));
((ViewGroup) layout).addView(frameLayout,
new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
return layout;
}
public void setListener(OnTouchListener listener) {
mListener = listener;
}
public interface OnTouchListener {
public abstract void onTouch();
}
public class TouchableWrapper extends FrameLayout {
public TouchableWrapper(Context context) {
super(context);
}
#Override
public boolean dispatchTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mListener.onTouch();
break;
case MotionEvent.ACTION_UP:
mListener.onTouch();
break;
}
return super.dispatchTouchEvent(event);
}
}
enter code here
}
it is complete code of tabview and google maps and it is working fine without scrollview. scrollview is working in tabs but not in whole activity. i want to use scrollview in whole activity. but it is not working.
i tried some more solution but gain nothing.
i mention all the code:
Activity,
.xml file
and one require java class
guide me please if anyone know.

Android studio ActionBar with Tabs inside a fragment

I have created a project with the navigation drawer template and I have different fragments for each navigation item.
I have an ActionBar with Tabs on one of the fragments and I have set the width to match parent but when I run it the tabs doesn't stick to the actionbar and the width is also not matching the parent. check the code below:
XML:
<FrameLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.yardbird.justice.yardbird.Fragments.DrinksFragment">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="47dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
/>
Java:
public class DrinksFragment extends Fragment {
public DrinksFragment() {
// Required empty public constructor
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_drink, container, false);
TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Frabment 1"));
tabLayout.addTab(tabLayout.newTab().setText("Frabment 1"));
final ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewPager);
viewPager.setAdapter(new CustomAdapter(getFragmentManager(),
tabLayout.getTabCount()));
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
return view;
}
private class CustomAdapter extends FragmentStatePagerAdapter {
int numberOfTabs;
public CustomAdapter(FragmentManager fragmentManager, int numberOfTabs) {
super( fragmentManager);
this.numberOfTabs = numberOfTabs;
}
#Override
public Fragment getItem(int position) {
switch (position)
{
case 0:
Tab1Fragment tab1 = new Tab1Fragment();
return tab1;
case 1:
Tab2Fragment tab2 = new Tab2Fragment();
return tab2;
default:
return null;
}
}
#Override
public int getCount() {
return numberOfTabs;
}
}
}
Screen Shot of the app:
Can anyone help fix that?
Try this:
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<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/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways" />
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

Scrolling in Fragment doesn't work into a TabLayout

I am stuck with scrolling in a fragment into a tablayout.
This is my MainActivity class
public class Mainctivity extends AppCompatActivity {
private TabLayout tabLayout;
private ViewPager viewPager;
ViewPagerAdapter viewPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager();
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setViewPager(TabLayout.GRAVITY_FILL);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager() {
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
String [] tabNames = new String[{"Technology","World","Life"} ;
int [] tabTopicIds = new int[]{1,2,3} ;
for(int i=0; i< 7; i++)
{
OneFragment homeFeed = new OneFragment();
Bundle homeArgs = new Bundle();
homeArgs.putString("PAGE_NUMBER",""+tabTopicIds[i]);
homeFeed.setArguments(homeArgs);
viewPagerAdapter.addFragment(homeFeed, tabNames[i]); // note: this line can cause crashes
}
viewPager.setAdapter(viewPagerAdapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>(); // note: this line can cause crashes
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
And here is My activity_main layout.
<?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="match_parent"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="48dip"
android:background="#color/white"
app:tabIndicatorColor="#color/colorPrimary"
app:pstsIndicatorHeight="3dip"
app:pstsTextAllCaps="false"
app:tabPaddingStart="2dp"
app:tabPaddingEnd="2dp"
app:tabGravity="fill"/>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
For each tab there is a FragmentOne class associated with viewPager and this is onCreateView Method of FragmentOne
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View mainView = inflater.inflate(R.layout.activity_main, container, false);
listView = (ListView) mainView.findViewById(R.id.list);
feedItems = new ArrayList<FeedItem>();
listAdapter = new FeedListAdapter(getActivity(), feedItems);
listView.setAdapter(listAdapter);
listView.setOnScrollListener(new EndlessScrollListener() {
#Override
public boolean onLoadMore(int page, int totalItemsCount) {
if(totalItemsCount > 150) return false;
return true;
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if(!isLoading && firstVisibleItem + visibleItemCount >= totalItemCount && totalItemCount<MAX_ITEM_IN_LIST){
requestToServer(); // request for next items
}
}
});
return mainView;
}
Layout of FragmentOne
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#android:color/darker_gray"
android:background="#color/white"
android:dividerHeight="1.0sp"/>
</LinearLayout>
Here I add listView.setOnScrollListener into ListView of FragmentOne but doesn't work scrolling.
But when I FragmentOne put in another Layout like LinearLayout except Tablayout, it works fine.
You have put the viewpager code inside AppBarLayout in activity_main.xml. Cut and Paste the ViewPager outside AppBarLayout. Copy this code in your activity_main.xml. Hope it solves your problem.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
app:tabIndicatorColor="#color/colorPrimary"
app:pstsIndicatorHeight="3dip"
app:pstsTextAllCaps="false"
app:tabPaddingStart="2dp"
app:tabPaddingEnd="2dp"
app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

Categories

Resources