I have a simple layout, which contains a text view, ImageView, and a recycler view, If i set recycler's view width & height to 0dp, and connects its constraint to parent, it does not render the recycler view.
But if i define height in numbers then it works fine, and renders recyclerview
This is my code
<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/blue"
tools:context=".activities.MyActivityV2">
<include layout="#layout/content_more_activity" />
</android.support.design.widget.CoordinatorLayout>
Inner Layout
<android.support.constraint.ConstraintLayout 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=".activities.MyActivityV2"
tools:showIn="#layout/activity_more">
<ImageView
android:id="#+id/imageView23"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/logo" />
<TextView
android:id="#+id/tvLabelMore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="10dp"
android:letterSpacing="0.1"
android:text="MORE"
android:textAllCaps="true"
android:textColor="#color/white"
android:textSize="24dp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="#+id/imageView23"
app:layout_constraintTop_toTopOf="parent" />
<android.support.v7.widget.RecyclerView
android:id="#+id/rvMoreItems"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constrainedHeight="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView23" />
Class File
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
generateDataForRecyclerView();
initRecyclerView();
}
private void init() {
rvMoreItems= findViewById(R.id.rvMoreItems);
}
private void generateDataForRecyclerView() {
MoreDataClass moreDataClass= new MoreDataClass();
moreDataClass.setLabel("AAA");
moreDataClass.setHeaderItem(true);
dataList.add(moreDataClass);
dataList.add(moreDataClass);
}
private void initRecyclerView() {
MoreAdapter moreAdapter= new MoreAdapter(this);
moreAdapter.setData(dataList);
rvMoreItems.setLayoutManager(new LinearLayoutManager(this));
rvMoreItems.setAdapter(moreAdapter);
}
Update this function check below.
private void initRecyclerView() {
MoreAdapter moreAdapter= new MoreAdapter(this);
rvMoreItems.setLayoutManager(new LinearLayoutManager(this));
rvMoreItems.setAdapter(moreAdapter);
moreAdapter.setData(dataList);
}
Related
My issue is: ui freezes when recycler view adapter start listening
my xml Code is as below
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainAdminPackage.AdminDashboardActivity">
<!--Navigation Drawer Setup-->
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/home_background"
app:headerLayout="#layout/menu_header_design"
app:menu="#menu/main_menu" />
<LinearLayout
android:id="#+id/contentViewLl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/home_background"
android:orientation="vertical">
<!--Navigation Menu and Fab Layout Setup-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="68dp"
android:padding="20dp">
<!--Menu Button-->
<ImageView
android:id="#+id/menuIcon"
android:layout_width="48dp"
android:layout_height="48dp"
android:padding="4dp"
android:layout_centerVertical="true"
android:focusable="true"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:src="#drawable/ic_menu_icon_dark_gray"
app:tint="#color/icon_color"
android:contentDescription="#null"/>
<!--FAB Layout System-->
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/addQuestionPollCl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toEndOf="#+id/menuIcon"
android:minHeight="68dp">
</androidx.constraintlayout.widget.ConstraintLayout>
</RelativeLayout>
<!--Nested Scroll View-->
<androidx.core.widget.NestedScrollView
android:id="#+id/dashboardNestedScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<!--Main Container Layout-->
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--LAYOUT FOR SEARCH WINDOW AND START DIALOG-->
<RelativeLayout
android:id="#+id/searchAndSloganRl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintVertical_bias="0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
</RelativeLayout>
<!--CATEGORIES BUTTON-->
<LinearLayout
android:id="#+id/categoryButtonsLl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:orientation="horizontal"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintVertical_bias="0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/searchAndSloganRl"
app:layout_constraintBottom_toBottomOf="parent">
</LinearLayout>
<!--FEATURED COURSE LAYOUT-->
<RelativeLayout
android:id="#+id/featuredCourseRl"
android:visibility="visible"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_marginBottom="10dp"
android:background="#color/banner_background_light"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintVertical_bias="0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/categoryButtonsLl"
app:layout_constraintBottom_toBottomOf="parent">
<!--Banner-->
<!--Recycler View for Featured Courses-->
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/featuredCoursesRv"
android:layout_width="match_parent"
android:layout_height="300dp"
android:orientation="horizontal"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
android:layout_toEndOf="#id/featuredBackground"
android:background="#color/home_background"
tools:listitem="#layout/row_featured_courses" />
</RelativeLayout>
<!--NEWS FEED-->
<androidx.appcompat.widget.LinearLayoutCompat
android:visibility="visible"
android:id="#+id/newsFeedMiniLlc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:minHeight="100dp"
android:orientation="vertical"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintVertical_bias="0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/featuredCourseRl"
app:layout_constraintBottom_toBottomOf="parent">
<!--News Feed Recycler View-->
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/newsFeedRv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
android:orientation="horizontal"
android:background="#color/home_background"
tools:listitem="#layout/row_news_feed_dashboard" />
</androidx.appcompat.widget.LinearLayoutCompat>
<!--NOTES CATEGORIES-->
<androidx.appcompat.widget.LinearLayoutCompat
android:id="#+id/notesCategoryLlc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="vertical"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintVertical_bias="0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/newsFeedMiniLlc"
app:layout_constraintBottom_toBottomOf="parent">
</androidx.appcompat.widget.LinearLayoutCompat>
<!--ASK QUESTION RECYCLER VIEW LAYOUT-->
<androidx.appcompat.widget.LinearLayoutCompat
android:id="#+id/askedQuestionMiniLlc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:orientation="vertical"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintVertical_bias="0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/notesCategoryLlc"
app:layout_constraintBottom_toBottomOf="parent">
<!--Recycler View for Asked Questions-->
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/askQuestionRv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
android:orientation="vertical"
tools:itemCount="10"
android:nestedScrollingEnabled="false"
tools:listitem="#layout/row_asked_question" />
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
</LinearLayout>
</androidx.drawerlayout.widget.DrawerLayout>
java code for recycler views
//For hiding the FAB when nest scroll is scrolled
dashboardNestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener(){
#Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY){
//For Adapters to Start listening
if(scrollY - oldScrollY != 0) {
if(isVisible(newsFeedMiniLlc)) {
indexOtherPost++;
if(indexOtherPost == 1) {
adapterNewsFeedListFireStoreDashBoard.startListening();
Log.d("NESTED_CHILD", "onScrollChange: indexOtherPost: " + indexOtherPost);
}
}
//Log.d("NESTED_CHILD", "onScrollChange: otherPostMiniLlc: " + isVisible(otherPostMiniLlc));
if(isVisible(askedQuestionMiniLlc)) {
indexAskedQuestion++;
if(indexAskedQuestion == 1) {
adapterAskedQuestionsFireStore.startListening();
Log.d("NESTED_CHILD", "onScrollChange: indexAskedQuestion: " + indexAskedQuestion);
}
}
//Log.d("NESTED_CHILD", "onScrollChange: askedQuestionMiniLlc: " + isVisible(askedQuestionMiniLlc));
}
}
});
//Function for getting Visibility of any View or Layout on the Screen
private static boolean isVisible(final View view) {
if (view == null) {
return false;
}
if (!view.isShown()) {
return false;
}
final Rect actualPosition = new Rect();
view.getGlobalVisibleRect(actualPosition);
int widthPixels = Resources.getSystem().getDisplayMetrics().widthPixels;
int heightPixels = Resources.getSystem().getDisplayMetrics().heightPixels;
final Rect screen = new Rect(0, 0, widthPixels, heightPixels);
return actualPosition.intersect(screen);
}
//For LOADING ASK QUESTIONS
private void loadAskQuestions(){
//for question Recycler view
LinearLayoutManager askQuestionLayoutManager = new LinearLayoutManager(getApplicationContext());
askQuestionLayoutManager.setReverseLayout(false);
askQuestionLayoutManager.setStackFromEnd(false);
askQuestionLayoutManager.setOrientation(RecyclerView.VERTICAL);
askQuestionRv.setLayoutManager(askQuestionLayoutManager);
//get all Question limited to last 10 questions
Query query = FirebaseFirestore.getInstance()
.collection("Asked Questions")
.orderBy("questionId", Query.Direction.DESCENDING)
.limit(10);
FirestoreRecyclerOptions<ModelAskedQuestion> options = new FirestoreRecyclerOptions.Builder<ModelAskedQuestion>()
.setQuery(query, ModelAskedQuestion.class)
.build();
adapterAskedQuestionsFireStore = new AdapterAskedQuestionsFireStore(this, options);
askQuestionRv.setAdapter(adapterAskedQuestionsFireStore);
}
ui freezes when ask question recycler view adapter start listening and ui only freezes for few seconds until all data is loaded in recycler view.
Any one have any solutions ???
More elaboration of the Issue:
1)I have three recycler view under this layout: Two top most Horizontal and last one Vertical in direction.
2)As this is my main dashboard. I have put nested scroll view listener for adapters to start listening when the particular layout containing that recycler view appears on the screen. Top most two adapters and their corresponding recycler views are working perfectly fine.
But when the last recycler view which is vertical in Direction, appears on the screen, It freezes the entire UI, as its adapter starts listening, for few seconds(aprrox. 1-2 seconds).
In this, adapter loads only last 10 items from the server.
I have tried:
1). Fixing the hieght of recyler view.
2). Fixing the height of Layout containing that recycler view.
3). Changed the Main Container Layout From Linear Layout to Constraint Layout.(As It was suggested in one of the comments under that thread(mentioned at the end of this thread)).
4). Tried android:nestedScrollingEnabled="false".
5). Tried app:layout_behavior="#string/appbar_scrolling_view_behavior".
Nothing has worked for this particular problem.
How to get rid of this this freezing of Ui. It should kept scrolling along the items get recycled in recycler view.
It could be same question asked in this thread.
RecyclerView inside NestedScrollview alternative
I have a RecyclerView inside a fragment with a search filter edit text above it, and when I scroll the recyclerview go through the edit text, but I want the RecyclerView always to be under the edit text and leave the edit text on top like a header or something like that no matter how much I scroll. How can I achieve that?
code:
the layout
<androidx.constraintlayout.widget.ConstraintLayout 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:clipChildren="false"
android:clipToPadding="false"
android:orientation="vertical"
tools:context=".fragments.FoodListFragment">
<com.steelkiwi.library.view.BadgeHolderLayout
android:id="#+id/badge"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:layout_marginStart="8dp"
android:layout_marginBottom="8dp"
android:elevation="100dp"
app:bhl_badge_radius="10dp"
app:bhl_default_badge_background="#color/five"
app:bhl_text_color="#android:color/white"
app:bhl_text_size="12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/capsule_shopping_cart"
android:elevation="10dp"
app:srcCompat="#drawable/white_shopping_cart" />
</com.steelkiwi.library.view.BadgeHolderLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerview_foodlist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/food_list_search_input" />
<EditText
android:id="#+id/food_list_search_input"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:background="#drawable/search_input_style"
android:drawableStart="#drawable/search_icon"
android:drawablePadding="8dp"
android:ems="10"
android:hint="search"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
the class
public class FoodListFragment extends Fragment {
EditText etSearchInput;
private RecyclerView recyclerView;
private FoodListAdapter foodListAdapter;
private BadgeHolderLayout badgeHolderLayout;
private List<Food> foods;
public interface ListenerFoodListFragment {
void sendToCart(int value, int adapterPosition);
void cartClickListener();
}
private ListenerFoodListFragment listenerFoodListFragment;
#Override
public void onAttach(#NonNull Context context) {
super.onAttach(context);
try {
listenerFoodListFragment = (ListenerFoodListFragment) context;
} catch (ClassCastException e) {
Log.d("FoodListFragment", "onAttach: " + e.getMessage());
}
}
public FoodListFragment() {
// 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_food_list, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
recyclerView = view.findViewById(R.id.recyclerview_foodlist);
badgeHolderLayout = view.findViewById(R.id.badge);
etSearchInput = view.findViewById(R.id.food_list_search_input);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
foods = new ArrayList<>();
foods.add(new Food("Chicken", "99999$", R.drawable.chicken));
badgeHolderLayout.setOnClickListener(v -> listenerFoodListFragment.cartClickListener());
foodListAdapter = new FoodListAdapter(getActivity(), foods, listenerFoodListFragment);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(foodListAdapter);
etSearchInput.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
foodListAdapter.getFilter().filter(s);
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
public void updateCartValue(int value) {
badgeHolderLayout.setCountWithAnimation(value);
}
}
Remove this android:layout_marginTop="?attr/actionBarSize" from your Recyclerview
and then Set android:layout_height of your RecyclerView to 0dp will works.
Further more add app:layout_constraintBottom_toTopOf="#id/recyclerview_foodlist" in search EditText or it may be due to background drawable file you added in search EditText
You are using a ConstraintLayout.
match_parent will ignore your constraints.
You need to replace the layout_height="match_parent" of the RecyclerView with layout_height="0dp". Same for its width.
Then it will be underneath the EditText as you expect.
i fixed it by changing my layout to this:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:background="#color/white"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerview_foodlist"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/food_list_search_input" />
<EditText
android:id="#+id/food_list_search_input"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:background="#drawable/search_input_style"
android:drawableStart="#drawable/search_icon"
android:drawablePadding="8dp"
android:ems="10"
android:hint="search"
android:inputType="textPersonName"
android:textColor="#fff"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.steelkiwi.library.view.BadgeHolderLayout
android:id="#+id/badge"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:layout_marginStart="8dp"
android:layout_marginBottom="8dp"
android:elevation="100dp"
app:bhl_badge_radius="10dp"
app:bhl_default_badge_background="#color/five"
app:bhl_text_color="#android:color/white"
app:bhl_text_size="12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/capsule_shopping_cart"
android:elevation="10dp"
app:srcCompat="#drawable/white_shopping_cart" />
</com.steelkiwi.library.view.BadgeHolderLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Change your recyclerview height to wrap_content
android:layout_height="wrap_content"
And remove all this code in recyclerview
android:layout_marginTop="?attr/actionBarSize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/food_list_search_input"
At last paste this two line in recylerview
app:layout_constraintEnd_toEndOf="#id/food_list_search_input"
app:layout_constraintTop_toBottomOf="#+id/food_list_search_input"
I have tried many solutions found in StackOverflow now and it is not working on what I was expecting. I want to disable the Recycler View Scroll allowing only the Nested scrollView to scroll my content.
Below is my XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:clickable="true"
android:focusable="true"
android:background="#color/backgroundColor">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:fillViewport="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/clProfileInfoContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/background_divider"
android:paddingStart="#dimen/ps_container_padding_start"
android:paddingTop="#dimen/ps_container_padding_top"
android:paddingEnd="#dimen/ps_container_padding_end"
android:paddingBottom="#dimen/ps_container_padding_bottom"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/ivPhotoSelector"
android:layout_width="#dimen/ps_photo_selector_max_width"
android:layout_height="#dimen/ps_photo_selector_max_height"
android:contentDescription="#string/cd_photo_selector"
android:maxWidth="#dimen/ps_photo_selector_max_width"
android:maxHeight="#dimen/ps_photo_selector_max_height"
android:src="#drawable/blue_profile_img_placeholder"
app:civ_circle_background_color="#color/colorAccent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.LinearLayoutCompat
android:id="#+id/llUsernameContainer"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginStart="#dimen/ps_username_margin_start"
android:layout_marginTop="#dimen/ps_username_margin_top"
app:layout_constraintStart_toEndOf="#+id/ivPhotoSelector"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/tvUserName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center_vertical"
android:text="Username"
android:textColor="#color/tvMainTextColor"
android:textSize="#dimen/ps_username_text_size"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="#+id/ivPhotoSelector"
app:layout_constraintTop_toTopOf="#+id/ivPhotoSelector" />
<androidx.appcompat.widget.AppCompatImageButton
android:id="#+id/btnEditProfile"
style="#style/AppTheme.Button.Blue"
android:adjustViewBounds="true"
android:layout_width="wrap_content"
android:layout_height="#dimen/small_button_height"
android:layout_gravity="end"
android:src="#drawable/edit_icon"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.appcompat.widget.LinearLayoutCompat>
<androidx.appcompat.widget.LinearLayoutCompat
android:id="#+id/llLocationContainer"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2"
android:layout_marginStart="#dimen/ps_location_margin_start"
android:layout_marginTop="#dimen/ps_location_margin_top"
app:layout_constraintStart_toEndOf="#+id/ivPhotoSelector"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/llUsernameContainer" >
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/tvLocation"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Location"
android:textColor="#color/tvSubTextColor"
android:textSize="#dimen/ps_location_text_size"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/btnShare"
style="#style/AppTheme.Button.Blue"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="#dimen/small_button_height"
android:minWidth="#dimen/small_share_button_min_width"
android:text="#string/ps_label_share_profile"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.appcompat.widget.LinearLayoutCompat>
<me.zhanghai.android.materialratingbar.MaterialRatingBar
android:id="#+id/rbRating"
style="#style/Widget.MaterialRatingBar.RatingBar.Indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/ps_star_rating_margin_start"
android:layout_marginTop="#dimen/ps_star_rating_margin_top"
android:layout_marginEnd="#dimen/ps_star_rating_margin_end"
android:maxHeight="#dimen/ps_star_rating_max_height"
android:minHeight="#dimen/ps_star_rating_min_height"
android:numStars="5"
android:rating="4"
app:layout_constraintStart_toEndOf="#+id/ivPhotoSelector"
app:layout_constraintTop_toBottomOf="#+id/llLocationContainer"
app:mrb_fillBackgroundStars="true"
app:mrb_progressBackgroundTint="#color/rbBackgroundColor"
app:mrb_progressTint="#color/rbProgressBackgroundColor" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/clListingContainer"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#android:color/white"
android:paddingStart="#dimen/ps_container_padding_start"
android:paddingTop="#dimen/ps_container_padding_top"
android:paddingEnd="#dimen/ps_container_padding_end"
android:paddingBottom="#dimen/ps_container_padding_bottom"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/clProfileInfoContainer"
app:layout_constraintBottom_toBottomOf="parent">
<androidx.appcompat.widget.AppCompatEditText
android:id="#+id/etShortBio"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="#dimen/ps_bio_min_height"
android:gravity="top"
android:padding="#dimen/ps_bio_padding"
android:text="Short Bio About Yourself"
android:inputType="textMultiLine"
android:clickable="false"
android:focusable="false"
android:enabled="false"
android:textColor="#color/labelColor"
android:background="#drawable/white_background"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvListing"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="#dimen/ps_listing_margin_top"
android:paddingTop="#dimen/ps_listing_padding_top"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/etShortBio"
tools:listitem="#layout/row_user_listing_item">
</androidx.recyclerview.widget.RecyclerView>
<com.github.ybq.android.spinkit.SpinKitView
android:id="#+id/skUserListingLoading"
style="#style/SpinKitView.Wave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:SpinKit_Color="#color/colorPrimaryDark"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/rvListing" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
and in my activity, I set this to disable the scroll of recycler view
private void setupListing() {
// rvMenuList should not be null
assert rvListing != null;
// initialize Recycler View Adapter
userListingAdapter = new UserListingAdapter(getContext());
// initialize GridLayoutManager
GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), gridSize);
// initialize custom divider item decorator
SpacesItemDecoration itemDecorator = new SpacesItemDecoration(10, 10, 30);
rvListing.addItemDecoration(itemDecorator);
ViewCompat.setNestedScrollingEnabled(rvListing, false);
// set the adapter to the recycler view
rvListing.setAdapter(userListingAdapter);
// set the layout manager to the recycler view
rvListing.setLayoutManager(gridLayoutManager);
}
Here's how it looks like
Now I can't scroll the recycler view because of using this code ViewCompat.setNestedScrollingEnabled(rvListing, false); and also I can't scroll the whole content to view the other items of my recycler view.
this may work for you, apply layout behavior:
<android.support.v7.widget.RecyclerView
android:id="#+id/conversation"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Instead of using this line:
ViewCompat.setNestedScrollingEnabled(rvListing, false);
Use this line:
rvListing.setNestedScrollingEnabled(false);
after setting the adapter like this:
private void setupListing() {
// rvMenuList should not be null
assert rvListing != null;
// initialize Recycler View Adapter
userListingAdapter = new UserListingAdapter(getContext());
// initialize GridLayoutManager
GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), gridSize);
// initialize custom divider item decorator
SpacesItemDecoration itemDecorator = new SpacesItemDecoration(10, 10, 30);
rvListing.addItemDecoration(itemDecorator);
// set the adapter to the recycler view
rvListing.setAdapter(userListingAdapter);
// set the layout manager to the recycler view
rvListing.setLayoutManager(gridLayoutManager);
rvListing.setHasFixedSize(true);
rvListing.setNestedScrollingEnabled(false);
}
use RecyclerView width
android:layout_height="wrap_content"
finally got the solution. the rvListing.setNestedScrollingEnabled(false); actually works. the issue was the parent height i accidentally set it to 0dp which is suppose to be wrap_content. that was the fix of this issue.
Just override the GridLayoutManager as below, and invoke the method setCanScroll(false).
public class CustomLayoutManager extends GridLayoutManager {
private boolean canScroll;
public CustomLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
/**
*
* #param canScroll true--enable scroll, false--disable scroll
*/
public void setCanScroll(boolean canScroll) {
this.canScroll = canScroll;
}
#Override
public boolean canScrollHorizontally() {
return this.canScroll;
}
#Override
public boolean canScrollVertically() {
return false;
}
}
To disable scrolling of RecyclerView, you can do it programmatically:
recyclerView.setNestedScrollingEnabled(false);
Or through xml:
<android.support.v7.widget.RecyclerView
android:nestedScrollingEnabled="false"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
But be careful when your RecyclerView contain many items. It will cause lag
Disable RecyclerView scrolling is bad practice, use RecyclerView with multiple item types instead!
Background
I've been trying to get parent-to-child navigational transition implemented (specifically for recyclerview items -> detail fragment). I'm currently using sharedElementTransitions with the item container and detail container as the sharedElement along with ChangeTransform and ChangeBounds transitions. The detail fragment's enter transition is mostly working. However, I am having problems with the recyclerview item's exit transition. What I want to do is have the recyclerview dim/turn darker while the detail view expands to full screen. When I try to use a custom transition, the exit transition never animates. Instead, the detail view is immediately shown and its enter transition animates.
As you can see, the recyclerview fragment is hidden before the exit animation is played. I need the recyclerview to be shown with the dimming effect while the detail view is expanding.
Problem
How do I get the dim custom exit transition to play while the detail enter transition is also playing?
What I've tried
setDelay - setDelay on detail enter transition. The transition is delayed but the detail fragment immediately shown (the recyclerview is blocked off by the new fragment).
setDuration - setDuration on recyclerview exit transition. Detail fragment still blocks the recyclerview.
ItemViewModel.java
public void displayArticle(View view) {
MainActivity mainActivity = (MainActivity) view.getContext();
ArticleFragment detailsFragment = ArticleFragment.newInstance(article.getValue(), transitionName.getValue());
ArticleListingFragment itemFragment = (ArticleListingFragment) mainActivity.getSupportFragmentManager().findFragmentByTag(ArticleListingFragment.TAG);
doOpenArticleTransition(detailsFragment, itemFragment);
mainActivity.getSupportFragmentManager()
.beginTransaction()
.addSharedElement(view.findViewById(R.id.article_entry), transitionName.getValue())
.replace(R.id.main_content_container, detailsFragment)
.addToBackStack(null)
.commit();
}
private void doOpenArticleTransition(Fragment newFragment, Fragment oldFragment) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Transition openArticleTransition = TransitionInflater.from(oldFragment.getContext()).inflateTransition(R.transition.transition_article);
newFragment.setSharedElementEnterTransition(openArticleTransition);
newFragment.setEnterTransition(new Fade());
oldFragment.setExitTransition(new Dim(oldFragment.getContext()));
newFragment.setSharedElementReturnTransition(openArticleTransition);
}
}
Dim.java
public class Dim extends BaseTransition {
private static final String PROPNAME_BACKGROUND = "meridian:dim:background";
private final Context context;
public Dim(Context context) {
this.context = context;
}
public Dim(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
#Override
public void captureValues(TransitionValues transitionValues) {
Drawable background = transitionValues.view.getBackground();
transitionValues.values.put(PROPNAME_BACKGROUND, background);
}
#Override
public Animator createAnimator(ViewGroup sceneRoot, TransitionValues startValues, TransitionValues endValues) {
final View view;
if (startValues == null && endValues != null) {
view = endValues.view;
} else {
view = startValues.view;
}
int whiteBackground = ContextCompat.getColor(context, R.color.white);
int grayBackground = ContextCompat.getColor(context, R.color.gray700);
ValueAnimator animator = ValueAnimator.ofArgb(whiteBackground, grayBackground);
animator.setDuration(400);
animator.setStartDelay(300);
animator.addUpdateListener(animation -> view.setBackgroundColor((Integer)animation.getAnimatedValue()));
return animator;
}
}
transition_article.xml
<?xml version="1.0" encoding="utf-8"?>
<transitionSet
xmlns:android="http://schemas.android.com/apk/res/android"
android:transitionOrdering="together">
<changeBounds/>
<changeTransform/>
<transition
class="meridian.custom.anim.Elevate"/>
</transitionSet>
recyclerview_item.xml
<android.support.constraint.ConstraintLayout
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/article_entry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground">
<TextView
android:id="#+id/article_listing_section"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/two_line_list_entry_margin_lr"
android:layout_marginEnd="#dimen/two_line_list_entry_margin_lr"
android:layout_marginTop="#dimen/two_line_list_entry_margin_tb"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="#id/article_listing_section_title_barrier"
app:layout_constraintTop_toTopOf="parent"
style="#style/EntryOverline"
tools:text="#tools:sample/cities" />
<TextView
android:id="#+id/article_listing_date"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/two_line_list_entry_margin_tb"
android:layout_marginEnd="#dimen/two_line_list_entry_margin_lr"
android:textAlignment="viewEnd"
app:layout_constraintEnd_toStartOf="#+id/article_listing_date_image_barrier"
app:layout_constraintTop_toTopOf="parent"
style="#style/EntryOverlineGray"
tools:text="17h" />
<TextView
android:id="#+id/article_listing_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/two_line_list_entry_margin_tb"
android:layout_marginEnd="#dimen/two_line_list_entry_margin_lr"
android:layout_marginTop="#dimen/two_line_list_entry_first_line_second_line_spacing"
app:layout_constraintStart_toStartOf="#id/article_listing_section"
app:layout_constraintEnd_toStartOf="#id/article_listing_section_title_barrier"
app:layout_constraintTop_toBottomOf="#id/article_listing_section"
app:layout_constraintBottom_toBottomOf="parent"
android:minLines="3"
android:maxLines="3"
style="#style/EntrySubtitle1"
tools:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." />
<ImageView
android:id="#+id/article_listing_image"
android:layout_width="#dimen/two_line_list_entry_image_width"
android:layout_height="#dimen/two_line_list_entry_image_width"
android:layout_marginStart="#dimen/two_line_list_entry_margin_lr"
android:layout_marginEnd="#dimen/two_line_list_entry_margin_lr"
android:contentDescription="#string/content_description_image_article_entry"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/article_listing_date_image_barrier"
app:layout_constraintTop_toTopOf="parent"
android:src="#drawable/item_article_entry_image_placeholder" />
<android.support.constraint.Barrier
android:id="#+id/article_listing_section_title_barrier"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="start"
app:constraint_referenced_ids="article_listing_date"/>
<android.support.constraint.Barrier
android:id="#+id/article_listing_date_image_barrier"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="start"
app:constraint_referenced_ids="article_listing_image"/>
</android.support.constraint.ConstraintLayout>
recyclerview_fragment.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_article_sections"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.CoordinatorLayout
android:id="#+id/coordinator_layout_articles"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#layout/toolbar" />
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipe_to_refresh_articles"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/articles"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:itemCount="2"
tools:layoutManager="android.support.v7.widget.LinearLayoutManager"
tools:listitem="#layout/item_article_entry" />
</android.support.v4.widget.SwipeRefreshLayout>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view_article_sections"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:itemTextAppearance="#style/SectionSubtitle1"
app:menu="#menu/navigation_article_topic"
app:headerLayout="#layout/navigation_header_article_sections"/>
details_fragment.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:layout_width="match_parent"
android:layout_height="match_parent"
app:elevation="24dp">
<include
layout="#layout/toolbar" />
<android.support.v4.widget.NestedScrollView
android:id="#+id/article_scroll_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<android.support.constraint.ConstraintLayout
android:id="#+id/article"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".ui.article.display.ArticleFragment">
<HorizontalScrollView
android:id="#+id/article_description_scroll_container"
style="#style/HorizontalChipScroller"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:scrollbars="none"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.design.chip.ChipGroup
android:id="#+id/article_description_chip_group"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:contentDescription="#string/chip_group_description_tags"
app:singleLine="true"/>
</HorizontalScrollView>
<ImageView
android:id="#+id/article_image"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="16dp"
android:layout_marginEnd="#dimen/detail_general_margin_lr"
android:layout_marginStart="#dimen/detail_general_margin_lr"
android:layout_marginTop="#dimen/detail_general_margin_tb"
android:contentDescription="#string/content_description_image_article"
android:scaleType="centerCrop"
android:src="#drawable/item_article_entry_image_placeholder"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.45"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/article_description_scroll_container"
tools:layout_height="208dp"
tools:src="#color/colorImagePlaceholder" />
<TextView
android:id="#+id/article_section"
style="#style/DetailOverline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="#dimen/detail_general_margin_lr"
android:layout_marginStart="#dimen/detail_general_margin_lr"
android:layout_marginTop="#dimen/detail_general_margin_tb"
app:layout_constraintEnd_toStartOf="#id/article_subsection"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/article_image"
tools:text="US"
tools:textColor="#color/gray900" />
<TextView
android:id="#+id/article_subsection"
style="#style/DetailOverline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/detail_general_margin_lr"
android:layout_marginTop="#dimen/detail_general_margin_tb"
app:layout_constraintStart_toEndOf="#id/article_section"
app:layout_constraintTop_toBottomOf="#id/article_image"
tools:text="Poverty"
tools:textColor="#color/gray900" />
<TextView
android:id="#+id/article_title"
style="#style/DetailH6Headline"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/detail_general_margin_tb"
android:layout_marginEnd="#dimen/detail_general_margin_lr"
android:layout_marginStart="#dimen/detail_general_margin_lr"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/article_section"
tools:text="Pope Accepts Wuerl's Resignation as Washington Archbishop, but Calls Him a Model Bishop"
tools:textColor="#color/gray900"
tools:textSize="20sp"
tools:textStyle="bold" />
<TextView
android:id="#+id/article_abstract"
style="#style/DetailBody2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/detail_general_margin_tb"
android:layout_marginEnd="#dimen/detail_general_margin_lr"
android:layout_marginStart="#dimen/detail_general_margin_lr"
android:layout_marginTop="#dimen/detail_general_margin_tb"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/article_title"
tools:text="Despite demands to oust Cardinal Wuerl over sexual abuse scandals, Pope Francis praised him as a model leader."
tools:textColor="#color/gray600" />
<TextView
android:id="#+id/article_author"
style="#style/DetailBody1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/detail_general_margin_tb"
android:layout_marginEnd="#dimen/detail_general_margin_lr"
android:layout_marginStart="#dimen/detail_general_margin_lr"
android:layout_marginTop="#dimen/detail_general_margin_tb"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/article_abstract"
tools:text="by Jason Horowitz, Elizabeth Dias and Laurie Goodstein"
tools:textColor="#color/gray900" />
<android.support.design.button.MaterialButton
android:id="#+id/article_full_content_button"
style="#style/OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/detail_general_margin_tb"
android:layout_marginEnd="#dimen/detail_general_margin_lr"
android:layout_marginStart="#dimen/outlined_button_padding_lr"
android:layout_marginTop="#dimen/detail_general_margin_tb"
android:text="#string/button_label_article_read_more"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/article_author" />
</android.support.constraint.ConstraintLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
DetailsFragment.java
#BindView(R.id.article)
ConstraintLayout container;
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setupToolbar();
setTransitionName();
loadArticle();
}
private void setTransitionName() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
container.setTransitionName(this.getArguments().getString(ARG_TRANSITION));
}
}
I was playing around with the Shared Element Transition and developed a testapp with 4 Fragments.
In the picture you can see, the first Fragment contains a "start now" message, when it's clicked I want to replace the container with the Fragment in the middle. As an eye candy I want an Animation by using Shared Element Transitions.
Problem
My Problem is, if I leave the first Fragment empty (without starting message) and I for example set the OnClickListener to the Icon itself, everything is working alright with a nice animation. But if the first Fragment has that message in it only the first Icon (the second Fragment, picture in mid) doesn't have an animation anymore. It's just replacing the first fragment. The curios thing is, if I change my OnClickListener and let it start the second page (right picture) the animation is working fine again. So only the first/left Icon does not provide an animation though all Methods and XML are mostly 1:1 the same.
As I couldn't develope a better solution, each "toolbar" is designed in the Fragment itself.
Main Activity
public class FirstStartupActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_firststartup);
doFragmentTransaction(new MainFragment(), "TAG", false, null);
}
public void doFragmentTransaction(Fragment fragment, String tag, boolean addToBackStack, List<View> sharedElements){
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.ActivityFirstStartup_fragmentContainer, fragment, tag);
if(addToBackStack){
transaction.addToBackStack(tag);
}
if( sharedElements != null && !sharedElements.isEmpty()){
for(int i = 0; i < sharedElements.size(); i++){
View view = sharedElements.get(i);
transaction.addSharedElement(view, view.getTransitionName());
}
}
transaction.commit();
}}
First Fragment with Message
public class MainFragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_firststartup_home, container, false);
View view = v.findViewById(R.id.relLayoutPageOne);
final List<View> listview = new ArrayList<>();
listview.add(view);
View view2 = v.findViewById(R.id.relLayoutPageTwo);
final List<View> listview2 = new ArrayList<>();
listview2.add(view2);
Button button = v.findViewById(R.id.buttonStart);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((FirstStartupActivity)getActivity()).doFragmentTransaction(new UsernameFragment(), "test", true, listview);
//((FirstStartupActivity)getActivity()).doFragmentTransaction(new CameraFragment(), "TEST2", true, listview2);
}
});
RelativeLayout rel2 = v.findViewById(R.id.relLayoutPageTwo);
rel2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((FirstStartupActivity)getActivity()).doFragmentTransaction(new CameraFragment(), "TEST2", true, listview2);
}
});
return v;
}}
Second Fragment No Animation
public class UsernameFragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_firststartup_pgone, container, false);
postponeEnterTransition();
return v;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
setSharedElementEnterTransition(TransitionInflater.from(getContext()).inflateTransition(android.R.transition.move));
}
}}
Third Fragment Working
public class CameraFragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_firststartup_pgtwo, container, false);
return v;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
setSharedElementEnterTransition(TransitionInflater.from(getContext()).inflateTransition(android.R.transition.move));
}
}}
Second Fragment (Picture in middle) No Animation when replacing)
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.constraint.ConstraintLayout
android:id="#+id/constraintLayout2"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#drawable/draw_login_edittext">
</android.support.constraint.ConstraintLayout>
<RelativeLayout
android:id="#+id/relLayoutPageTwo"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginTop="4dp"
android:background="#drawable/draw_login_edittext_rounded"
android:transitionName="ProfileCam"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:transitionName="ProfileCam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitStart"
app:srcCompat="#mipmap/testtwo" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/relLayoutPageThree"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:background="#drawable/draw_login_edittext_rounded"
android:transitionName="ProfileGender"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.95"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitStart"
app:srcCompat="#mipmap/testthree" />
</RelativeLayout>
<RelativeLayout
android:transitionName="ProfilePic"
android:layout_width="273dp"
android:layout_height="210dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#drawable/draw_login_edittext_rounded"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/constraintLayout2">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitStart"
app:srcCompat="#mipmap/test" />
</RelativeLayout>
First Fragment (Picture left)
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.constraint.ConstraintLayout
android:id="#+id/constraintLayout2"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#a8655c">
</android.support.constraint.ConstraintLayout>
<RelativeLayout
android:transitionName="ProfilePic"
android:id="#+id/relLayoutPageOne"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:background="#drawable/draw_login_edittext_rounded"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.049"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitStart"
app:srcCompat="#mipmap/test" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/relLayoutPageTwo"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:background="#drawable/draw_login_edittext_rounded"
android:transitionName="ProfileCam"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitStart"
app:srcCompat="#mipmap/testtwo" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/relLayoutPageThree"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:background="#drawable/draw_login_edittext_rounded"
android:transitionName="ProfileGender"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.95"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitStart"
app:srcCompat="#mipmap/testthree" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:forceHasOverlappingRendering="true"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#drawable/draw_login_edittext_rounded"
android:padding="25dp"
android:transitionName="ProfilePic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/constraintLayout2">
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="Bevor du loslegen könnst benötigen wir noch kurz ein paar Informationen über dich! :)"
android:textAlignment="center"
android:textColor="#BFFFFFFF" />
<Button
android:id="#+id/buttonStart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/text"
android:layout_centerHorizontal="true"
android:layout_marginHorizontal="15dp"
android:layout_marginTop="15dp"
android:background="#drawable/draw_rounded_edittext_dark"
android:text="Start now"
android:textColor="#BFFFFFFF" />
</RelativeLayout>
Third Fragment (right Picture) Animation when replacing
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.constraint.ConstraintLayout
android:id="#+id/constraintLayout2"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#drawable/draw_login_edittext">
</android.support.constraint.ConstraintLayout>
<RelativeLayout
android:id="#+id/relLayoutPageOne"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:background="#drawable/draw_login_edittext_rounded"
android:transitionName="ProfilePic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.049"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitStart"
app:srcCompat="#mipmap/test" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/relLayoutPageThree"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:background="#drawable/draw_login_edittext_rounded"
android:transitionName="ProfileGender"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.95"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitStart"
app:srcCompat="#mipmap/testthree" />
</RelativeLayout>
<RelativeLayout
android:layout_width="273dp"
android:layout_height="210dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#c8c8c8"
android:transitionName="ProfileCam"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/constraintLayout2">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitStart"
app:srcCompat="#mipmap/testtwo" />
</RelativeLayout>
Visual Description
First Icon doesn't work but the second one works like charm
For some Reason the first GIF is way too fast, its popping up normally the only Problem is the missing Animation
Well I tried to change some things and I made everything worse. Then I "repaired" it and now it works, unfortunetly I can't show you a solution for that. I try to work through the codes again
EDIT: #LieForBanana had the solution but I understood him wrong, indeed my first Fragment also had the Transition Name it (3x TransitionName ProfilePic). I am ashamed, it was just one silly mistake