I have to hide Particular views in recyclerview, I am trying to hide some views but view has gone but space is there,I have tried putting heights=0 in cardview but still did not work , what I should I do?
This is my XML code list data
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-
auto">
<android.support.v7.widget.CardView
android:id="#+id/child_card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2sp"
app:cardElevation="12sp"
android:visibility="gone">
<LinearLayout
android:id="#+id/main_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="?attr/selectableItemBackground"
android:gravity="center"
android:orientation="vertical"
android:padding="3dp"
android:visibility="visible">
<ImageView
android:id="#+id/icon"
android:layout_width="48dp"
android:layout_height="48dp"
app:srcCompat="#drawable/bsp_icon" />
<android.support.v7.widget.AppCompatTextView
android:id="#+id/label_value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:lines="2"
android:maxLines="2"
android:padding="0dp"
android:textColor="#color/Black" />
</LinearLayout>
</android.support.v7.widget.CardView>
</layout>
and this is my recyclerview
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="0dp"
tools:listitem="#layout/list_layout_dashboard_child_view">
</android.support.v7.widget.RecyclerView>
this is my Adapter code
if (txnTypeMap.get("CUST_CR") != null &&
obj.getChildName().equalsIgnoreCase(txnTypeMap.get("CUST_CR"))) {
binding.childCardView.setVisibility(View.VISIBLE);
}
important always set this if you are sure your data will change in recyclerview mRecyclerView.setHasFixedSize(false);
first remove the item from list e.g. list.remove(position which you want to hide)
next call notifyItemChanged(postion) and it should work ->this is better approach than calling notifyDataSetChanged()
You should hide all views or parent from UsersViewholder layout xml. You should hide entire viewholder or each view
Entire viewholder:
itemView.setVisibility(View.GONE);
or each element:
view.setVisibility(View.GONE);
But don't forget to set them VISIBLE otherwise, you will end up with some strange things from recycling
Without setting setVisibility to View.GONE, try to remove the the data from the data source and call notifyDataSetChanged()
This is the more efficient way to hide a view from RecyclerView
you have to set visibility gone to view in else case because views are reused in recycler view
if (txnTypeMap.get("CUST_CR") != null &&
obj.getChildName().equalsIgnoreCase(txnTypeMap.get("CUST_CR"))) {
binding.childCardView.setVisibility(View.VISIBLE);
} else binding.childCardView.setVisibility(View.GONE);
I hope it will work.
Related
<RelativeLayout
android:id="#+id/reviews_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/border"
android:clickable="true">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/reviews"
android:paddingTop="20dp"
android:layout_width="match_parent"
android:layout_height="400dp"/>
</RelativeLayout>
I'm using above code in xml.
binding.apply {
reviewsRoot.setOnClickListener {
NavigateWithItemName(itemname)
Log.i("click","click") //doesn't work
}
}
I'm using above code in onCreateView in Fragment.
I want to setOnClickListener on RelativeLayout, but it doesn't work when I click RelativeLayout..
I don't know why.
The RecyclerView lying over the RelativeLayout is consuming the clicks that are intended to be handled by the layout. To prevent that You would have to set:
android:clickable="false"
on the RecyclerView. Now the layout will consume the clicks.
I want to add footer with empty layout to the bottom of the recyclerView which will show up only if certain View is visible on screen(In my case FabButton). FabButton is overlapping my last RecyclerView item, so I want to add empty Footer to the bottom of the recyclerview if this FabButton is visible.
ViewHolder inside Adapter for Footer:
inner class FooterViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
Inside onBindViewHolder:
... ViewHolder above is for items.
else if(holder is FooterViewHolder){
if (parentFragment.isFabButtonVisible()){
holder.itemView.visibility = View.VISIBLE
} else {
holder.itemView.visibility = View.GONE
}
}
This part of the code is working(also else branch is executed correctly), but my footer view is permanently visible on screen. Even if i add only View.GONE it is still visible.
XML for Parent Activity:
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#color/splash_white"
android:layout_weight="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
>
<com.app.TabLayoutEntity.CustomViewPager
android:id="#+id/fragmentViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="0dp"
android:background="#color/splash_white"
tools:layout_editor_absoluteY="-2dp" />
<ImageButton
android:id="#+id/fabButton"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:background="#drawable/fab_gradient"
android:clickable="true"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:padding="8dp"
android:elevation="8dp"
android:shape="oval"
android:src="#drawable/ico_fab" />
</android.support.design.widget.CoordinatorLayout>
Fragment XML(Inside ViewPager):
<?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:clipToPadding="true"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/itemRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/recyclerview_corners"
android:clipToPadding="false"
android:scrollbars="vertical"
android:paddingBottom="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
If You just wanna avoid overlapping of last item and Fab then you should do this below simple change in xml on recyclerview and parent layout
<android.support.v7.widget.RecyclerView
android:clipToPadding="false"
android:paddingBottom="paddingindp"/>// for example You can add 100dp and test
and on parent layout add this below line
android:clipToPadding="true"
this will make your recyclerview scroll over that Fab and its doesnt require to add Footer view
Hope this work for your requirement.
You can add your condition in getItemCount() method since you need to increase your list size if you add footer
Like,
override fun getItemCount(): Int {
if (parentFragment.isFabButtonVisible()){
return mModels.size + 1
} else {
return mModels.size
}
}
I'm trying to implement an empty view solution for a RecyclerView via data binding based on this solution. I have a RecyclerView and RelativeLayout (the empty view) within a SwipeRefreshLayout. Here is the XML for the layout:
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<import type="android.view.View"/>
<variable
name="dataset"
type="...SeriesList"/>
</data>
<android.support.v4.widget.SwipeRefreshLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/list"
android:name="...SeriesFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="10dp"
app:layoutManager="LinearLayoutManager"
tools:context=".fragments.SeriesFragment"
android:visibility="#{dataset.size() > 0 ? View.VISIBLE : View.GONE}"/>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/empty_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="#{dataset.size() > 0 ? View.GONE : View.VISIBLE}">
<TextView
android:id="#+id/empty_text"
style="#style/Base.TextAppearance.AppCompat.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/empty_image"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:width="300dp"
android:gravity="center"
android:textColor="#59000000" />
<ImageView
android:id="#+id/empty_image"
android:layout_width="128dp"
android:layout_height="160dp"
android:layout_centerInParent="true"
android:src="#drawable/empty" />
</RelativeLayout>
</android.support.v4.widget.SwipeRefreshLayout>
</layout>
As you can see the RelativeLayout after the RecyclerView is the empty view, with their visbilities based on the size of the dataset, which is a SeriesList (extends ObservableArrayList). In the fragment containing this layout I bind the data like so:
FragmentSeriesListBinding binding = FragmentSeriesListBinding.inflate(inflater);
binding.setDataset(getmAdapter().getVisibleSeries());
So based off of this, I'm assuming when the adapter's visible SeriesList is empty, the visibilities of the RecyclerView will be GONE and the empty view will be VISIBLE. Upon testing with an empty dataset both the RecyclerView (with no items) and the empty view have a visibility of VISIBLE, which shouldn't be possible. Could someone shed some light as to why this isn't working as I expect?
So I ended up fixing this after reading here that SwipeRefreshLayout must have one child, so I wrapped the RecyclerView and RelativeLayout in a FrameLayout. I also used this solution to inflate the Fragment's layout with the DataBindingUtil inflate() method and setting the binding in the Fragment's onCreateView() method.
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 have a fragment which includes a Textview, and a RecyclerView (list)
My problem is when scrolling the RecyclerView, the textview above it stays on top and doesn't get scrolled.
How do I make the textview to be non-sticky and scrollable like in a ScrollView?
My layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.easyuni.courserecommender.ResultsCareerTabFragment">
<TextView
android:id="#+id/tv_results_rec_careers_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:layout_marginTop="24dp"
android:text="#string/results_rec_careers_title"
android:textSize="24sp"
android:textStyle="bold" />
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_careers"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_results_rec_careers_title"
android:layout_gravity="center"
android:layout_marginTop="20dp"></android.support.v7.widget.RecyclerView>
</RelativeLayout>
Screenshots:
Normal look:
When scrolling down in RecyclerView:
Thanks.
You will need to add your textview as an item that feeds your adapter for your recyclerview. You can use the following code to distinguish between the view that you want to display in your recyclerview:
public int getItemViewType (int position)
Please have a look at this thread:
How to create RecyclerView with multiple view type?
You can fix it with this:
Message message = messageList.get(position);
viewHolder.setIsRecyclable(false);
you can lean on this link:
http://www.homebrewandtechnology.com/blog/recyclevewadaptershowwrongdatawhenscrolling