Android RecyclerView in ScrollView in SwipeRefresh is not visible - android

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 ();
}
}
}

Related

Unable to scroll RecyclerView with Child items

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.

Pull the whole layout down on swipeRefreshLayoutListener android

I want to move the whole layout down when SwipeRefreshLayout Listener is called? Is this thing possile in android .?
This is my whole xml layout.SwipeRefreshLayout contains a progressbar and FrameLayout and that is included in a RelativeLayout.So I want the FrameLyout should pull down when I call for refresh.
<?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:custom="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.support.v4.widget.DrawerLayout
android:id="#+id/drawer_navigation_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar_layout" />
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipeRefresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/toolbar">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ProgressBar
android:id="#+id/horizontal_sync_progress"
style="#style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/gradient_bg_end"
android:indeterminateDrawable="#drawable/progress_drawable"
android:progressDrawable="#drawable/progress_drawable"
android:visibility="gone"/>
<FrameLayout
android:id="#+id/container_frag"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
<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.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swp1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<-----Your design here------>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
try this. you have to set your SwipeRefreshLayout above. Hope it helps you. If this doesn't helps you ask me what is the problem i will help you
And My class code is below
mSwipeRefreshLayout1 = (SwipeRefreshLayout) findViewById(R.id.swp1);
//set colors if you need
mSwipeRefreshLayout1.setColorSchemeResources(R.color.colorPrimary, R.color.colorPrimaryDark, R.color.colorPrimaryLight);
mSwipeRefreshLayout1.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
mSwipeRefreshLayout1.setRefreshing(true);
try {
//Refresh data
} catch (Exception e) {
//Toast.makeText(getActivity(), String.valueOf(e.getMessage()), Toast.LENGTH_SHORT).show();
//customtoast.ShowToast(getActivity(), String.valueOf(e.getMessage()), R.layout.red_toast);
}
}
});
you can code like this
swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
new class(getActivity(), listView, progressBar, mApp.getPreference().getString(Common.u_id, ""), "all", swipe).execute();
}
});
return view;
}
Include code below in your xml file
<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">
//your xml codes
</RelativeLayout>
</android.support.v4.widget.SwipeRefreshLayout>

RecyclerView not displaying last item but is calling onBindView

I am trying a list of items using RecyclerView , everything works fine but the last item is not being displayed! The length of the list is 2 and onBindView is called twice and the when I used Log to print the items it aslo prints the last item. So the list is fine but the RecyclerView is not displaying it.
This is my code RecyclerView Adapter:
public class ModuleList extends RecyclerView.Adapter<ModuleList.ViewHolder> {
private List<Module> moduleList;
public ModuleList(List<Module> moduleList){
this.moduleList = moduleList;
}
public ModuleList(){
this.moduleList = new ArrayList<>();
}
public List<Module> getModuleList() {
return moduleList;
}
public void setModuleList(List<Module> moduleList) {
this.moduleList = moduleList;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_modules,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Module module = moduleList.get(position);
// function is called twice and the all items are logged works fine
Log.d("ALL",module.getModuleName());
Log.d("ALL", String.valueOf(module.getNumberOfLevels()));
holder.moduleName.setText(String.valueOf(module.getModuleName()));
holder.numberOfLevels.setText(String.valueOf(module.getNumberOfLevels()));
}
#Override
public int getItemCount() {
//size is 2
return moduleList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView moduleName;
public TextView numberOfLevels;
public ViewHolder(View itemView) {
super(itemView);
moduleName = (TextView) itemView.findViewById(R.id.list_module_name);
numberOfLevels = (TextView) itemView.findViewById(R.id.list_no_of_levels);
}
}
}
This is my xml where recyclerview is:
<?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">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/all_modules_list"
android:scrollbars="vertical"
/>
</LinearLayout>
This is my xml where RecyclerView Fragment is displayed:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_screen"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_screen_container"
android:orientation="vertical"
>
<android.support.v7.widget.Toolbar
android:id="#+id/main_screen_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragment_container">
</FrameLayout>
</LinearLayout>
<ListView
android:layout_width="240dp"
android:layout_height="match_parent"
android:id="#+id/navigation_items"
android:layout_gravity="start"
android:background="#android:color/white"
>
</ListView>
</android.support.v4.widget.DrawerLayout>
Edit:list_module.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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/list_module_name"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/list_no_of_levels"
/>
</LinearLayout>
Hey can you try the solution below?
Change this in the list_module.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="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/list_module_name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/list_no_of_levels"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
I have replaced match_parent to wrap_content to parent layout.

Android listview doesn't get displayed in bottomsheet

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"

Recyclerview inside scrollview- How to scroll whole content?

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);
}
}

Categories

Resources