android:staggergridlayout while moving up shuffles - android

I am using staggered grid layout. the following is the code:
StaggeredGridLayoutManager glm= new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
glm.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
I am using GAP_HANDLING_NONE to avoid images swapping from one column to other.
When I start the app, the beginning of the screen is:
After scrolling down to bottom and when I return to the top. randomly the following three images show the layout (it keeps varying)

Staggered grids are likely to have gaps at the edges of the layout. To avoid these gaps, StaggeredGridLayoutManager can offset spans independently or move items between spans. You can control this behavior via setGapStrategy(int):
StaggeredGridLayoutManager sGrid = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
sGrid.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS);
I hope this will help a buddy.

Try below code.
Add this Override methode in your Adapter
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemViewType(int position) {
return position;
}
If this solution is not work for you!
Try below solution
I am using StaggeredGridLayout for getting all gallery images and it's working fine for me.
- Main Activity
/*Init Gallery Photos RecyclerView*/
gallerySglm = new StaggeredGridLayoutManager(2, RecyclerView.VERTICAL);
gallerySglm.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS);
binding.rvGalleryImg.setLayoutManager(gallerySglm);
galleryPhotosAdapter = new GalleryPhotosAdapter(context, galleryPhotoArrayList);
binding.rvGalleryImg.setAdapter(galleryPhotosAdapter);
galleryPhotosAdapter.notifyDataSetChanged();
/*End Init Gallery Photos RecyclerView*/
- Item Layout
Add ImageView inside ConstraintLayout
<?xml version="1.0" encoding="utf-8"?>
<layout>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/mainCard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="#color/main_bg"
app:cardCornerRadius="#dimen/_5sdp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="false"
android:layout_margin="#dimen/_2sdp"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/clMain"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.AppCompatImageView
android:id="#+id/img"
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="fitXY"
android:visibility="visible"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</layout>
- Adapter
public class GalleryPhotosAdapter extends RecyclerView.Adapter<GalleryPhotosAdapter.ViewHolder> {
private Context context;
private ArrayList<String> arrayList;
private ConstraintSet set = new ConstraintSet();
public GalleryPhotosAdapter(Context context, ArrayList<String> arrayList) {
this.context = context;
this.arrayList = arrayList;
}
#NonNull
#Override
public GalleryPhotosAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
ItemGalleryPhotosBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),
R.layout.item_gallery_photos, parent, false);
return new ViewHolder(binding);
}
#Override
public void onBindViewHolder(#NonNull final GalleryPhotosAdapter.ViewHolder holder, final int position) {
//Set size
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(arrayList.get(position), options);
String ratio = String.format(Locale.getDefault(), "%d:%d", options.outWidth, options.outHeight);
set.clone(holder.binding.clMain);
set.setDimensionRatio(holder.binding.img.getId(), ratio);
set.applyTo(holder.binding.clMain);
//End set size
Glide.with(context)
.load(arrayList.get(position))
.into(holder.binding.img);
}
#Override
public int getItemCount() {
return arrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ItemGalleryPhotosBinding binding;
public ViewHolder(ItemGalleryPhotosBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
}
}
I hope this can help You!
Thank You.

Use this simple solustion for your problem.
StaggeredGridLayoutManager straggerGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
straggerGridLayoutManager .setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS);
recyclerView.setLayoutManager(straggerGridLayoutManager);
dataList = YourDataList (Your Code for Arraylist);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerAdapter = new DataAdapter(dataList, recyclerView);
recyclerView.setAdapter(recyclerAdapter);
recyclerView.addOnScrollListener(new ScrollListener());
Create class for Custom RecyclerView Scroll Listener.
private class ScrollListener extends RecyclerView.OnScrollListener {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
recyclerView.invalidateSpanAssignments();
}
}

Notice the sample code I provided for you
Images are shown in different proportions based on their dimensions and even some apps show a mixed view of images and videos like Instagram. In the coming tutorial, we will be experiencing such a view where videos are loaded and played with autoplay features when we strolled onto the video view.
When it comes to the implementation technically there is no much difference in regular style of showing images to this staggering view.
Activity_main.Xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context="staggered.android.com.staggeredview.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/stagRecycleView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.constraint.ConstraintLayout>
Recyclerview_row.Xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp">
<ImageView
android:id="#+id/imgView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"/>
</RelativeLayout>
MainActivity.Java
public class MainActivity extends AppCompatActivity {
RecyclerViewAdapter adapter;
ArrayList<Integer> Image;
RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Image = new ArrayList<>(Arrays.asList(
R.drawable.img1,R.drawable.img2,
R.drawable.img3,R.drawable.img4,
R.drawable.img5,R.drawable.img6,
R.drawable.img7,R.drawable.img8,
R.drawable.img9,R.drawable.img10,
R.drawable.img1,R.drawable.img2,
R.drawable.img3,R.drawable.img4,
R.drawable.img5,R.drawable.img6,
R.drawable.img7,R.drawable.img8,
R.drawable.img9)
);
recyclerView = findViewById(R.id.stagRecycleView);
layoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
adapter = new RecyclerViewAdapter(this, Image);
recyclerView.setAdapter(adapter);
}
}
And finally
RecyclerViewAdapter.Java
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
ArrayList<Integer> Image;
Context context;
public RecyclerViewAdapter(Context context, ArrayList<Integer> Image) {
super();
this.context = context;
this.Image = Image;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.recyclerview_row, viewGroup, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
viewHolder.imgview.setImageResource(Image.get(i));
}
#Override
public int getItemCount() {
return Image.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ImageView imgview;
public ViewHolder(View itemView) {
super(itemView);
imgview = (ImageView) itemView.findViewById(R.id.imgView);
}
}
}
I hope this will help you ;)

Related

How to make large scrolling screens with images/buttons?

I have the following code:
<ScrollView
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:fillViewport="false"
tools:context=".bakers">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
...
in an activity. I have a few images on the activity and know I won't be able to fit them all on one screen, so I added a ScrollView with a height that is much larger than the screen. However, all this does is simply scale the existing images I have so that they are larger and take up more of the screen. I've tried fixing the fillViewport and clipToPadding settings, but this doesn't help anything.
Essentially what I'm asking is: Is there a way to add an image "below" the preview on screen using a ScrollView, so you can fit more on the screen than you normally would be able to? If I make the phone screen larger or the ScrollView larger, the images simply scale up.
Thanks
First you need use an recyclerview RecyclerView documentation
your activity view will have this code
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/buttonAction"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button action" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:scrollingCache="true" />
</LinearLayout>
you need create another layout in this case the layout that will display your image, example item_image.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/image"
android:layout_marginTop="4dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:src="#drawable/image_1"/>
</LinearLayout>
well now you have your views.
the next is create a new class thats implement an adapter: CardAdapter.java
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {
Context context;
public void setContext(Context context) {
this.context = context;
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView imgCover;
public ViewHolder(final View itemView, int type) {
super(itemView);
imgCover = itemView.findViewById(R.id.image);
}
}
#Override
public int getItemViewType(int position) {
return 1;
}
#NonNull
#Override
public CardAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = null;
RecyclerView.LayoutParams lp;
switch (viewType) {
case 1:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_card_payment, null, false);
lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
view.setLayoutParams(lp);
break;
}
return new CardAdapter.ViewHolder(view, viewType);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
}
#Override
public int getItemCount() {
return 10;
}
public Context getContext() {
return context;
}
}
and in your activity you will implement the recycler and the adapter
public class CardsActivity extends AppCompatActivity {
RecyclerView recyclerView;
CardAdapter cardAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cards);
initToolbar();
recyclerView = findViewById(R.id.recyclerView);
cardAdapter = new CardAdapter();
cardAdapter.setContext(this);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setAdapter(cardAdapter);
}
}
and its all.

Why isn't my DiffUtil with RecyclerView working?

I'm trying to create a simple DiffUtil Adapter to work with a RecyclerView and nothing shows up (I only get a blank activity). Here is the code:
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private MyAdapter mAdapter;
private ArrayList<User> myDataset;
private RecyclerView mRecyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdapter = new MyAdapter();
myDataset = new ArrayList<User>();
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
myDataset.add(new User("User1"));
myDataset.add(new User("User2"));
myDataset.add(new User("User3"));
mRecyclerView.setAdapter(mAdapter);
mAdapter.updateList(myDataset);
}
}
Here is MyAdapter.java:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private AsyncListDiffer<User> mAsyncListDiffer;
public static class MyViewHolder extends RecyclerView.ViewHolder {
public TextView singleItemTextView;
public View layout;
public ConstraintLayout constraintLayout;
public MyViewHolder(View v) {
super(v);
layout = v;
singleItemTextView = (TextView) v.findViewById(R.id.singleItemTextView);
constraintLayout = (ConstraintLayout) v.findViewById(R.id.constraintLayout);
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter() {
DiffUtil.ItemCallback<User> diffUtilCallback = new DiffUtil.ItemCallback<User>() {
#Override
public boolean areItemsTheSame(#NonNull User newUser, #NonNull User oldUser) {
return newUser.getUserId().equals(oldUser.getUserId());
}
#Override
public boolean areContentsTheSame(#NonNull User newUser, #NonNull User oldUser) {
return newUser.equals(oldUser);
}
};
mAsyncListDiffer = new AsyncListDiffer<>(this, diffUtilCallback);
}
// Create new views (invoked by the layout manager)
#Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_text_view, parent, false);
MyViewHolder vh = new MyViewHolder(v);
return vh;
}
#Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
User user = mAsyncListDiffer.getCurrentList().get(position);
holder.singleItemTextView.setText(user.getUserId());
}
#Override
public int getItemCount() {
return mAsyncListDiffer.getCurrentList().size();
}
public void updateList(ArrayList<User> newList) {
mAsyncListDiffer.submitList(newList);
}
}
Here is User.java:
public class User {
private String mUserId;
public User(String userId) {
mUserId = userId;
}
public String getUserId() {
return mUserId;
}
}
Here are the XML files:
acivity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/my_recycler_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="4dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:background="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
and here is single_text_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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/constraintLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:layout_editor_absoluteY="81dp">
<TextView
android:id="#+id/singleItemTextView"
android:layout_width="78dp"
android:layout_height="78dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
I know that DiffUtil is supposed to compare 2 different lists and here I only set up the initial list, but still, shouldn't it at least display the first list (with 3 users in this case)?
Set LayoutManager with Recyclerview
// add data on list
private ArrayList<User> myDataset=new ArrayList;
myDataset.add(new User("abc"));
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
LayoutManager layoutManager=new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
MyAdapter myAdapter=new MyAdapter();
mAdapter.updateList(myDataset);
mRecyclerView.setAdapter(mAdapter);
Do you check if your getItemCount() method returns more than 0 ?
If it is, you should look at your item view. Try to remove constraints on this view first to see if anything appears on the screen.

RecyclerView horizontal wrap_content max size

I have a RecyclerView (image below) and I have some content (a picture and some text) loading from an API. I'd like all the rows to be exactly the same height, but the text could vary wildly in length. So the yellow box should be bigger to match the green box which has more text. If that makes sense?
Currently, I'm using a LinearLayoutManager set to Horizontal, but I've also tested with a GridLayoutManager with 1 row which produces the same effect.
I'm also using setAutoMeasureEnabled to true.
Is there a simple way to do this with a RecyclerView?
If not, how would be best to calculate the height and send it down to each ViewHolder?
Thank you,
As requested:
my initialisation which is in the MainActivity onCreate:
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
// use a linear layout manager
mLayoutManager = new GridLayoutManager(MainActivity.this, 1, GridLayoutManager.HORIZONTAL, false);
mLayoutManager.setAutoMeasureEnabled(true);
mRecyclerView.setLayoutManager(mLayoutManager);
setUpSomeData();
TopFilmsCard card1 = (TopFilmsCard)cards.get(0);
mAdapter = new FilmsAdapter(card1.getFilms());
mRecyclerView.setAdapter(mAdapter);
FilmsAdapter
private class FilmsAdapter
extends RecyclerView.Adapter<FilmsCardHolder> {
private List<Film> films;
public FilmsAdapter(List<Film> films) {
this.films = films;
}
#Override
public int getItemViewType(int position) {
return R.layout.holder_top_films;
}
#Override
public FilmsCardHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new FilmsCardHolder(LayoutInflater.from(MainActivity.this).inflate(R.layout.holder_film, parent, false));
}
#Override
public void onBindViewHolder(FilmsCardHolder holder, int position) {
holder.bindCard(films.get(position));
}
#Override
public int getItemCount() {
return films.size();
}
}
FilmsCardHolder
public class FilmsCardHolder extends RecyclerView.ViewHolder {
public FilmsCardHolder(View itemView) {
super(itemView);
}
public void bindCard(Film film) {
ViewGroup layout = (ViewGroup)itemView.findViewById(R.id.layoutFilm);
TextView lblFilm = (TextView)itemView.findViewById(R.id.lblFilmName);
ImageView imgPicture = (ImageView)itemView.findViewById(R.id.imgPicture);
layout.setBackgroundResource(film.getColor());
lblFilm.setText(film.getName());
imgPicture.setImageResource(film.getPicture());
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#CCD1D9"/>
holder_film
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutFilm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#5D9CEC">
<TextView
android:id="#+id/lblFilmName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="10dp"
android:textSize="22sp"/>
<ImageView
android:id="#+id/imgPicture"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center_horizontal"
/>

RecyclerView inside ViewPager Fragment don't show

So i have a fragment, which have a viewpager with tablayout, which consist of two tabs-two fragments.
The thing is, recyclerview shows empty, and i have no idea why.,
Tab Fragment LAyout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Tag Fragment:
List<OrderListItem> orderList = new ArrayList<>();
orderList.add(new OrderListItem(333, "ABCDE", new Date(), new Date(), false, true));
adapter = new OrderListAdapter(orderList, this.getActivity());
layoutManager = new LinearLayoutManager(this.getActivity(), LinearLayoutManager.VERTICAL, false);
myRecyclerView.setLayoutManager(layoutManager);
myRecyclerView.setAdapter(adapter);
Adapter:
public class OrderListAdapter extends RecyclerView.Adapter<OrderListAdapter.ViewHolder>{
private List<OrderListItem> orderList;
#LayoutRes
private final int layoutRes;
private Context context;
private final LayoutInflater inflater;
public OrderListAdapter(List<OrderListItem> orderList, Context context){
this.orderList = orderList;
this.layoutRes = R.layout.order_list_item_layout;
this.context = context;
inflater = LayoutInflater.from(this.context);
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(layoutRes, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
final OrderListItem item = orderList.get(position);
}
public void setItems(List<OrderListItem> orderList){
this.orderList.clear();
this.orderList.addAll(orderList);
notifyDataSetChanged();
}
#Override
public int getItemCount() {
return orderList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View itemView) {
super(itemView);
}
}
}
RecycleView Item some colorful layouts insider, so i know if the child layout is there or not, which it isnt. any idea why recyclerview is empty?
edit1: i know the recyclerview is there, because its in a lolipop phone, and if i make a movement at the recycler place it shows me the ripple top and bottom scroll border. but the child layouts are empty and blank, and should be colorful as i specificed in the child layout.
eit2. just used a listview with a simpleAdapter and it is showing. there must be something buggy with the rv
edit3: row layout (i should clearly see an empty textview with a color, besides not any value setted.)
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#color/black"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/material_deep_teal_200"
android:text="New Text"
android:id="#+id/textView" />
</LinearLayout>
Change:
android:layout_width="match_parent"
android:layout_height="match_parent"
to
android:layout_width="50dp" // set yourself
android:layout_height="50dp" // set yourself
Where is the logic in which you set OrderListItem properties to ui?
You have first to set ViewHolder components:
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView mTitleTv;
public ViewHolder(View itemView) {
super(itemView);
mTitle = itemView.findViewById(R.id.textview_id_in_your_xml_file);
}
}
And then set them in onBindViewHolder:
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
final OrderListItem item = orderList.get(position);
holder.mTitleTv.setText(item.getReplaceThisWithAStringProperty);
}
EDIT
If you have to use default LinearLayoutManager properties use this constructor:
layoutManager = new LinearLayoutManager(getActivity());
Instead of this:
layoutManager = new LinearLayoutManager(this.getActivity(), LinearLayoutManager.VERTICAL, false);
Add also fixed size property for RecyclerView:
recyclerView.setHasFixedSize = true;

Implementing an RecycleView or CardView items with the same design for eachother

I need to know, when the user clicking on one item(or for example, whatsApp contacts) WhatsApp showing to the user one design with this picture example:
here is the recycle view items:
and i'm using :
http://code.tutsplus.com/tutorials/getting-started-with-recyclerview-and-cardview-on-android--cms-23465
Main-activity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView rv = (RecyclerView)findViewById(R.id.rv);
rv.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(context);
rv.setLayoutManager(llm);
RVAdapter adapter = new RVAdapter(persons);
rv.setAdapter(adapter);
}
}
Mainactivity Xml layout:
<?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:padding="16dp"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cv"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/person_photo"
android:src="#mipmap/ic_launcher"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="16dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/person_name"
android:layout_toRightOf="#+id/person_photo"
android:layout_alignParentTop="true"
android:textSize="30sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/person_age"
android:layout_toRightOf="#+id/person_photo"
android:layout_below="#+id/person_name"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/rv"/>
</LinearLayout>
here is the Adaptor:
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder>{
#Override
public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.activity_main, viewGroup, false);
PersonViewHolder pvh = new PersonViewHolder(v);
return pvh;
}
#Override
public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
personViewHolder.personName.setText(persons.get(i).name);
personViewHolder.personAge.setText(persons.get(i).age);
personViewHolder.personPhoto.setImageResource(persons.get(i).photoId);
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
#Override
public int getItemCount() {
return persons.size();
}
List<Person> persons;
RVAdapter(List<Person> persons){
this.persons = persons;
}
public static class PersonViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView personName;
TextView personAge;
ImageView personPhoto;
PersonViewHolder(View itemView) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.cv);
personName = (TextView)itemView.findViewById(R.id.person_name);
personAge = (TextView)itemView.findViewById(R.id.person_age);
personPhoto = (ImageView)itemView.findViewById(R.id.person_photo);
}
}
}
currently, there is an error in MainActivity Java codes:
cannot resolve symbol Contect and also for Person!
So, after this (i hope this fixed).
I need to when user clicked on the each item, It showing to us similar design, But, With names for each item.
Example: user clicked on item 2: Lavery Maiss
and then goto another activity or layout and show us this name.
and if user clicked on the item 1: Emma Wilson it show us in another activity same design for each item, But, with Emma Wilson name.
What should i do and what's wrong with my codes?
what we can do for showing this ?
Cheers!
After researching about this, i found a good tutorial which is no one mention it.
https://github.com/tarek360/Material-Animation-Samples
So, We need a Adaptor for doing this like below:
public class BlogRecyclerAdapter extends
RecyclerView.Adapter<BlogRecyclerAdapter.SimpleItemViewHolder> {
private List<Blog> items;
// Provide a reference to the views for each data item
// Provide access to all the views for a data item in a view holder
public final static class SimpleItemViewHolder extends RecyclerView.ViewHolder {
ImageView image;
TextView title;
TextView subTitle;
CardView cardView;
public SimpleItemViewHolder(View itemView) {
super(itemView);
image = (ImageView) itemView.findViewById(R.id.imageThumb);
title = (TextView) itemView.findViewById(R.id.title);
subTitle = (TextView) itemView.findViewById(R.id.subTitle);
cardView = (CardView) itemView.findViewById(R.id.cardView);
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public BlogRecyclerAdapter(List<Blog> items) {
this.items = items;
}
// Return the size of your dataset (invoked by the layout manager)
#Override
public int getItemCount() {
return this.items.size();
}
// Create new items (invoked by the layout manager)
// Usually involves inflating a layout from XML and returning the holder
#Override
public SimpleItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View itemView = LayoutInflater.from(viewGroup.getContext()).
inflate(R.layout.blog_item, viewGroup, false);
return new SimpleItemViewHolder(itemView);
}
// Replace the contents of a view (invoked by the layout manager)
// Involves populating data into the item through holder
#Override
public void onBindViewHolder(SimpleItemViewHolder viewHolder, int position) {
viewHolder.image.setImageResource(items.get(position).getImageRes());
viewHolder.image.setTag(position);
viewHolder.title.setText(items.get(position).getTitle());
viewHolder.subTitle.setText(items.get(position).getSubTitle());
viewHolder.cardView.setCardBackgroundColor(items.get(position).getBackGroundColor());
}
}
Take a look at these codes:
// Replace the contents of a view (invoked by the layout manager)
// Involves populating data into the item through holder
#Override
public void onBindViewHolder(SimpleItemViewHolder viewHolder, int position) {
viewHolder.image.setImageResource(items.get(position).getImageRes());
viewHolder.image.setTag(position);
viewHolder.title.setText(items.get(position).getTitle());
viewHolder.subTitle.setText(items.get(position).getSubTitle());
viewHolder.cardView.setCardBackgroundColor(items.get(position).getBackGroundColor());
}
Here is a good example for doing this with Animation:
https://github.com/tarek360/Material-Animation-Samples/tree/master/app/src/main/java/com/tarek360/animationsamples
exactly what we need ;)

Categories

Resources