In my app i am using this library
https://github.com/DmitryMalkovich/circular-with-floating-action-button
to implement progress bar with floating action button. Its working on activity but when i included this layout in my fragment's layout progress bar doesn't show.
Here is my code for better explanation
Please guide me where i am going wrong
Any help will be appreciated.
customlayout.xml
<?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:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
>
<com.dmitrymalkovich.android.ProgressFloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:clickable="true">
<ProgressBar
android:id="#+id/progressbar"
style="#style/Widget.AppCompat.ProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/transparent_logo"
app:backgroundTint="#color/bg_color"
android:layout_centerInParent="true"
/>
</com.dmitrymalkovich.android.ProgressFloatingActionButton>
</RelativeLayout>
fragmentlayout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layoutcontainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="bg_color"
>
<include
layout="#layout/customlayout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<include
layout="#layout/otherlayout
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
I am hiding and showing the parent layout of customlayout.xml on runtime when i get data from serivice, before service call i SHOW it with View.VISIBLE and after service call i HIDE it with View.GONE
UPDATED
Java code
Fragment
public class MyFragment extends Fragment {
private Context context;
//root view of layout
View rootView;
private static final String ARG_PARAM1 = "Class";
private String screen_title;
private String URL = "";
public MyFragment() {
// Required empty public constructor
}
public static MyFragment newInstance(String param1) {
MyFragmentfragment = new MyFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = getActivity();
if (getArguments() != null) {
screen_title = getArguments().getString(ARG_PARAM1);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.fragmentlayout, container, false);
//find views by ids
getData();
return rootView;
}
private void getData() {
CustomClass.getInstance(context, rootView).show();
//service call
//on getting response from serivce (have implemented a listener here)
CustomClass.getInstance(context, rootView).hide(); }
#Override
public void onAttach(Context context) {
super.onAttach(context);
}
#Override
public void onDetach() {
super.onDetach();
}
CustomClass
public class CustomClass {
private ProgressBar mProgressBar;
private FloatingActionButton mFab;
private RelativeLayout parentLayout;
private LayoutInflater inflater;
private static CustomClass custom;
private CustomClass (Context context) {
findViewsById(context);
}
private CustomClass (View view) {
findViewsById(view);
}
public static CustomClass getInstance(Context context, View view) {
custom = new CustomClass (view);
custom.setPColor(context);
return custom;
}
public static CustomClass getInstance(Context context) {
custom = new CustomClass (context);
custom.setPColor(context);
return custom;
}
private void setPColor(Context context) {
if (mProgressBar != null)
mProgressBar.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(context, R.color.bluecolor), android.graphics.PorterDuff.Mode.MULTIPLY);
}
private void findViewsById(View view) {
if (view != null) {
mProgressBar = (ProgressBar) view.findViewById(R.id.custom_progressbar);
mFab = (FloatingActionButton) view.findViewById(R.id.custom_floatingActionButton);
parentLayout = (RelativeLayout) view.findViewById(R.id.rl_progress_fab_container);
}
}
private void findViewsById(Context context) {
Activity activity = (Activity) context;
if (activity != null) {
mProgressBar = (ProgressBar) activity.findViewById(R.id.custom_progressbar);
mFab = (FloatingActionButton) activity.findViewById(R.id.custom_floatingActionButton);
parentLayout = (RelativeLayout) activity.findViewById(R.id.rl_progress_fab_container);
}
}
public void show() {
if (custom.parentLayout != null)
custom.parentLayout.setVisibility(View.VISIBLE);
}
public void hide() {
if (custom.parentLayout != null)
custom.parentLayout.setVisibility(View.GONE);
}
make your customlayout at the bottom with the parent layout.
so your z index for that layout is at the top and it will be visible and gone that can be seen by you.
You need to use CoordinatorLayout with FAB .
Just put your FAB inside CoordinatorLayout.
As you can see library sample code suggests to use it too.
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.dmitrymalkovich.android.progressfabsample.ScrollingActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="#dimen/app_bar_height"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<com.dmitrymalkovich.android.ProgressFloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/fab_margin"
android:clickable="true"
app:layout_anchor="#id/app_bar"
app:layout_anchorGravity="bottom|end">
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_backup_black_24dp"
app:backgroundTint="#color/colorFab" />
<ProgressBar
style="#style/Widget.AppCompat.ProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</com.dmitrymalkovich.android.ProgressFloatingActionButton>
<com.dmitrymalkovich.android.ProgressFloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:clickable="true">
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_backup_black_24dp"
app:backgroundTint="#color/colorFab" />
<ProgressBar
style="#style/Widget.AppCompat.ProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</com.dmitrymalkovich.android.ProgressFloatingActionButton>
try this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.fragmentlayout, container, false);
//find views by ids
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
getData();
}
});
return rootView;
}
or
private void getData() {
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
CustomClass.getInstance(context, rootView).show();
CustomClass.getInstance(context, rootView).hide();
}
});
}
Related
When Bottom sheet is open(First image below) and
When tab on editText , the keyboard hides the edit text(Second image below) , i want to move keyword below the "ADD" botton of bottomsheet.How to do this?
Image 2:
Fragment
public class CustomExerciseFragment extends BaseCardFragment {
private View parent_view;
private RecyclerView recyclerView;
private AdapterEasyListAnimation mAdapter;
private LinkedList<DaysDetails> items = new LinkedList<>();
private int animation_type = ItemAnimation.BOTTOM_UP;
private DaysDAO daysDAO;
private ExercisesDAO exercisesDAO;
private ImageView imageView;
private Button buttonBreif;
//=========Configure this at the beginning=============
private final String routineId="1520";
private final String legWorkoutImageName="leg_workout_customize";
private BottomSheetBehavior mBehavior;
private BottomSheetDialog mBottomSheetDialog;
private View bottom_sheet;
public static CustomExerciseFragment newInstance() {
CustomExerciseFragment fragment = new CustomExerciseFragment();
return fragment;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_custom_exercise, container, false);
mCardView = (CardView) root.findViewById(R.id.cardView);
mCardView.setMaxCardElevation(mCardView.getCardElevation()
* CardAdapter.MAX_ELEVATION_FACTOR);
bottom_sheet = root.findViewById(R.id.bottom_sheet_add_days);
mBehavior = BottomSheetBehavior.from(bottom_sheet);
mBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
((ExtendedFloatingActionButton) root.findViewById(R.id.extended_fab)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showAddDaysBottomSheetDialog();
}
});
return root;
}
private void showAddDaysBottomSheetDialog() {
if (mBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED) {
mBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
final View view = getLayoutInflater().inflate(R.layout.sheet_add_days, null);
mBottomSheetDialog = new BottomSheetDialog(getContext());
mBottomSheetDialog.setContentView(view);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mBottomSheetDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
mBottomSheetDialog.show();
mBottomSheetDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
mBottomSheetDialog = null;
}
});
}
}
BottomSheetLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:background="#android:color/white"
android:fitsSystemWindows="true"
android:orientation="vertical">
<androidx.core.widget.NestedScrollView
android:id="#+id/nested_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:scrollbars="vertical"
android:layout_gravity="bottom"
android:scrollingCache="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="#dimen/spacing_middle"
android:paddingLeft="#dimen/spacing_mlarge"
android:paddingRight="#dimen/spacing_mlarge"
android:paddingTop="#dimen/spacing_middle">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/spacing_medium"
android:orientation="vertical"
android:padding="#dimen/spacing_medium">
<EditText
style="#style/EditText.Flat.Grey"
android:layout_width="match_parent"
android:layout_height="#dimen/spacing_xmlarge"
android:background="#drawable/edit_text_round_bg_outline"
android:inputType="text"
android:minHeight="#dimen/spacing_xmlarge"
android:paddingLeft="#dimen/spacing_large"
android:paddingRight="#dimen/spacing_large"
android:text="400" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/spacing_middle"
android:orientation="vertical">
<Button
style="#style/Button.Primary.Borderless"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/btn_rect_black_grey"
android:text="ADD"
android:textColor="#android:color/white" />
</LinearLayout>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</LinearLayout>
This question already has answers here:
CardViews not showing in RecyclerView
(3 answers)
Closed 2 years ago.
activity_driver_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".DriverListActivity">
<include
android:id="#+id/toolbarDL"
layout="#layout/toolbar"/>
<RelativeLayout
android:id="#+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
</LinearLayout>
fragment_driver_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DLFragment.DriverListFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/driverList"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
raw_driver_list.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:elevation="5dp"
app:cardCornerRadius="4dp"
xmlns:tools="http://schemas.android.com/tools">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:orientation="horizontal">
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/profile_image"
android:layout_width="95dp"
android:layout_height="95dp"
android:padding="10dp"
android:src="#drawable/ic_android"
app:civ_border_width="2dp"
app:civ_border_color="#color/quantum_lightgreen"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/carName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Suzuki Abcd"
android:text="SUZUKI Wagon r"
android:layout_gravity="center_horizontal"
android:padding="10dp"
android:textColor="#color/places_text_black_alpha_87"
android:textSize="20sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/driverNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="0312-1234567"
android:text="0312-1234567"
android:layout_gravity="center_horizontal"
android:padding="10dp"
android:textColor="#color/cardview_dark_background"
android:textSize="15sp"
android:textStyle="bold"/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
DriverListAdapter.Java
public class DriverListAdapter extends RecyclerView.Adapter<DriverListAdapter.DriverListViewHolder> {
private ArrayList<Driver> mLst;
public class DriverListViewHolder extends RecyclerView.ViewHolder{
public CircleImageView mImage;
public TextView mCName, mDNumber;
public DriverListViewHolder(#NonNull View itemView) {
super(itemView);
mImage = itemView.findViewById(R.id.profile_image);
mCName = itemView.findViewById(R.id.carName);
mDNumber = itemView.findViewById(R.id.driverNumber);
}
}
#NonNull
#Override
public DriverListViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.raw_driver_list, parent, false);
DriverListViewHolder driverListAdapter = new DriverListViewHolder(view);
return driverListAdapter;
}
#Override
public void onBindViewHolder(#NonNull DriverListViewHolder holder, int position) {
Driver driver = mLst.get(position);
holder.mImage.setImageResource(driver.getdImage());
holder.mCName.setText(driver.getdCName());
}
#Override
public int getItemCount()
{
return mLst.size();
}
public DriverListAdapter(ArrayList<Driver> lst) {
this.mLst = lst;
}
}
DriverListFragment.Java
public class DriverListFragment extends Fragment {
public static final String TAG = "DriverListFragment";
private ArrayList<Driver> driverLst;
private RecyclerView mRecyclerView;
private RecyclerView.LayoutManager mLayoutManager;
private DriverListAdapter mAdapter;
public DriverListFragment() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_driver_list, container, false);
driverLst = new ArrayList<>();
driverLst.add(new Driver(R.drawable.ic_android,"Car One","0312-1234567"));
driverLst.add(new Driver(R.drawable.ic_android,"Car Two","0312-1234567"));
driverLst.add(new Driver(R.drawable.ic_android,"Car Three","0312-1234567"));
driverLst.add(new Driver(R.drawable.ic_android,"Car Four","0312-1234567"));
driverLst.add(new Driver(R.drawable.ic_android,"Car Five","0312-1234567"));
mRecyclerView = (RecyclerView) v.findViewById(R.id.driverList);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this.getActivity());
mAdapter = new DriverListAdapter(driverLst);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
return v;
}
}
DriverListActivity.Java
public class DriverListActivity extends AppCompatActivity {
public static final String TAG = "DriverListActivity";
private TDD tdd = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_driver_list);
initToolbar();
getMyIntent();
driverList();
}
private void getMyIntent()
{
Intent i = getIntent();
tdd = (TDD) i.getSerializableExtra("tdd");
Log.d(TAG,tdd.toString());
}
private void initToolbar()
{
Toolbar toolbar = findViewById(R.id.toolbarDL);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Hello There..");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
public void driverList()
{
DriverListFragment driverListFragment = new DriverListFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragmentContainer,driverListFragment);
fragmentTransaction.addToBackStack("driverListFragment");
fragmentTransaction.commit();
}
}
I want to show my RecyclerView list in fragment instead of an activity, when i call fragment in activity it simply showing me blank activity however, there's no error in code. How can i achieve this task or where i am doing a mistake?
How to build a Horizontal ListView with RecyclerView?
LinearLayoutManager layoutManager
= new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
RecyclerView myList = (RecyclerView) findViewById(R.id.my_recycler_view);
myList.setLayoutManager(layoutManager);
I am trying to build a e-commerce app, now I am stucked here. When try to add fragment class in my activity some errors showing.
when I open my fragment recyclerview items bottom margin is too long, its adjusting automatically.
when I back to activity fragment class items and activity items all showing
when I back a blank page is shown
my codes are below
//this is my activity.xml
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android" >
<FrameLayout
android:id="#+id/fragment_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
In this activity.xml had a frame layout.i try to add fragment in this frame layout. and i have two fragnent class.fragment one and two
This is my one_fragment.xml
<RelativeLayout
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">
<include layout="#layout/app_bar"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar"
android:layout_margin="16dp">
<android.support.v7.widget.RecyclerView
android:id="#+id/old_order_recycle"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
</RelativeLayout>
//two_fragment.xml
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.sirajmalayil.sneakers.OrderActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="2dp"
app:titleTextAppearance="#style/Toolbar.TitleText"
android:minHeight="?attr/actionBarSize"
android:background="#color/colorPrimaryDark"/>
<LinearLayout
android:id="#+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="20dp"
android:layout_below="#+id/toolbar"
android:background="#color/colorWhite">
<TextView
android:text="no active orders"
android:textAllCaps="true"
android:textStyle="bold"
android:layout_marginTop="20dp"
android:textSize="14sp"
android:fontFamily="sans-serif"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="There are no recent orders to show."
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:fontFamily="sans-serif"
android:textSize="12sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:src="#mipmap/footprints"
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="100dp" />
<TextView
android:id="#+id/txt_start_shopping"
android:text="start shopping"
android:background="#drawable/login_btn"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp"
android:layout_gravity="center"
android:textAllCaps="true"
android:textColor="#color/colorPink"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:id="#+id/txt_old_orders"
android:text="show my old orders"
android:gravity="center"
android:padding="10dp"
android:textAllCaps="true"
android:layout_margin="16dp"
android:textColor="#color/colorPink"
android:textStyle="bold"
android:background="#drawable/rectangle_colored_bg"
android:layout_below="#+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
//this is my two_fragment class
public class two_Fragment extends Fragment {
private TextView txtStartShoppi,txtoldOrders;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_active_orders,container,false);
txtStartShoppi = (TextView) rootView.findViewById(R.id.txt_start_shopping);
txtoldOrders = (TextView) rootView.findViewById(R.id.txt_old_orders);
Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
Drawable drawable = ContextCompat.getDrawable(getActivity(),R.drawable.ic_close_black_24dp);
toolbar.setNavigationIcon(drawable);
AppCompatActivity activity = (AppCompatActivity) getActivity();
activity.setSupportActionBar(toolbar);
ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
actionBar.setTitle("ORDERS");
toolbar.setTitleTextColor(Color.WHITE);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
clickListeners();
return rootView;
}
private void clickListeners() {
txtStartShoppi.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getActivity(),MainActivity.class));
}
});
txtoldOrders.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment fragment = new OldOrderFragment();
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.fragment_frame,fragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
}
}
//this is my activity class
public class OrderActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order);
if (savedInstanceState == null) {
Fragment fragment = new ActiveOrdersFragment();
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.fragment_frame, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
}
//this is my one_fragment
public class one_fragment extends Fragment {
RecyclerView recyclerView;
private List<Orders> ordersList = new ArrayList<>();
private List<products> productList = new ArrayList<>();
OldOrdersAdapter oldOrdersAdapter;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_old_orders,container,false);
Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
AppCompatActivity activity = (AppCompatActivity) getActivity();
activity.setSupportActionBar(toolbar);
ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
actionBar.setTitle("MY ARCHIVED ORDERS");
toolbar.setTitleTextColor(Color.WHITE);
actionBar.setDisplayHomeAsUpEnabled(true);
prepareOrderList();
recyclerView = (RecyclerView) rootView.findViewById(R.id.old_order_recycle);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
oldOrdersAdapter = new OldOrdersAdapter(getActivity(),ordersList);
recyclerView.setAdapter(oldOrdersAdapter);
return rootView;
}
private void prepareOrderList() {
Orders orders = new Orders("delivered","1245-5566-7890-1123","Placed on Sunday, December, 2017",
"Rs.750.45","Friday, January 5, 2018");
ordersList.add(orders);
orders = new Orders("delivered","1245-5566-7890-1123","Placed on Sunday, December, 2017",
"Rs.750.45","Friday, January 5, 2018");
ordersList.add(orders);
orders = new Orders("delivered","1245-5566-7890-1123","Placed on Sunday, December, 2017",
"Rs.750.45","Friday, January 5, 2018");
ordersList.add(orders);
orders = new Orders("delivered","1245-5566-7890-1123","Placed on Sunday, December, 2017",
"Rs.750.45","Friday, January 5, 2018");
ordersList.add(orders);
//oldOrdersAdapter.notifyDataSetChanged();
}
}
//this is my Adapter class
public class OldOrdersAdapter extends RecyclerView.Adapter<OldOrdersAdapter.MyViewHolder> {
Context context;
private List<Orders> ordersList = new ArrayList<>();
public OldOrdersAdapter(Context context, List<Orders> ordersList) {
this.context = context;
this.ordersList = ordersList;
}
#NonNull
#Override
public OldOrdersAdapter.MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_old_orders,parent,false);
return new MyViewHolder(rootView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Orders orders = ordersList.get(position);
holder.orderStatus.setText(orders.getOrder_status());
holder.orderNo.setText(orders.getOrder_no());
holder.orderPlacedDate.setText(orders.getOrder_placed_date());
holder.orderdItemPrice.setText(orders.getOrder_item_price());
holder.orderDeliverDate.setText(orders.getOrder_delivered_date());
}
#Override
public int getItemCount() {
return ordersList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
private TextView orderStatus,orderNo,orderPlacedDate,orderdItemPrice,orderDeliverDate;
public MyViewHolder(View itemView) {
super(itemView);
orderStatus = (TextView) itemView.findViewById(R.id.txt_order_status);
orderNo = (TextView) itemView.findViewById(R.id.txt_order_no);
orderPlacedDate = (TextView) itemView.findViewById(R.id.txt_order_placed_date);
orderdItemPrice = (TextView) itemView.findViewById(R.id.txt_order_price);
orderDeliverDate = (TextView) itemView.findViewById(R.id.txt_order_item_deliver_date);
}
}
}
Ok, all codes here.iam new in android . so i dont know whtais the problrm here. some time fragment and activity contents are mixing..
the problems are only in xml file.not in programattic code
Try this code..
set both fragment background color into root attribute it means Relative layout tag.
android:background="#android:color/white"
and card view refer this link it give good demo for that..
https://www.androidhive.info/2016/05/android-working-with-card-view-and-recycler-view/
add fragment into activity then call onCreate method in below code..
Fragment fragment = new MainFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_frame, fragment, fragment.getClass().getSimpleName()).addToBackStack(null).commit();
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
i set on click on the card view when i click on the card view the fragment is shown on the activity i assume that but the fragment is not shown the onclick of cardview is working but fragment is not shown i try alot but no solution i do not know why this happen. There is no error also but my fragment is not shown even no error Advance thanlks
here's code
Activity
public class SubmitAddActivity extends AppCompatActivity implements CallbackFromFragment {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.submitadd_layout);
findViewById(R.id.choose_category_layout).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("bullhead", "onClick: hellow rodl ");
fragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.container,fragmentUsage);
fragmentTransaction.commit();
fragmentUsage.setTYPE(1);
}
});
submitbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
settingToolbar();
}
#Override
public void setValues(int type, String value) {
if(type==1)
{
fragmentTransaction.remove(fragmentUsage);
category=value;
}
else if(type==2)
{
fragmentTransaction.remove(fragmentUsage);
location=value;
}
}
}
Activity layout
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/containerfragments"
android:fitsSystemWindows="true"
android:orientation="vertical"
tools:context="com.asad.taleembazar.activities.HomeActivity">
<include
layout="#layout/cardview_choose_category_submitadds"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/container"
/>
</FrameLayout>
</ScrollView>
fragment
public class SelectFragment extends Fragment implements com.asad.taleembazar.adpaters.callback {
private SelectCategorySubmitAddAdapter adapter;
private RecyclerView.LayoutManager layoutManager;
private ArrayList<String> arrayList=new ArrayList<>();
CallbackFromFragment communication;
int type;
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
public SelectFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_select_category_submit,container,false);
arrayList.add("Cars");
arrayList.add("Mobiles");
arrayList.add("Bags");
RecyclerView recyclerViewforsubmitadd = (RecyclerView)view.findViewById(R.id.recyclerview_for_categoriessubmit);
layoutManager = new LinearLayoutManager(getActivity());
recyclerViewforsubmitadd.setLayoutManager(layoutManager);
recyclerViewforsubmitadd.setHasFixedSize(true);
adapter = new SelectCategorySubmitAddAdapter(arrayList);
adapter.setOnClick(this);
recyclerViewforsubmitadd.setAdapter(adapter);
return view;
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
communication = (CallbackFromFragment) context;
}
#Override
public void onClick(int adapterPosition) {
if(type==1)
communication.setValues(1,arrayList.get(adapterPosition));
else if(type==2)
communication.setValues(1,arrayList.get(adapterPosition));
}
public void setTYPE(int i)
{
type=i;
}
}
match_parent of your container Framelayout won't work inside your ScrollView. You can either set android:fillViewport="true" in your ScrollView or You can set a fixed height for your container.
Try below code
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/containerfragments"
android:fitsSystemWindows="true"
android:orientation="vertical"
tools:context="com.asad.taleembazar.activities.HomeActivity">
<include
layout="#layout/cardview_choose_category_submitadds"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/container"
/>
</FrameLayout>
</ScrollView>