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;
Related
How can I create a horizontal scroll using a recycler view, for the elements under Features and Rules?
I do know that for Property type and Number of rooms I can use two separate recycler views. However, Would I need to create two recycler views to display those elements since the length of the text elements will be uneven and to cater for an ever-expanding list of items if more rules and features are added?
You can create a horizontal scrollview by doing this ie set horizontal
layout manager
recyclerViewTeams.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
Example code
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerViewProperty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp" />
in Java class
private RecyclerView recyclerViewProperty;
recyclerViewProperty = (RecyclerView) view.findViewById(R.id.recyclerViewProperty);
recyclerViewTeams.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
public class TeamsAdapter extends RecyclerView.Adapter {
ArrayList<TeamsListingModal> arrayList;
private Activity mContext;
private int selected_position;
private boolean isSelected = false;
public class MyView extends RecyclerView.ViewHolder {
public TextView textview;
private RelativeLayout parentLayout;
public MyView(View view) {
super(view);
parentLayout = (RelativeLayout) view.findViewById(R.id.parentLayout);
textview = (TextView) view.findViewById(R.id.textview);
}
}
public TeamsAdapter(Activity activity, ArrayList<CustomModal> arrayList) {
this.mContext = activity;
this.arrayList = arrayList;
}
#Override
public MyView onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.property_adapter_item, parent, false);
return new MyView(itemView);
}
#Override
public void onBindViewHolder(final MyView holder, final int position) {
//inflate views here
}
#Override
public int getItemCount() {
return arrayList.size();
}
}
and xml here property_adapter_item
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/parentLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:id="#+id/textView"
android:layout_width="70dp"
android:layout_height="70dp"
app:border_width="5dp" />
</RelativeLayout>
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
In Kotlin Horizontal Scroolview in RecyclerView: - We use this Code
recycler_upcoming.layoutManager =
LinearLayoutManager(sContext, LinearLayoutManager.HORIZONTAL, false)
recycler_upcoming.adapter =
InboxUpcomingAdapter(list, sContext)
-> sContext mean Ur Context
-> If you pass false then scroll right to left
-> If You pass true then scroll left to right
I want to build a complex layout using recyclerview android. In the layout, I want to have a camera button to the top left fixed and a recyclerview wrapped around it with gallery images. I have checked flexbox layout manager for recyclerview but it doesn't seem to match my use-case.
I want the header to be non-repeating and not to scroll with other items vertically. Here's the layout for the header:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/shareLayout"
android:layout_width="185dp"
android:layout_height="135dp"
android:layout_below="#id/trendingToolbar"
android:background="#color/black">
<ImageView
android:id="#+id/cameraShareIV"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
app:srcCompat="#drawable/camera_white" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/cameraShareIV"
android:layout_centerHorizontal="true">
<TextView
android:id="#+id/infoTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="20dp"
android:gravity="center_horizontal"
android:text="#string/share_pic_video"
android:textColor="#android:color/white"
android:textSize="13sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/infoTxt"
android:layout_marginLeft="16dp"
android:text="#string/share_timeout_txt"
android:textColor="#color/colorPrimaryDark"
android:textSize="11sp"
android:textStyle="bold" />
</RelativeLayout>
and in my activity, here's the XML:
<RelativeLayout 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="base.android.com.thumbsapp.UI.Fragments.TrendingFragment">
<include layout="#layout/trending_toolbar"
android:id="#+id/trendingToolbar"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/trendingRV"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/trendingToolbar"/>
Previously, I had the header inside the activity XML but had no way to wrap a recyclerview around it. So, I have decide to use an adapter like below:
public class TrendingAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final String TAG = TrendingAdapter.class.getSimpleName();
private Context context;
private List<Trending> itemList;
private static final int HEADER = 0;
private static final int ITEMS = 1;
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v;
switch (viewType){
case HEADER:
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.trending_header, parent, false);
return new TrendingHeaderViewHolder(v);
case ITEMS:
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.trending_items_layout, parent, false);
return new TrendingItemsViewHolder(v);
}
return null;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
Trending tr = itemList.get(position);
if (holder instanceof TrendingHeaderViewHolder){
((TrendingHeaderViewHolder) holder).cameraShareIV.setOnClickListener( view -> {
// TODO: 4/2/2018 select image from gallery
});
} else if (holder instanceof TrendingItemsViewHolder){
// TODO: 4/2/2018 populate gallery items here with picasso
}
}
#Override
public int getItemCount() {
return itemList.size();
}
#Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
}
I'm confused how to make the header stick and also what to do for getItemViewType method.
Is this the right way to approach this?
Can anyone help out? Thanks.
For this lay out i suggest better option is use this header view
https://github.com/edubarr/header-decor
To make things simple i suggest you to look into this library
In your XML Place RecylerView into StickyHeaderView,choose horizontal or vertical orientation for your RecylerView
<tellh.com.stickyheaderview_rv.StickyHeaderView
android:id="#+id/stickyHeaderView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:scrollbars="vertical" />
</tellh.com.stickyheaderview_rv.StickyHeaderView>
Create data bean class for each item type in RecyclerView. They should extend DataBean. Override the method
public boolean shouldSticky() to decide whether the item view should be suspended on the top.
public class User extends DataBean {
private String login;
private int id;
private String avatar_url;
private boolean shouldSticky;
#Override
public int getItemLayoutId(StickyHeaderViewAdapter adapter) {
return R.layout.item_user;
}
public void setShouldSticky(boolean shouldSticky) {
this.shouldSticky = shouldSticky;
}
// Decide whether the item view should be suspended on the top.
#Override
public boolean shouldSticky() {
return shouldSticky;
}
}
public class ItemHeader extends DataBean {
private String prefix;
#Override
public int getItemLayoutId(StickyHeaderViewAdapter adapter) {
return R.layout.header;
}
#Override
public boolean shouldSticky() {
return true;
}
}
Create ViewBinder to bind different type views with specific data beans. As you see, provideViewHolder(View itemView) corresponds for onCreateViewHolder in RecyclerView, and bindView corresponds for onBindViewHolder in RecyclerView.
public class ItemHeaderViewBinder extends ViewBinder<ItemHeader, ItemHeaderViewBinder.ViewHolder> {
#Override
public ViewHolder provideViewHolder(View itemView) {
return new ViewHolder(itemView);
}
#Override
public void bindView(StickyHeaderViewAdapter adapter, ViewHolder holder, int position, ItemHeader entity) {
holder.tvPrefix.setText(entity.getPrefix());
}
#Override
public int getItemLayoutId(StickyHeaderViewAdapter adapter) {
return R.layout.header;
}
static class ViewHolder extends ViewBinder.ViewHolder {
TextView tvPrefix;
public ViewHolder(View rootView) {
super(rootView);
this.tvPrefix = (TextView) rootView.findViewById(R.id.tv_prefix);
}
}
}
Instantiate StickyHeaderViewAdapter for RecyclerView and register ViewBinders for each item types.
rv = (RecyclerView) findViewById(R.id.recyclerView);
rv.setLayoutManager(new LinearLayoutManager(this));
List<DataBean> userList = new ArrayList<>();
adapter = new StickyHeaderViewAdapter(userList)
.RegisterItemType(new UserItemViewBinder())
.RegisterItemType(new ItemHeaderViewBinder());
rv.setAdapter(adapter);
I had a listview which item contain fab & textview. The listviewitem layout code bellow :
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.FloatingActionButton
android:id="#+id/fabActiveStat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:clickable="true"
android:focusable="false"
app:fabSize="mini"
app:srcCompat="#drawable/ic_done" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fabEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="0dp"
android:layout_toEndOf="#+id/fabActiveStat"
android:layout_toLeftOf="#+id/fabActiveStat"
android:clickable="true"
android:focusable="false"
app:fabSize="mini"
app:srcCompat="#android:drawable/ic_menu_edit" />
<TextView
android:id="#+id/tvDesc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/fabEdit"
android:layout_centerVertical="true"
android:layout_marginLeft="0dp"
android:layout_marginStart="0dp"
android:layout_toEndOf="#+id/fabEdit"
android:maxLines="1"
android:text=""
android:textSize="17sp"
android:clickable="true"
android:focusable="false"
android:textStyle="bold" />
</RelativeLayout>
The Adapter Code Below :
ctx = getActivity();
// The desired columns to be bound
AScolumns = new String[]{
//dbHandler.COLUMN_ID,
dbHandler.COLUMN_CATEGORY,
//dbHandler.COLUMN_ACTIVE_STATUS
};
AIto = new int[]{
//R.id.fabActiveStat,
R.id.tvDesc,
//R.id.fabEdit
};
dbHandler = new databaseHandler(ctx, null, null, 1);
csAdapter = new SimpleCursorAdapter(ctx, R.layout.view_categ_data, null, AScolumns, AIto, 0);
lvCategory.setAdapter(csAdapter);
getLoaderManager().initLoader(0, null, this);
lvCategory.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d(TAG, "Listview di click");
}
});
}
When all fab removed and I'm using more than one textview listview on item click work just fine. But when the fab used listview on item click didn't work at all. Based on what I read on the internet focusable must be set to false, but that didn't work to. Anyone know how to put fab as list view item and make the textview & fab clickable?
Note : I am a novice
You can use RecyclerView. I use the String model here, but you can use your own custom model later. To populate a list (of items), you need to create custom adapter and custom view (just like you did). Here is the custom adapter:
public abstract class CustomAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private List<String> data;
private LayoutInflater inflater;
public CustomAdapter(Context context, List<String> data) {
this.context = context;
this.data = data;
this.inflater = LayoutInflater.from(context);
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.custom_view, parent, false); //use your custom view here
return new BodyViewHolder(view);
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
String model = data.get(position);
BodyViewHolder body = (BodyViewHolder) holder;
body.populate(model);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemCount() {
return data.size();
}
class BodyViewHolder extends RecyclerView.ViewHolder {
TextView tvDesc;
FloatingActionButton btn_edit;
FloatingActionButton btn_active;
ImageView imageView;
public BodyViewHolder(View itemView) {
super(itemView);
tvDesc = (TextView) itemView.findViewById(R.id.tvDesc);
btn_active = (FloatingActionButton) itemView.findViewById(R.id. fabActiveStat);
btn_edit = (FloatingActionButton) itemView.findViewById(R.id.fabEdit);
}
public void populate(final String model) {
tvDesc.setText(model);
btn_edit.setOnClickListener(view -> onModelClick(model));//firing listener to btn_edit, call the abstract method
}
}
public abstract void onModelClick(ProductModel model);
}
In your xml file:
<android.support.v7.widget.RecyclerView
android:padding="5dp"
android:id="#+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
In your activity:
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
List<String> listProduct = new ArrayList<>();
CustomAdapter adapterProduct = new CustomAdapter(getApplicationContext(), listProduct) {
#Override
public void onModelClick(String model) {
//your custom action
}
};
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setAdapter(adapterProduct);
//add some data to the ArrayList
adapter.notifyDataSetChanged();
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 ;)
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 ;)