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
Related
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"
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.
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()));
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.
How to do add custom layout in NavigationView and design my create custom NavigationView use material design,i want put my drawer icon to right and text left of it something like this
I Search too much and this is my experience that works fine
at first create layout for header. its name is nav_header_main.xml then put it in layouts folders in res and put this code in it..
<?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="#dimen/nav_header_height"
android:background="#drawable/header"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:theme="#style/ThemeOverlay.AppCompat.Dark"
android:gravity="top">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/cv_nave_profile_image"
android:layout_width="#dimen/nav_profile_image"
android:layout_height="#dimen/nav_profile_image"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/profile"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/cv_nave_profile_image"
android:layout_alignParentTop="true"
android:padding="#dimen/activity_horizontal_margin"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_nav_name"
android:textStyle="bold"
android:typeface="sans"
android:textColor="#ffffff"
android:gravity="right"
android:layout_gravity="right"
android:text="رخداد جدید"
android:paddingBottom="5dp"
android:textSize="#dimen/body"
/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:typeface="sans"
android:textColor="#ffffff"
android:id="#+id/tv_nav_phone"
android:layout_alignParentLeft="true"
android:text="0370077315"
/>
</RelativeLayout>
</LinearLayout>
then i include it as child of NavigationView and For menu item i use RecyclerView to show menu and icon so my NavigationView is
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="spydroid.ir.dorobar.Activities.SearchActivity">
<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.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_search" />
<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" />
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView android:id="#+id/nav_view"
android:layout_width="fill_parent" android:layout_height="match_parent"
android:layout_gravity="right" android:fitsSystemWindows="true"
android:layout_marginLeft="#dimen/nav_margin"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<include layout="#layout/nav_header_main" />
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/drawer_slidermenu"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginTop="16dp"/>
</RelativeLayout>
</LinearLayout>
</android.support.design.widget.NavigationView>
just you have to remember put your NavigationView in DrawerLayout
then i create layout for menu item with ImageView and TextView this layout and this name is card_drawer_item.xml and its code is here
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp">
<ImageView
android:id="#+id/drawer_icon"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:src="#drawable/ic_about"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/drawer_text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toLeftOf="#id/drawer_icon"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:typeface="sans"
android:paddingRight="40dp"/>
</RelativeLayout>
then i create ViewHolder folder for this layout.
public class DrawerItemHolder extends RecyclerView.ViewHolder {
public ImageView itemIcon;
public TextView itemText;
public DrawerItemHolder(View itemView) {
super(itemView);
itemIcon= (ImageView) itemView.findViewById(R.id.drawer_icon);
itemText= (TextView) itemView.findViewById(R.id.drawer_text);
}
}
now i define text of menu items as string array and array that contains menu icons in menu in strings.xml
<string-array name="drawer_items">
<item>setting</item>
<item>add record</item>
<item>ads</item>
<item>about</item>
<item>call</item>
<item>help</item>
<item>privacy</item>
</string-array>
<array name="drawers_icons">
<item>#drawable/ic_action_settings</item>
<item>#drawable/ic_plus</item>
<item>#drawable/ic_ads</item>
<item>#drawable/ic_about</item>
<item>#drawable/ic_phone</item>
<item>#drawable/ic_help</item>
<item>#drawable/ic_policy</item>
</array>
then we just need an Adapter like this
public class DrawerItemAdapter extends RecyclerView.Adapter<DrawerItemHolder> {
// slide menu items
private List<DrawerItem> items;
private List<Integer> drawerIcons;
public DrawerItemAdapter(List<DrawerItem> items) {
super();
this.items = items;
}
#Override
public DrawerItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.
from(parent.getContext()).
inflate(R.layout.card_drawer_item, parent, false);
return new DrawerItemHolder(itemView);
}
#Override
public void onBindViewHolder(DrawerItemHolder holder, int position) {
holder.itemIcon.setImageResource(items.get(position).getIconId());
holder.itemText.setText(items.get(position).getText());
}
#Override
public int getItemCount() {
return items.size();
}
}
every thing is ok .. just now we have to set NavigationView in Activity.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
recList = (RecyclerView) findViewById(R.id.drawer_slidermenu);
recList.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
recList.setLayoutManager(llm);
String []itemsTitle=getResources().getStringArray(R.array.drawer_items);
TypedArray icons=getResources().obtainTypedArray(R.array.drawers_icons);
List<DrawerItem>drawerItems= new ArrayList<DrawerItem>();
for(int i=0;i<itemsTitle.length;i++){
drawerItems.add(new DrawerItem(icons.getResourceId(i,-1),itemsTitle[i]));
}
DrawerItemAdapter ad= new DrawerItemAdapter(drawerItems);
recList.setAdapter(ad);
}
#Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.END)) {
drawer.closeDrawer(GravityCompat.END);
return;
}
super.onBackPressed();
}