I have a NestedScrollView containing a LinearLayout and a RecyclerView (both inside a RelativeLayout).
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scroll">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/lay_account">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:paddingTop="10dp"
android:id="#+id/lay_total"
android:paddingBottom="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/total"
android:id="#+id/lblTotal"
android:textSize="#dimen/text_medium"
android:layout_marginLeft="20dp"
android:textColor="#color/dark_eurakostheme_color"
android:textStyle="bold"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtTotalAmount"
android:textSize="#dimen/text_extra_extra_large"
android:gravity="center_horizontal"
android:textColor="#color/eurakostheme_color"/>
<ImageView
android:layout_width="#dimen/icon_extra_extra_large"
android:layout_height="match_parent"
android:id="#+id/imageView3"
android:src="#drawable/coin_eurakos"/>
</LinearLayout>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:id="#+id/divisor"
android:background="#color/dividerColor"
android:layout_alignBottom="#+id/lay_total"/>
<android.support.v7.widget.RecyclerView
android:layout_below="#id/lay_total"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rclBasketAmounts"
android:background="#android:color/white"
android:padding="10dp">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
After loading the data into the RecyclerView, I need to change it's height programmatically like this:
RelativeLayout.LayoutParams lay_params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
50 + ((int) (baskets.length * getResources().getDimension(R.dimen.list_height_small))));
lay_params.addRule(RelativeLayout.BELOW, lay_total.getId());
recyclerView.setLayoutParams(lay_params);
recyclerView.setAdapter(new ListBasketAmountAdapter(baskets));
The problem is when the data has finished loading the NestedScrollView scrolls automatically scrolls to the top of the RecyclerView, hiding the LinearLayout above it. Any ideas?
Thanks.
After several days I've found a solution to the problem. You just need to add the descendantFocusability in the first layout under the ScrollView like this:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/lay_account"
android:descendantFocusability="blocksDescendants">
The solution given by kevings14 works but it wasn't helpful in my case. I've a RecyclerView + a couple of EditText under NestedScrollView, when I added
android:descendantFocusability="blocksDescendants"
in main layout under scroll view. I was unable to focus EditText.
Then I came up with this solution, which worked for me.
<RelativeLayout
android:layout_width="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_height="wrap_content">
In my case, I use multiple RecyclerView inside NestedScrollView. Then
it does not work for me. So my solution is, When my data update complete then I just scroll to top,
yourList.setAdapter(yourListAdapter);
scrollView.post(new Runnable() {
#Override
public void run() {
scrollView.fullScroll(ScrollView.FOCUS_UP);
//scrollView.scrollTo(0,0);
}
});
I had a nestedscrollview that would get scrolled up by itself. In the post run method of the recyclerview, I scrolled the nestedscrollview to (0,0) and all good.
rvComments.setAdapter(adapterComment);
rvComments.post(new Runnable() {
#Override
public void run() {
nestedScrollView.scrollTo(0, 0);
}
});
Related
Display pic
In my app, i want to increase the height of recycler view dynamically. As i add new item under complaints, the height of recycler view should increase. Is there a way to do it? Please help me with this. Thanks in advance.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/complaint_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/background_line"
android:padding="#dimen/layout_margin"
android:text="Complaints"
android:textColor="#color/textColorFullBlack" />
<LinearLayout
android:id="#+id/complaints_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="#dimen/layout_margin"
android:layout_weight="1">
<android.support.v7.widget.RecyclerView
android:id="#+id/complaint_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/layout_margin"
android:divider="#color/toolBarBackground"
android:groupIndicator="#android:color/transparent" />
</LinearLayout>
<TextView
android:id="#+id/instructions_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/background_line"
android:padding="#dimen/layout_margin"
android:text="Instructions"
android:textColor="#color/textColorFullBlack" />
<FrameLayout
android:id="#+id/instructions_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="#dimen/layout_margin"
android:layout_weight="1">
<android.support.v7.widget.RecyclerView
android:id="#+id/instructions_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/layout_margin"
android:divider="#color/toolBarBackground"
android:groupIndicator="#android:color/transparent" />
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#color/colorPrimary"
android:orientation="horizontal">
<in.palmpower.videocon.uicommon.widget.EditText
android:id="#+id/search_text_field"
style="#style/FormEditText"
android:layout_width="0dp"
android:layout_gravity="center_vertical"
android:layout_margin="#dimen/one_dp"
android:layout_weight="1"
android:background="#android:color/white"
android:inputType="textFilter"
android:padding="#dimen/search_edit_text_padding"
android:privateImeOptions="nm" />
<Button
android:id="#+id/add_button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:background="?attr/selectableItemBackground"
android:contentDescription="#string/edittext_hint_search"
android:padding="#dimen/button_padding"
android:text="#string/add_button_string"
android:textColor="#color/textColorWhite" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Got stuck on same issue, i was using RecyclerView with wrap_content inside LinearLayout, i change LinearLayout to RelativeLayout and it set RecyclerView Height Based On Children.
This will work for you
RecyclerView mRecyclerView
= (RecyclerView) findViewById(R.id.complaint_recycler_view);
int originalItemSize = iteList.size();
ViewGroup.LayoutParams params = mRecyclerView.getLayoutParams();
if (itemList.size() > originalItemSize ){
params.height = params.height + 100;
originalItemSize = itemList.size();
mRecyclerView.setLayoutParams(params);
}
i suggest you to put teh recyclerview in any other layout (Relative layout is preferable). Then change recyclerview's height/width as match parent to that layout and set teh parent layout's height/width as wrap content. it works for me
WRAP_CONTENT will not work if you want recyclerView to take part of screen.
If you want other content to be there below recyclerView, you can put recyclerView inside LinearLayout and use the following code to change the height dynamically.
I assume you are using a list(complaint_list) for storing complaints.Adjust the complaint_height accordingly
ViewGroup.LayoutParams params = mRecyclerView.getLayoutParams();
int complaint_height=400;
if (complaints_list.size() > 2) {
params.height = ((complaint_list.size() - 1) * 100) + complaint_height;
} else {
params.height = complaint_height+100;
}
mRecyclerView.setLayoutParams(params);
I want to hide two views (a Viewpager and a LinearLayout) at once when a Button is clicked. I tried using android:animateLayoutChanges="true", but it will always start hiding the Viewpager first and then the LinearLayout, which doesn't look good.
How can I manually hide both views at the same time? I have seen examples like this were the visibility of one view is changed:
myImageView.animate()
.translationY(myImageView.getHeight())
.alpha(0.0f)
.setDuration(300)
.setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
myImageView.setVisibility(View.GONE);
}
});
Is there a way to chain two animate() calls of different Viewsto trigger them simultaneously?
EDIT: This is my layout file, the Views I want to hide at the same time are the Viewpagerwith the id viewpager and the LinearLayout with the id ll_indicators:
<?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:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:fitsSystemWindows="true"
android:orientation="vertical">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
<include
android:id="#+id/tv_statusbar"
layout="#layout/layout_statusbar"
android:layout_width="match_parent"
android:layout_height="#dimen/statusbar_height" />
<TextView
android:id="#+id/tv_title"
android:layout_width="match_parent"
android:textColor="#color/black"
android:text="#string/some_text"
android:padding="10dp"
android:textSize="#dimen/fontSizeMedium"
android:gravity="center_horizontal"
android:layout_height="wrap_content" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<LinearLayout
android:id="#+id/ll_indicators"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal">
<ImageView
android:id="#+id/intro_indicator_0"
android:layout_width="8dp"
android:layout_height="8dp"
android:layout_marginEnd="#dimen/activity_margin_half"
android:layout_marginRight="#dimen/activity_margin_half"
android:background="#drawable/indicator_unselected" />
<ImageView
android:id="#+id/intro_indicator_1"
android:layout_width="8dp"
android:layout_height="8dp"
android:layout_marginEnd="#dimen/activity_margin_half"
android:layout_marginRight="#dimen/activity_margin_half"
android:background="#drawable/indicator_unselected" />
</LinearLayout>
<include
android:id="#+id/ll_login"
android:layout_weight="0"
layout="#layout/item_login"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</include>
</LinearLayout>
Since you didn't post your layout, if you can put your ViewPager and LinearLayout in one layout, (for example inside a RelativeLayout) then you can animate that.
if( A.isShowing && B.isSowing(
A.hide
b.hide
)
Maybe this?
You can either just animate the parent LinearLayout, all Views within will animate with it :
yourLinearLayout.animate().setDuration(300).alpha(0). ...
, or call animate() on two Views simultaneously :
yourImageView.animate().alpha(0). ...
yourLinearLayout.animate().alpha(0). ...
which would look like they are disappearing at the same time.
This is my xml:
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipeRefreshLayout"
android:layout_below="#id/id_bar"
android:layout_marginBottom="0.5dp"
android:background="#color/blue"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/list"
android:dividerHeight="0dp"
android:layout_gravity="top"
android:divider="#null"
android:background="#color/yellow"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<com.bartoszlipinski.recyclerviewheader2.RecyclerViewHeader
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="70dp"
android:visibility="gone"
android:layout_gravity="center_horizontal|top">
<include
android:id="#+id/header_profile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/rounded_top_green"
layout="#layout/header_profile"/>
</com.bartoszlipinski.recyclerviewheader2.RecyclerViewHeader>
<RelativeLayout
android:id="#+id/placeholderContainer"
android:layout_width="match_parent"
android:visibility="gone"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:layout_height="match_parent">
<ImageView
android:id="#+id/placeholderPic"
android:layout_alignParentTop="true"
android:layout_marginTop="100dp"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/pin_newtrips"/>
<TextView
android:id="#+id/placeholderText"
android:layout_below="#id/placeholderPic"
android:padding="10dp"
android:textSize="28dp"
android:textColor="#color/gray32"
android:text="#string/home.status.heading.setup"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RelativeLayout
android:id="#+id/placeholderAddItems"
android:layout_marginTop="10dp"
android:layout_below="#id/placeholderText"
android:layout_width="match_parent"
android:layout_height="wrap_content"></RelativeLayout>
</RelativeLayout>
</FrameLayout>
</android.support.v4.widget.SwipeRefreshLayout>
I initiate it via butterknife:
#InjectView(R.id.swipeRefreshLayout) SwipeRefreshLayout mSwipeRefreshLayout;
This is my refreshListener:
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
// Refresh items
if(timeLineAdapter!= null)
timeLineAdapter.setLastAnimatedPosition(0);
refresh(false);
}
});
I call this on onResume, so that it will work if I come back to it.
Now initially it works, if I press home button and come back, it works.
If I change my activity to another one, and then come back to it, it doesn't work anymore. How can this be fixed?
EDIT: As you can see, the main child of the SwipeRefreshLayout is a FrameLayout which contains the RecyclerView, my RecyclerViewHeader and a placeholder for the list (in here, cause swiping down on the placeholder should also swipe to refresh.
I Added between this 2 views, a ScrollView, and now the swipe to refresh works. BUT, having the recyclerview inside the scrollview ruins my recyclerView. I can scroll down but not up. Any other ideeas?
I had timelineList.setVisibility(View.Invisible) inside the code when I would check to see if I have objects to show inside the list.
I removed that and set this instead:
if(timeLineAdapter != null) {
timeLineAdapter.clearItems();
timeLineAdapter.notifyDataSetChanged();
}
Now the list is not set as invisible nowhere.
I added my placeholder and list inside the SwipeRefreshLayout, thinking that it will work on both, but in reality it only works on the list, so if the list is gone, the pull to refresh is gone
I've got a problem with my view which looks like below:
My view before scrolling
My view after scrolling
As you can see it's supposed to be a scrollable view with unscrollable ListViews.
It consists of DrawerLayout inside which I've got CoordinatorLayout (with AppBarLayout and my FrameLayout containing my fragments) and NavigationView.
activity_main.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_layout"
android:background="#color/colorBackground"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_vision"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_vision"
app:menu="#menu/activity_vision_drawer" />
app_bar_vision.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"
android:fitsSystemWindows="true"
tools:context=".activity.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorBar"
app:popupTheme="#style/AppTheme.PopupOverlay"
app:layout_scrollFlags="scroll|enterAlways|snap"/>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"/>
Now the main part. My fragment layout consists of ScrollView inside which there is RelativeLayout inside wich I've got two ListViews wchich height is set programatically according to assigned elements.
fragment_preferences.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include layout="#layout/content_preferences" />
</ScrollView>
content_preferences.xml
<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"
android:layout_gravity="center_horizontal"
tools:context="dron.mkapiczynski.pl.dronvision.fragment.PreferencesFragment"
tools:showIn="#layout/fragment_preferences">>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Preferencje wizualizacji"
android:id="#+id/preferencesTitle"
android:layout_gravity="center" />
<TextView
android:id="#+id/trackedListTitle"
android:text="Drony śledzone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/preferencesTitle"
android:layout_marginTop="20dp"/>
<ListView
android:id="#+id/trackedDroneList"
android:layout_below="#+id/trackedListTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_gravity="center_horizontal"
android:layout_centerInParent="true"
tools:showIn="#layout/fragment_preferences"/>
<TextView
android:id="#+id/visualizedListTitle"
android:text="Drony do wizualizacji obszaru przeszukanego"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/trackedDroneList"
android:layout_marginTop="20dp"/>
<ListView
android:id="#+id/visualizedDroneList"
android:layout_below="#+id/visualizedListTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_gravity="center_horizontal"
android:layout_centerInParent="true"
android:nestedScrollingEnabled="true"
tools:showIn="#layout/fragment_preferences"/>
In general scrolling works but only if I touch the screen in areas outside of ListView for example in area with my TextView. When I try to scroll in areas with ListView nothing happens.
I'll be gratefull for any advice. I've tried to implement my own nonScrollingListView extending ListView thought that may enable parent scrolling but that doesn't help.
I've also read about using LinearLayout instead of ListView but maybe there is still a way to use ListView with ScrollView?
It is generally a discouraged to use a Scrollable view as a childview of the scrollview or any other scrollable content.
But, however if there is a need of the application and if the developer wants to handle the scrolling events smoothly, below is the way to handle the scrollable contents individually,
For the parent scrollview, you can handle the scrolling event as
m_parentScrollView.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View p_v, MotionEvent p_event)
{
m_childScrollView.getParent().requestDisallowInterceptTouchEvent(false);
// We will have to follow above for all scrollable contents
return false;
}
});
For the child scrollview,in your case Listview, we have to handle the event as shown below:
m_childScrollView.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View p_v, MotionEvent p_event)
{
// this will disallow the touch request for parent scroll on touch of child view
p_v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
You can get more details about it from here:
handle-scrollable-views-or-controls-in-another-scrollview-in-android
Also, as a side note, I have also faced some similar issues while using Scrollview and RelativeLayout and this user is also facing same issue as you.
Hope it helps you :)
I have the following layout
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white" >
<LinearLayout
android:id="#+id/answerMainFrame"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:isScrollContainer="true"
android:onClick="toQuestion" >
<ImageView
android:id="#+id/answer_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:contentDescription="#string/question_img_cd" />
<TextView
android:id="#+id/answer"
style="#style/Question" />
</LinearLayout>
Sometimes the ScrollView is too small and won't fill the whole screen but still I want to call the method toQuestion() clicking anywhere on the screen.
I've tried setting android:layout_height="wrap_content" to the LinearLayout and android:onClick="toQuestion" to the ScrollView but same result.
You could try to let the LinearLayout implement the onClick attribute and then set:
android:fillViewport="true"
to the ScrollView.
I had a similar issue, and simply got rid of the scrollview itself. Instead, I directly inserted the TextView itself, with the constraints previously put on the ScrollView. The whole purpose of my ScrollView was to scroll in the TextView, not among several items, so it seemed a more elegant way to do.
Add a (vertical) scrollbar to my textview in activity.hmtl :
<TextView
android:id="#+id/tvInfo"
(...)
android:scrollbars="vertical"
/>
Add this to the onCreate method:
oTV = (TextView) findViewById(R.id.tvInfo);
oTV.setMovementMethod(new ScrollingMovementMethod());
No need to write the scrolling response. I like that.
I found the solution here (thx Juned Mughal) :
http://www.android-examples.com/make-textview-scrollable-in-android-programmatically/
To make the scroll view fill the screen change:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white" >
to
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/white" >
and for the click put this in your onCreate method:
((ScrollView) findViewById(R.id.scrollView))
.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
toQuestion();
}
});