I have a RecyclerView where in each item i can add X child items, when a parent item is added my RecyclerView scrolls to bottom but the issue is that when lot of child items are added to the last element it's not scrolling to last child (obviously as i'm doing scrollToPosition of last RecyclerView item).
So how should i perform the scrolling in the way that it scrolls to even last child not only the parent item?
Here a gif of how it looks like.
XML
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
app:stackFromEnd="true"
android:paddingBottom="45dp"
tools:listitem="#layout/comanda_list" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fabInvia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:backgroundTint="#00C853"
app:layout_anchor="#+id/bottomSheet"
app:borderWidth="0dp"
app:layout_anchorGravity="top|center"
app:srcCompat="#drawable/ic_baseline_send"
android:contentDescription="#string/verifica_gp" />
<include layout="#layout/bottom_sheet_pterm" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Code where i add a child item in my Adapter
public void addChild(int position, Varianti variante) {
comandeList.get(position).setVariant(variante);
notifyItemChanged(position);
}
onBindViewHolder where the child items are rendered
#Override
public void onBindViewHolder(#NonNull final ExampleViewHolder holder, final int position) {
final Comanda item = comandeList.get(position);
...
List<Varianti> variants = item.getVarianti();
if (variants != null && variants.size() > 0) {
for (Varianti v : variants) {
View vView = mInflater.inflate(R.layout.varianti_layout, holder.variantsContainer, false);
TextView nameTV = vView.findViewById(R.id.variant_name);
if (v.getState().equals("K") || v.getState().equals("KK")) {
nameTV.setTypeface(null, Typeface.BOLD);
}
if (!item.getState().equals("S")) {
nameTV.setTextColor(Color.parseColor("#FFFFFF"));
}
nameTV.setText(v.getDescrizione());
nameTV.setBackground(v.getDrawable());
holder.variantsContainer.addView(vView);
}
}
...
}
In activity after adding new item / child item
adapterComanda.addVariante(position, variante);
recyclerComanda.scrollToPosition(adapterComanda.getItemCount() - 1);
BottomSheet
<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/bottomSheet"
android:background="#FFFFFF"
android:layout_width="match_parent"
android:layout_height="450dp"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
app:behavior_hideable="false"
android:elevation="5dp"
app:behavior_peekHeight="60dp"
android:orientation="vertical">
... (different LinearLayouts)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:gravity="bottom"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerViewTasti"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="50dp"
android:paddingTop="3dp"
android:paddingBottom="3dp"
tools:listitem="#layout/tasto_list"
app:spanCount="4"
android:scrollbars="vertical"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager" />
</LinearLayout>
</LinearLayout>
The issue with your code is it is Scrolling to the bottom, but the recycler view is going below the <include layout="#layout/bottom_sheet_pterm" />
Try setting a layout_anchor
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingBottom="45dp"
app:layout_anchor="#+id/fabInvia"
app:stackFromEnd="true" />
Or use a constraintlayout which constraints bottom to top of fabInvia.
Related
I have three recycler View inside the Nested Scroll View when the activity is started it doesn't load first recycler View. I have swipe Refresh Layout in app when I refresh all the recycler View are loaded properly. The second recycler view doesn't load at the start of the activity tried every solution.
Java File
shops.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot photosnapshot:dataSnapshot.getChildren())
{
Shops temp=photosnapshot.getValue(Shops.class);
Location templocation=new Location(LocationManager.GPS_PROVIDER);
Location templocation1=new Location(LocationManager.GPS_PROVIDER);
templocation.setLatitude(temp.getAddress().getLatitude());
templocation.setLongitude(temp.getAddress().getLongitude());
templocation1.setLatitude(common.currentLocation.getLatitude());
templocation1.setLongitude(common.currentLocation.getLongitude());
if(templocation.distanceTo(templocation1)<1500)
{
shopslist.add(new Shops(temp.getName(),temp.getImage(),temp.getStatus(),temp.getTimings(),temp.getAddress(),photosnapshot.getKey(),temp.getCategory()));
if(temp.getCategory().equals("02"))
{
dairyshoplist.add(new Shops(temp.getName(),temp.getImage(),temp.getStatus(),temp.getTimings(),temp.getAddress(),photosnapshot.getKey(),temp.getCategory()));
}
}
}
menuAdapter.notifyDataSetChanged();
recyclemenu= (RecyclerView) findViewById(R.id.recycler_menu);
recyclecategory=findViewById(R.id.recycler_menu_categories);
headerusername= (TextView) headerview.findViewById(R.id.textnameheader);
headerusername.setText(common.currentuser.getName());
recyclemenu.setHasFixedSize(true);
recyclecategory.setHasFixedSize(true);
layoutManager= new LinearLayoutManager(this);
layoutManager.setAutoMeasureEnabled(true);
recyclemenu.setLayoutManager(layoutManager);
recyclecategory.setLayoutManager(new GridLayoutManager(this,2));
recycle_dairy_shops.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true));
recycle_dairy_shops.setHasFixedSize(true);
recyclemenu.setNestedScrollingEnabled(false);
recycle_dairy_shops.setNestedScrollingEnabled(false);
recyclecategory.setNestedScrollingEnabled(false);
.xml File
<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/swipeLayout"
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"
tools:context="com.example.mahesh.door_step.Home"
tools:showIn="#layout/app_bar_home"
>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/recycler_menu_categories"
android:fillViewport="true"
android:layout_below="#+id/search"
android:layout_marginTop="10dp"
android:scrollbars="vertical"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/recycler_menu"
android:fillViewport="true"
android:layout_marginTop="10dp"
android:layout_below="#+id/recycler_menu_categories"
android:scrollbars="vertical"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/recycler_menu_dairyshops"
android:fillViewport="true"
android:layout_marginTop="10dp"
android:layout_below="#+id/nearbyshopid"
android:scrollbars="vertical"/>
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
I want to create a navigational bottom sheet like that in Google Maps Navigational BottomSheet where we have the list of directions for the route. I am using android's BottomSheetBehaviour to open up the bottomsheet. The issue i am currently facing is that the listview doesn't show up inside the bottom sheet layout when it pops up. The view is simply blank. I also tried to inflate view myself inside a NestedScrollView to get the same result, but that too didn't show up.
This is my bottomsheet 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"
android:id="#+id/bottomSheet1"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:elevation="4dp"
android:background="#color/white"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
android:clipToPadding="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="This is a sheet with listview."
android:textSize="20dp"
android:layout_marginTop="10dp"
android:textColor="#color/primaryText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="This is a secondary text!"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.v4.widget.NestedScrollView
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/nestedLinearLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
<ListView
android:id="#+id/bottomSheetListview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
And in my java code,
View bottomSheetView = findViewById(R.id.bottomSheet1);
ListView listView = (ListView) findViewById(R.id.bottomSheetListview);
List<String> listData = getListData(); //returns a simple array list of strings, about 15 items
listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listData));
mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheetView);
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
Am I doing anything wrong here? Why doesn't the litview or scrollview show up.
Edited
My Whole Activity
public class BottomSheetActivity extends AppCompatActivity {
private static final String TAG = "==> BottomSheetActivity";
BottomSheetBehavior mBottomSheetBehavior;
Button peek;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bottom_sheet);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
this.peek = (Button) findViewById(R.id.peek);
peek.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showSheet1();
}
});
}
private List<String> getListData(){
List<String> stringList = new ArrayList<>();
for (int i = 0; i < 15; i++) {
stringList.add("This is string number "+i);
}
return stringList;
}
private void showSheet1(){
if(mBottomSheetBehavior != null){
//hide any previous bottom sheets
mBottomSheetBehavior.setHideable(true);
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
}
//initialize a new sheet
View bottomSheetView = findViewById(R.id.bottomSheet1);
ListView listView = (ListView) bottomSheetView.findViewById(R.id.bottomSheetListview);
listView.setVisibility(View.VISIBLE);
List<String> listData = getListData();
listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listData));
mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheetView);
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
}
activity_bottom_sheet.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.vedamic.androidtutorial.BottomSheetActivity">
<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="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_bottom_sheet" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_dialog_email" />
<include layout="#layout/layout_bottom_sheet_1" />
</android.support.design.widget.CoordinatorLayout>
content_bottom_shee.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"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.vedamic.androidtutorial.BottomSheetActivity"
tools:showIn="#layout/activity_bottom_sheet">
<Button
android:id="#+id/peek"
android:text="peek 1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/expand"
android:text="peek 2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/flipboardSheet"
android:text="Flipbard BottomSheets"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:id="#+id/testListView"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
I imported your xml and the android studios "Preview" indeed showed that the NestedScrollView with android:layout_height="match_parent" takes the whole space so the ListView hasn't got space left.
When setting the NestedScrollView to android:visibility="gone" the ListView has enough space.
So the only reason now that nothing is shown is maybe because your listData is empty?
EDIT
OK I implemented all info and the style is very messy. First of all the peek 1 button is behind the toolbar.
But I can still click it, so this happens:
Dont make your listview visibility gone at first. Bottom should know what size it has to be. If you need to make its visibility gone, done it after the bottom sheet created.
And try in your bottom sheet content_bottom_shee.xml parent linearlayout
android:fillViewport="true"
I have a problem which is driving me nuts. Basically it is a simple RecyclerView which displays a cardview. There are a lot of posts out there already, which I checked. I have assigned a LayoutManager as indicated here (RecyclerView Not Displaying Any CardView Items) , I have implemented the getItemCount method as explained here (card view not showing up), and I changed to the following versions (changing version was suggested here RecyclerView of cards not showing anything):
compile 'com.android.support:support-v4:24.0.0'
compile 'com.android.support:cardview-v7:24.0.0'
compile 'com.android.support:recyclerview-v7:24.0.0'
I also did the notifyDataSetChanged() as indicated here (RecyclerView Not Displaying Any CardView Items). However, nothing fixed it for me.
I have an Activity which has the following code (only relevant sections, getDummyData returns a List with 1 dummy data):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DatabaseHelper databaseHelper = new DatabaseHelper(getApplicationContext());
setContentView(R.layout.activity_overview);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
RecyclerView rv = (RecyclerView) findViewById(R.id.rv);
rv.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
rv.setLayoutManager(linearLayoutManager);
RVAdapter adapter = new RVAdapter(getDummyData());
rv.setAdapter(adapter);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
adapter.notifyDataSetChanged();
}
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.DocumentViewHolder> {
List<Document> DocumentList;
public RVAdapter(List<Document> Documents) {
this.DocumentList = Documents;
}
#Override
public DocumentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.document_item, parent, false);
DocumentViewHolder pvh = new DocumentViewHolder(v);
return pvh;
}
#Override
public void onBindViewHolder(DocumentViewHolder DocumentViewHolder, int i) {
DocumentViewHolder.Date.setText(DocumentList.get(i).getCreatedFormatted());
DocumentViewHolder.participants.setText(DocumentList.get(i).getParticipants(getApplicationContext()));
DocumentViewHolder.counterOfItems.setText(DocumentList.get(i).getcounterOfItems(getApplicationContext()) + StringUtils.EMPTY);
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
#Override
public int getItemCount() {
//return DocumentList.size();
return 1;
}
public class DocumentViewHolder extends RecyclerView.ViewHolder {
TextView Date;
TextView participants;
TextView ideasPreview;
TextView counterOfItems;
ImageView Photo;
public DocumentViewHolder(View itemView) {
super(itemView);
Date = (TextView) itemView.findViewById(R.id._date);
participants = (TextView) itemView.findViewById(R.id.participants);
ideasPreview = (TextView) itemView.findViewById(R.id.ideas_preview);
counterOfItems = (TextView) itemView.findViewById(R.id.counter_of_items);
Photo = (ImageView) itemView.findViewById(R.id._photo);
}
}
}
And I have a Document_item - xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
card_view:cardCornerRadius="#dimen/_card_corner">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/_photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingEnd="#dimen/activity_horizontal_margin"
android:paddingStart="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<TextView
android:id="#+id/_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="21st June 2016" />
<TextView
android:id="#+id/participants"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Thomas, Christian and Johann" />
<TextView
android:id="#+id/counter_of_items"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Thomas, Christian and Johann" />
<TextView
android:id="#+id/ideas_preview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello, Test, Test, Test" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
The activity's xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.chmaurer.idea.OverviewActivity">
<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="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#drawable/ic_playlist_add_white_24dp" />
</android.support.design.widget.CoordinatorLayout>
The code is compiling and executing normally, but no cards are shown. I have tried a few hour for myself now, but it is definitely the point where I'd be glad to have a hint...
Your code is fine, The problem is with layout file: document_item.xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/_photo"
android:layout_width="wrap_content" -->mistake
android:layout_height="wrap_content"
android:layout_weight="0.25" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" -->mistake
android:gravity="center_horizontal"
android:orientation="vertical"
...
The second LinearLayout gets its height from parent (first LinearLayout), and the first one gets its height from its only child: ImageView (so without no drawable set, the second layout's height will be zero! ), if you set a drawable for ImageView (in onBindViewHolder method or in layout xml file) it probably crops some of TextView items, Also when you set android:layout_width to "wrap_content", actually you're ignoring layout_weight.
So edit layout file like this:
...
<ImageView
android:id="#+id/_photo"
android:layout_width="0dp"
android:layout_gravity="center_vertical"
android:layout_height="wrap_content"
android:layout_weight="0.25" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
...
You are using RecyclerView with vertical orientation (in Java code)
In the Activity XML you are setting the layout height to Wrap_Content
The card layout also has an height of match_parent
They will not work like that, I lost three days on this
Try to give fix height to your card layout, test also other alteration of other layout, it will work for you
Good luck
Just try after adding this line inside your recyclerview:
app:layout_behavior="#string/appbar_scrolling_view_behavior"
like this:
<android.support.v7.widget.RecyclerView
android:id="#+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
</android.support.v7.widget.RecyclerView>
Hope it will help you.
I am having Recyclerview inside Scrollview
<Scrollview
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:id="#+id/layoutStaticContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
//Static content.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp">
.
.
<LinearLayout>
//Dynamic content(newsfeed)
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</ScrollView>
Now while scrolling, layoutStaticContent stays fix on the top & recyclerview content scrolls independently in the bottom part.
How to scroll the whole content i.e (layoutStaticContent + recyclerview content) such that there is only 1 scrollview?
I also tried replacing scrollview with Nestedscrollview but no success.
Use the android.support.v4.widget.NestedScrollView then inside both layout
NestedScrollView
Put the LinearLayout which contains both the static and dynamic data inside of a NestedScrollView and it'll work like a charm.
Here is the code you need:
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/layoutStaticContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
//Static content.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp">
.
.
<LinearLayout>
//Dynamic content(newsfeed)
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
I hope it helps!
One possible way around this is only use RecyclerView with the static content as header to your Recyclerview.
Then the layout would simply be:
//Dynamic content(newsfeed)
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
There will be a list_item_header.xml layout for your static content:
//Static content.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp">
.
.
<LinearLayout>
And you'll have to change your recyclerview adapter to contain:
private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;
#Override
public int getItemCount()
{
int itemCount = super.getItemCount();
if (mIsHeaderPresent)
{
itemCount += 1;
}
return itemCount;
}
#Override
public int getItemViewType(int position)
{
if (mIsHeaderPresent && position == 0)
{
return TYPE_HEADER;
}
return TYPE_ITEM;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
if (viewType == TYPE_HEADER)
{
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_header, parent, false);
ViewHolderHeader viewHolder = new ViewHolderHeader(itemView);
return viewHolder;
} else if (viewType == TYPE_ITEM)
{
return getItemViewHolder(parent);
}
throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder passedViewHolder)
{
if (mIsHeaderPresent && passedViewHolder instanceof ViewHolderHeader)
{
onBindHeaderViewHolder((ViewHolderHeader) passedViewHolder);
} else
{
onBindItemViewHolder(passedViewHolder);
}
}
if you want to scroll all data i think you have to use CoordinatorLayout. in CoordinatorLayout you use appbar layout and CollapsingToolbarLayout where you can put your static content. because its a wrong approach in android to use a scroll able container in to another scroll able container. you can use coordinater layout like this.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
>
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentScrim="?attr/colorPrimary"
android:fitsSystemWindows="true"
app:expandedTitleMarginStart="48dp"
app:expandedTitleMarginEnd="64dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
//Static content.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp">
.
.
<LinearLayout>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
/>
</android.support.design.widget.CoordinatorLayout>
After a lot of searching and trying, I found the solution:
1. Set the height for the RecyclerView inside the ScrollView:
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
...
<android.support.v7.widget.RecyclerView
android:id="#+id/myPostsRecyclerView"
android:layout_width="match_parent"
**android:layout_height="550dp"**
android:layout_below="#+id/textView7"
android:background="#drawable/background"
android:padding="5dp"
**android:visibility="gone"**
tools:listitem="#layout/item_send" />
</RelativeLayout>
</ScrollView>
2. In the adapter , where you set the data list :
public void setData(List<Post> posts) {
this.posts.clear();
this.posts.addAll(posts);
notifyDataSetChanged();
if (posts.size() < 1)
activity.recyclerView.setVisibility(View.GONE);
else {
activity.recyclerView.setVisibility(View.VISIBLE);
ViewGroup.LayoutParams params=activity.recyclerView.getLayoutParams();
params.height=ViewGroup.LayoutParams.WRAP_CONTENT;
activity.recyclerView.setLayoutParams(params);
}
}
I have RecyclerView inside ScrollView which is inside SwipeRefresh layout. I have few layouts on top of RecyclerView, but my RecyclerView is not visible, even if i remove other layouts from ScrollView. What do you suggest? I need to put list below some layouts in ScrollView somehow.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<include layout="#layout/toolbar"></include>
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
....
<android.support.v7.widget.RecyclerView
android:id="#+id/simpleList"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
Tried to give specific height but still not visible o.O
If i replace RecyclerView with ListView, then list is visible.
If you want to have all the items of the list always visible then replace RecyclerView with LinearLayout and add your items to it.
An example:
your_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<include layout="#layout/toolbar"></include>
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
....
<LinearLayout
android:id="#+id/simpleList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"/>
</ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/item_text"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/abc_ic_ab_back_mtrl_am_alpha"/>
</LinearLayout>
And in your Java code:
first declare the list
mSimpleList = (LinearLayout) findViewById(R.id.simpleList);
then method for adding the items
public void addListItems(ArrayList<String> strings) {
LayoutInflater inflater = LayoutInflater.from(this);
for(String s : strings) {
View item = inflater.inflate(R.layout.item_layout, mSimpleList, false);
TextView text = (TextView) item.findViewById(R.id.item_text);
text.setText(s);
mSimpleList.addView(item);//you can add layout params if you want
}
}
I just had to put android:fillViewport="true" to SwipeRefreshLayout. And now RecyclerView is visible.
My earlier answer is blocking the down scroll. So, a good work around will be add your Stuff as the first element of the RecyclerView. That can be easily implemented using the adapter. That way you can still scroll your stuff as well as your actual list elements.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="#layout/toolbar"></include>
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/simpleList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#+id/your_stuff" />
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
And using something like the following for your adapter implimentation,
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
class YourStuffViewHolder extends RecyclerView.ViewHolder {
//
}
class ViewHolderListItem extends RecyclerView.ViewHolder {
//
}
#Override
public int getItemViewType(int position) {
if(position>0){
return 1;
}
return 0;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case 0: return new YourStuffViewHolder();
case 1: return new ViewHolderListItem ();
}
}
}