Scroll two recyclerView as one - android

I have two Recyclerview inside NestedScrollView. The second Recyclerview also have SwipeRefreshLayout. I try to set one scroll for both recycler, but my solution didn't work. Two recycler scroll separately and I don't have solution, please help my. Thanks in advance. My code
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/noFriendsTitle"
android:layout_marginTop="#dimen/marginTop"
android:layout_centerHorizontal="true"
android:visibility="gone"
android:text="#string/no_friends_title"/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progressBarGetFriends"
android:layout_centerInParent="true"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/mainFriendsRelative"
android:paddingTop="#dimen/marginTopSmall"
android:paddingBottom="#dimen/marginTopSmall"
android:visibility="invisible">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/paddingStart"
android:id="#+id/lastConversationTitle"
android:text="#string/friends_important_title"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/lastChatFriendsRecyclerView"
android:layout_below="#+id/lastConversationTitle"
android:layout_marginTop="#dimen/marginTopSmall"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/paddingStart"
android:layout_below="#+id/lastChatFriendsRecyclerView"
android:layout_marginTop="#dimen/marginTopSmall"
android:id="#+id/allFriendsTitle"
android:text="#string/friends_all"/>
<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/friendSwipeToRefresh"
android:layout_below="#+id/allFriendsTitle"
android:layout_marginTop="#dimen/marginTopSmall">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/friendsRecyclerView"
android:scrollbars="vertical"/>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
</RelativeLayout>
My fragment code
private void setLastConversationFriendsRecycler(RecyclerView lastConversationRecyclerView, List<Edge<Friends>> friends){
lastConversationList.addAll(friends);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
lastConversationRecyclerView.setHasFixedSize(true);
lastConversationRecyclerView.setLayoutManager(layoutManager);
lastConversationAdapter = new FriendsAdapter(getActivity(), lastConversationList, lastConversationRecyclerView);
lastConversationRecyclerView.setAdapter(lastConversationAdapter);
}
private void setFriendsRecycler(RecyclerView friendsRecyclerView, List<Edge<Friends>> friends){
friendsList.addAll(friends);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
friendsRecyclerView.setHasFixedSize(true);
friendsRecyclerView.setLayoutManager(layoutManager);
friendsAdapter = new FriendsAdapter(getActivity(), friendsList, friendsRecyclerView);
friendsAdapter.setOnLoadMoreListener(friendsListener);
friendsRecyclerView.setAdapter(friendsAdapter);
}
I also try different combination with setNestedScrollingEnabled, but result was the same.

I have already found solution and what is wrongs with my code. It's work code, maybe someone have the same issue.
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/friendSwipeToRefresh">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/noFriendsTitle"
android:layout_marginTop="#dimen/marginTop"
android:layout_centerHorizontal="true"
android:visibility="gone"
android:text="#string/no_friends_title"/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progressBarGetFriends"
android:layout_centerInParent="true"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/mainFriendsRelative"
android:paddingTop="#dimen/marginTopSmall"
android:paddingBottom="#dimen/marginTopSmall"
android:visibility="invisible">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/paddingStart"
android:id="#+id/lastConversationTitle"
android:text="#string/friends_important_title"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/lastChatFriendsRecyclerView"
android:layout_below="#+id/lastConversationTitle"
android:layout_marginTop="#dimen/marginTopSmall"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/paddingStart"
android:layout_below="#+id/lastChatFriendsRecyclerView"
android:layout_marginTop="#dimen/marginTopSmall"
android:id="#+id/allFriendsTitle"
android:text="#string/friends_all"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/friendsRecyclerView"
android:layout_marginTop="#dimen/marginTopSmall"
android:layout_below="#+id/allFriendsTitle" />
</RelativeLayout>
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
Fragment
private void setLastConversationFriendsRecycler(RecyclerView lastConversationRecyclerView, List<Edge<Friends>> friends){
lastConversationList.addAll(friends);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
lastConversationRecyclerView.setHasFixedSize(true);
lastConversationRecyclerView.setNestedScrollingEnabled(false);
lastConversationRecyclerView.setLayoutManager(layoutManager);
lastConversationAdapter = new FriendsAdapter(getActivity(), lastConversationList, lastConversationRecyclerView);
lastConversationRecyclerView.setAdapter(lastConversationAdapter);
}
private void setFriendsRecycler(RecyclerView friendsRecyclerView, List<Edge<Friends>> friends){
friendsList.addAll(friends);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
friendsRecyclerView.setHasFixedSize(true);
friendsRecyclerView.setNestedScrollingEnabled(false);
friendsRecyclerView.setLayoutManager(layoutManager);
friendsAdapter = new FriendsAdapter(getActivity(), friendsList, friendsRecyclerView);
friendsAdapter.setOnLoadMoreListener(friendsListener);
friendsRecyclerView.setAdapter(friendsAdapter);
}

This is my working solution:
studentsRecyclerView = (RecyclerView) view.findViewById(R.id.rv_students);
studentsRecyclerView.setHasFixedSize(true);
studentsRecyclerView.setNestedScrollingEnabled(false); // this line prevents nested scrolling, you can set it to true / false depending on you want nested scroll or not
studentsRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

Related

cardview extra space in recycleview android app

I want my cards be one to another,but they spawn with extra space,i don't know how to fix this.
I thought that my problem with recycleView.
//My RVadapter
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder>{
List<Bus> abstractlistbus;
public static class PersonViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView bus_name;
TextView controller_Status;
Context mContext;
PersonViewHolder(final View itemView, final Context context) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.cv);
mContext=context;
bus_name = (TextView)itemView.findViewById(R.id.name);
controller_Status = (TextView)itemView.findViewById(R.id.Status);
}
}
RVAdapter(List<Bus> abstractlist){
this.abstractlistbus = abstractlist;
}
#Override
public int getItemCount() {
return abstractlistbus.size();
}
#Override
public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cards_layout, viewGroup, false);
PersonViewHolder pvh = new PersonViewHolder(v,v.getContext());
return pvh;
}
#Override
public void onBindViewHolder(/*final*/ PersonViewHolder personViewHolder, final int i) {
personViewHolder.bus_name.setText("Номер автобуса:"+abstractlistbus.get(i).number);
personViewHolder.controller_Status.setText(abstractlistbus.get(i).controller+"");
}
}
//My activity_main.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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
/>
</RelativeLayout>
//My cards_layout.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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:tag="cards main container">
<android.support.v7.widget.CardView
android:id="#+id/cv"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="10dp"
card_view:cardElevation="5dp"
card_view:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_weight="2"
android:orientation="vertical"
>
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="Android Name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/Status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="Android Version"
android:textAppearance="?android:attr/textAppearanceMedium"/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
//And piece of code im MainActivity.java
RecyclerView rv = (RecyclerView) findViewById(R.id.my_recycler_view);
rv.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(this);
rv.setLayoutManager(llm);
extra space between cards
enter image description here
enter image description here
enter image description here
in your card linear layout make **android:layout_height="match_parent"** to **android:layout_height="wrap_content"**
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:tag="cards main container">
In your cards_layout.xml - add these margins to your linear layout.
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
This will solve your problem.
in you adapter view change CardView's height match_parent to wrap_content
Try it with this tag in your CardView:
app:cardUseCompatPadding="true"

1 of the RecyclerView inside NestedScrollView doesn't load at the start of the activity

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>

onChildViewDetachedFromWindow not working when recyclerview inside nestedscrollview

I am using LinearLayoutManager with RecyclerView which is inside NestedScrollView. Everything is working fine but addOnChildAttachStateChangeListener not wroking
My xml file of fragmnet is
=====================================================:
<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/parent_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#dedcdc"
android:orientation="vertical">
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.NestedScrollView
android:id="#+id/nested_scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/layout_rel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#color/app_bg"
android:paddingBottom="#dimen/margin_16dp"
android:paddingLeft="#dimen/margin_10dp"
android:paddingRight="#dimen/margin_10dp"
android:paddingTop="#dimen/margin_16dp">
<ImageView
android:id="#+id/image_user"
android:layout_width="#dimen/post_profile_dp_size"
android:layout_height="#dimen/post_profile_dp_size"
android:layout_centerVertical="true"
android:adjustViewBounds="true"
android:scaleType="fitXY"
tools:src="#drawable/default_user" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_margin="#dimen/margin_5dp"
android:layout_toLeftOf="#+id/button_add_post"
android:layout_toRightOf="#+id/image_user"
android:gravity="center_vertical"
android:text="Say something" />
</RelativeLayout>
<ProgressBar
android:id="#+id/progress_bar_horizontal"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="10dip"
android:scaleY="2"
android:layout_below="#+id/layout_rel"
android:layout_centerVertical="true"
android:indeterminate="true" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_below="#+id/progress_bar_horizontal"/>
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
and code in myfragmnet is
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mNestedScrollView = (NestedScrollView) view.findViewById(R.id.nested_scrollview);
mNestedScrollView.setNestedScrollingEnabled(false);
mHorizontalProgressBar = (ProgressBar) view.findViewById(R.id.progress_bar_horizontal);
mHorizontalProgressBar.setVisibility(View.GONE);
mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh_layout);
mSwipeRefreshLayout.setRefreshing(false);
mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
mRecyclerView.setNestedScrollingEnabled(false);
mLinearLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
mRecyclerView.setLayoutManager(mLinearLayoutManager);
mAdapter = new DataListAdapter(this, getContext(), arrayList);
mRecyclerView.setAdapter(mAdapter);
endlessNestedScrollListener = new EndlessNestedScrollListener(mLinearLayoutManager) {
#Override
public void onLoadMore(int page, int totalItemsCount) {
LoadMoreData();
}
};
mNestedScrollView.setOnScrollChangeListener(endlessNestedScrollListener);
mRecyclerView.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
#Override
public void onChildViewAttachedToWindow(View view) {
}
#Override
public void onChildViewDetachedFromWindow(View view) {
Log.d("tag","on detach");
}
});
}
Method onChildViewDetachedFromWindow in above code is not called because of nrestedscrollview.Is there any solution for it? Without nestedscrollview its woking perfectly, but i need to use nestedscrollview in my code.
onChildViewDetachedFromWindow is not called simply because when you use RecyclerView with WRAP_CONTENT inside NestedScrollView it attaches all elements at once and keeps them all the time. In other words elements will never be detached from RecyclerView inside NestedScrollView

How to create a CardView with clickable rows

For my App I need to create a layout similar to this:
I have been thinking about having a RecyclerView inside a CardView. But if I do it like that I have to create two separate row.xml (one for first and second row and the other for third and fourth). I was wondering if there is a simpler way to get the same result.
Can you point in the right direction?
Create it using Relative layout or linear layout,and set id for parent layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:id="#+id/card_view_notification"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
card_view:cardCornerRadius="2dp"
card_view:contentPadding="7dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:id="#+id/rel_one"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Version"
android:id="#+id/hedone"
android:textColor="#000000"
android:textSize="18sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="5.3(123)"
android:textColor="#000000"
android:layout_below="#+id/hedone"
android:textSize="14sp" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:background="#7B7A7F"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:id="#+id/relative_two"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Version"
android:id="#+id/hedtwo"
android:textColor="#000000"
android:textSize="18sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Disponibile"
android:textColor="#000000"
android:layout_below="#+id/hedtwo"
android:textSize="14sp" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:background="#7B7A7F"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:id="#+id/relative_three"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Your text"
android:textColor="#000000"
android:textSize="18sp" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:background="#7B7A7F"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:id="#+id/relative_four"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Yourtext"
android:textColor="#000000"
android:textSize="18sp" />
</RelativeLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
OUTPUT
Take a listview and initially make bottom textview gone. Make it visible as and when needed. Using cardview wont help you create such layout.
Maybe you could create a single row.xml and hide the subtitle when it's necessary.
mySubtitleTextView.setVisibility(View.GONE) like that subtitle doesn't take any space and title is still in center.
I would suggest making multiple card views and using the getItemViewType method. You can set various view types for different layouts and in the onCreateViewHolder method you can inflate the card view based on the view type.
How to create RecyclerView with multiple view type?
First of all a point with in a RecyclerView there will be CardView not reverse .if you want to do it with RecyclerView then follow some steps like:
step 1:
create a Layout named as Activity_main.xml which having a RecyclerView like:
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".ui.MainActivity">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/recycler_view"
android:scrollbars="vertical"
></android.support.v7.widget.RecyclerView>
</RelativeLayout>
step 2. Create layout named as product_layout which having a CardView like:
<?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="100dp">
<!--<TextView-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:text="Products"-->
<!--android:gravity="center"/>-->
<!--<RelativeLayout-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content">-->
<!--<TextView-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:gravity="center"-->
<!--android:textStyle="italic"-->
<!--android:text="Products"-->
<!--android:textAppearance="?android:textAppearanceLarge"/>-->
<!--</RelativeLayout>-->
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cardview">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/smartphone"
android:id="#+id/mobileimage"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SmartPhones"
android:layout_toRightOf="#+id/mobileimage"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:textStyle="bold"
android:id="#+id/productname"
android:layout_alignParentTop="true"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
step 3. Now set first RecyclerView layout in MainActivity.java, if you are working on json then parse it after that set Your adapter like:
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
adapter = new ProductAdapter(list, getApplicationContext());
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(adapter);
step 4. create an Adapter named as ProductAdapter.java dafile like:
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.dharmendras.productdetail.models.Products;
import com.example.dharmendras.productdetail.R;
import java.util.ArrayList;
public class ProductAdapter extends RecyclerView.Adapter <ProductAdapter.ProductsViewHolder> {
ArrayList<Products> products = new ArrayList<Products>();
private Context context;
public ProductAdapter(ArrayList<Products> products,Context context){
this.context = context;
this.products = products;
}
#Override
public ProductsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_layout,parent,false);
ProductsViewHolder productsViewHolder = new ProductsViewHolder(view);
return productsViewHolder;
}
#Override
public void onBindViewHolder(ProductsViewHolder holder, int position) {
Products pdt = products.get(position);
//Picasso.with(context).load(pdt.getImage_url()).resize(120, 60).into(holder.product_img);
//pdt.getProduct_name()
holder.product_name.setText(pdt.getProduct_name());
//Drawable myDrawable = context.getResources().getDrawable(pdt.getImage_url());
// int id = getContext().getResources().getIdentifier("imageName", "drawable", getContext().getPackageName());
// ImageView myImageView = (ImageView)findViewById(R.id.myImage);
//myImageView.setImageResource(R.drawable.icon);
// holder.product_img.setImageDrawable(pdt.getImage_url());
//imageView.setBackground(getResources().getDrawable(getResources().getIdentifier("name","id",getPackageName())));
String name = pdt.getImage_url();
int id = context.getResources().getIdentifier(name, "drawable",context.getPackageName());
Drawable drawable = context.getResources().getDrawable(id);
holder.product_img.setImageDrawable(drawable);
}
#Override
public int getItemCount() {
return products.size();
}
public static class ProductsViewHolder extends RecyclerView.ViewHolder{
ImageView product_img;
TextView product_name;
public ProductsViewHolder(View view){
super(view);
product_img = (ImageView) view.findViewById(R.id.mobileimage);
product_name = (TextView) view.findViewById(R.id.productname);
}
}
}
Step 5. if u want to add RecyclerView clickable then set onClickListener on RecyclerView in your Activity_main.java.

recyclerview android mix data pictures after scroll

i am developing and app that should show users instagram posts in a recyclerview...i get all data correctly but after i scroll my recyclerview pictures get mess up and one picture shows twice ...and other messed ups in my list.
here is my onbind View holder in recyclerview extends RecyclerView.Adapter :
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
Picasso.with(context).
load(imageThumbList.get(position)
).
into(ivimage);
igetPositionImage.getPosition(position);
/*imageLoader.DisplayImage(imageThumbList.get(position), ivimage);*/
count_like.setText(likeCounts.get(position));
and its my called up recycle view in my fragment:
private void setImageGridAdapter() {
recyclerView = (RecyclerView) getView().findViewById(R.id.recycler_view);
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getActivity(),3);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.addItemDecoration(new GridSpacingItemDecoration(1, dpToPx(1), true));
recyclerView.setItemAnimator(new DefaultItemAnimator());
RecycleListAdapter recycleListAdapter = new RecycleListAdapter(getActivity(),imageThumbList , likeCounts ,LikeGetterF.this);
recyclerView.setAdapter(recycleListAdapter);
Paginate.with(recyclerView , callbacks)
.setLoadingTriggerThreshold(2).
addLoadingListItem(true).
setLoadingListItemCreator(new RecycleListAdapter(getActivity(),imageThumbList,likeCounts,LikeGetterF.this))
.setLoadingListItemSpanSizeLookup(new RecycleListAdapter(getActivity(),imageThumbList,likeCounts,LikeGetterF.this))
.build();
//gvAllImages.setAdapter(new MyGridListAdapter(context,imageThumbList));
}
and my xml code :
> <?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:id="#+id/all_media_files"
>
<!-- <ListView -->
<!-- android:id="#+id/lvImages" -->
<!-- android:layout_width="fill_parent" -->
<!-- android:layout_height="wrap_content" > -->
<!-- </ListView> -->
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:background="#AFAFAFAF"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="true"
android:scrollbars="horizontal" />
</LinearLayout>
and my item adapter xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="20dp"
>
<FrameLayout
android:layout_width="100dp"
android:layout_height="100dp"
>
<ImageView
android:id="#+id/ivImage"
android:layout_width="100dp"
android:layout_height="100dp"
android:clickable="true"
android:scaleType="fitCenter"
android:src="#drawable/profile"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="bottom"
android:background="#AFAFAFAF"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:src="#drawable/like_red"
android:id="#+id/like_img"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:text="0"
android:gravity="right"
android:textColor="#color/textColorPrimary"
android:textSize="#dimen/buttonSizes"
android:id="#+id/like_count"
/>
</LinearLayout>
</FrameLayout>
<!-- android:background="#drawable/char_02"-->
</LinearLayout>
Remove final for position in bind holder,
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
int adapterPos=holder.getAdapterPosition();
Picasso.with(context).
load(imageThumbList.get(adapterPos)
).
into(ivimage);
igetPositionImage.getPosition(position);
holder.setIsRecyclable(false);
/*imageLoader.DisplayImage(imageThumbList.get(position), ivimage);*/
count_like.setText(likeCounts.get(position));
and use adapterPos everywhere in bindView holder

Categories

Resources