Actually I'm trying to use recyclerview as a viewpager which is working fine by the help of PagerSnapHelper() class . But now the problem is how can I force recyclerview to change item heights dynamically and not show white space . Now when I try to load drawable pictures of two different sizes i.e a small and a bigger at first the smaller one is having correct size but when I scroll to next and then first some white space is shown below the imageView.You can see aforementioned problems in the given pictures.
XML:
<android.support.v7.widget.RecyclerView
android:id="#+id/rView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
Recyclerview Items:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/recyclerImgView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</FrameLayout>
Adapter Code:
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Glide.with(ctx).load(pics.get(position)).into(holder.imageView);
}
Activity Code:
rView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL,false));
rView.setAdapter(adp);
SnapHelper snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(rView);
Change your Recyclerview Items with this code by adding scaleType in your image view
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="150dp">
<ImageView
android:id="#+id/recyclerImgView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"/>
</FrameLayout>
you can change image view width and height like below code..
<ImageView
android:id="#+id/recyclerImgView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
and recycler view change height like below code..
<android.support.v7.widget.RecyclerView
android:id="#+id/rView"
android:layout_width="#dimen/_100sdp"
android:layout_height="#dimen/_100sdp"
/>
add this below dependency into app level gradle file and give size is same all the device like below ..
implementation 'com.intuit.sdp:sdp-android:1.0.4'
Related
I have this problem - I guess someone can surely put light on what the heck is going wrong here...
My overall flow goes like this - Activity(#+id/activity) shows a fragment(#+id/image_container) from screen bottom when a button is tapped(keyboard closes if present and fragment shown sliding upwards from bottom), this fragment shows a viewpager with page indicator in linear layout, viewpager shows another fragment which contains recycler view with horizontal gridlayoutmanager. This recycler view contains various imageview and i need to arrange them keeping my rows fixed say 3 but columns can be variable based on screen width and density. Imageview has fixed height and width.
This is my activity layout
<?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="match_parent"
android:id="#+id/activity">
<include
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentTop="true" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="4dp"
android:id="#+id/divider"
android:layout_below="#+id/toolbar"/>
<include
layout="#layout/edit_bar"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_below="#+id/divider"/>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/edit_bar" />
<FrameLayout
android:id="#+id/image_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/container"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
// Fragment inside the image_container
<?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"
android:weightSum="1">
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="12dp"
android:layout_weight=".9"/>
<LinearLayout
android:id="#+id/sliderDots"
android:layout_width="match_parent"
android:layout_height="0dp"
android:gravity="center_vertical|center_horizontal"
android:orientation="horizontal"
android:layout_weight=".1"/>
// Fragment inside viewpager
<?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.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="26dp"
android:paddingRight="26dp">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
// Item inside recycler view
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/imageView"
android:layout_width="48dp"
android:layout_height="48dp"
tools:visibility="visible"
android:layout_margin="6dp"/>
// Fragment code which gets added to image_container upon button tap
FragmentManager fm = activity.getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.view_transition_up_from_bottom, R.anim.view_transition_down_to_bottom);
fragmentTransaction.add(R.id.image_container, new ImageFragment(activity.getApplicationContext())).commit();
Inside this ImageFragment i calculate the rows(this is fixed let's say 3) and columns(Math.round((dpWidth - 52) / 60)) - 52(26*2 - for both side of recycler view, 60 - space occupied my 1 item including width and margin(L+R)), hence i know the images to be shown in each page and pass it to ImageViewFragment containing the recycler view which is used by adapter.
I use
recyclerView.setLayoutManager(new CustomGridLayoutManager(getActivity(), 3, CustomGridLayoutManager.HORIZONTAL, false));
to make my recycler view and hence i use fixed rows.
So my question is -
1. I wrote the code to keep my rows fixed and calculate columns based on screen width but sometimes i see too much spacing from right side of recycler view to screen edge, i kept it fixed with 26 dp but it shows more that that. How can i implement this type of functionality where i see uniform spacing in grid form??
2. How to handle orientation change when grid fragment is open, my activity is not redrawn. as i see i can only override onConfigurationChanged.
my friends
I am a new in android and I wanted to do an example (code) like the picture
GridView - Images,Text and ItemClick
Thanks to all
You are looking for RecyclerView. Create a layout and add recyclerView to it then create another layout file for the rows of the recyclerview as below:
<RelativeLayout
android:id="#+id/relativeLayout2"
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"/>
</RelativeLayout>
Create another layout row_layout.xml
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#mipmap/ic_launcher" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"/>
</RelativeLayout>
Then in your MainActivity, add following lines of code:
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager gridLayoutManager = new GridLayoutManager(this, 2, false);
recyclerView.setLayoutManager(gridLayoutManager);
After this, create an Adapter according to your specific need for the recyclerView which can be found with a quick google search. Just remmeber to infalte your row_layout in the adapter's onCreateViewHolder. After creating the adapter just set it to your recyclerView.
recyclerView.setAdapter(your_custom_adapter);
This is just a gist of what you ought to do. It'll get you started in the right direction. The rest is just a search away.
I am trying to make viewpager work like one in google playstore in movies,music,books section.
Below is screenshot of one:
This is screenshot of viewpager i want:
below is my xml code:
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/vp"
android:layout_width="match_parent"
android:layout_height="160dp"
android:layout_marginBottom="5dp">
</android.support.v4.view.ViewPager>
below is java code:
ViewPager vp;
int mResources[]= {R.drawable.b,R.drawable.c,R.drawable.d};
vp = (ViewPager)findViewById(R.id.vp);
vp.setPageMargin(-10);
vp.setAdapter(new ViewPageAdapter(ViewPagerActivity.this,mResources));
below is xml for single item:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="#eee"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_width="match_parent"
android:layout_height="220dp">
<android.support.v7.widget.CardView
android:elevation="10dp"
app:cardCornerRadius="10dp"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="200dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="#drawable/c"
android:id="#+id/imageView" />
</android.support.v7.widget.CardView>
Thanks in advance for help
If I'm not mistaken, you want a ViewPager whose next/previous items partly showing.
If so, set minus page margin to the viewPager by ViewPager.setPageMargin(int), and add (positive) margins to each item's left and right.
I hope it'll help you.
You should use RecyclerView with LinearLayoutManager with a horizontal orientation. This post should answer your question. ViewPager is typically used in conjunction with entire fragments as opposed to scrolling components within those fragments according to the documentation.
I am developing an Android app. I am not good in Android development. In my app, I am using RecyclerView and CardView with StaggeredGridLayoutManager. Because I want each cell size in the list different to each other and staggered like in below image.
But when I run my app, cells are not staggered and they are equal in size to each other.
This is screenshot
This is my recycler view XML
<android.support.v7.widget.RecyclerView
android:id="#+id/df_rv_destination"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
This is XML for cell item
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/di_iv_image"
android:scaleType="centerCrop"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:textSize="17dp"
android:textColor="#color/white"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:id="#+id/di_tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</android.support.v7.widget.CardView>
This is how I configure my RecyclerView with StaggerGridLayoutManager
StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, 1);
rcDestinations.setLayoutManager(staggeredGridLayoutManager);
rcDestinations.setItemAnimator(new DefaultItemAnimator());
regionsList = new ArrayList<Region>();
destinationsAdapter = new DestinationsAdapter(getActivity(),regionsList);
rcDestinations.setAdapter(destinationsAdapter);
So why is my RecyclerView not staggered? How can I fix it?
set your imageview with android:adjustViewBounds="true"
Your cell items all got the same size because there is nothing that would change size... I guess you want to get bigger items if you got bigger pics
First of all change this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
to this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
then instead of:
<ImageView
android:id="#+id/di_iv_image"
android:scaleType="centerCrop"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
try:
<ImageView
android:id="#+id/di_iv_image"
android:scaleType="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
and scale the images height to your wished size before adding them ;)
with cropCenter you always get the images cropped to the same size
666
You need to force height resizing to have the effect you wanted.
I am using data binding so, a set the height like this:
public int getHeight(int position) {
Random r = new Random();
int randomHeight = r.nextInt(Math.abs(mRecyclerViewHeight)/9) + Math.abs(mRecyclerViewHeight)/4;
if(position%2 == 0){
randomHeight += Math.abs(mRecyclerViewHeight)/9;
}
return randomHeight;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
((DiscoveryViewHolder) holder).bindRepository(mPostList.get(position), getHeight(position));
holder).bindRepository(mPostList.get(position));
}
I have a fragment which includes a Textview, and a RecyclerView (list)
My problem is when scrolling the RecyclerView, the textview above it stays on top and doesn't get scrolled.
How do I make the textview to be non-sticky and scrollable like in a ScrollView?
My layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.easyuni.courserecommender.ResultsCareerTabFragment">
<TextView
android:id="#+id/tv_results_rec_careers_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:layout_marginTop="24dp"
android:text="#string/results_rec_careers_title"
android:textSize="24sp"
android:textStyle="bold" />
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_careers"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_results_rec_careers_title"
android:layout_gravity="center"
android:layout_marginTop="20dp"></android.support.v7.widget.RecyclerView>
</RelativeLayout>
Screenshots:
Normal look:
When scrolling down in RecyclerView:
Thanks.
You will need to add your textview as an item that feeds your adapter for your recyclerview. You can use the following code to distinguish between the view that you want to display in your recyclerview:
public int getItemViewType (int position)
Please have a look at this thread:
How to create RecyclerView with multiple view type?
You can fix it with this:
Message message = messageList.get(position);
viewHolder.setIsRecyclable(false);
you can lean on this link:
http://www.homebrewandtechnology.com/blog/recyclevewadaptershowwrongdatawhenscrolling