big space between imagesview ( RecyclerView ) - android

i'm trying to create an wallpaper application but i have this problem with recyclerView There is a big space between photos qnd spaced apart like this
photo
and i don't why Appears in this format Width larger than Height
java :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
List<String> movieList = new ArrayList<>();
movieList.add("https://lh3.ggpht.com/-yKfQjOKJq9HcsOqzdCfpP-POQgEtJb43RZAQjEn10XiOEgM_CBgndV7stWe67wXiCq5=h900");
movieList.add("http://www.botherland.com/wp-content/uploads/2017/04/Samsung-Galaxy-S4-Black-Edition-Wallpaper-16.jpg");
movieList.add("http://www.botherland.com/wp-content/uploads/2017/04/Samsung-Galaxy-S4-Black-Edition-Wallpaper-5.jpg");
movieList.add("http://www.androidhive.info/wp-content/uploads/2016/01/android-recycler-view-example.png");
movieList.add("https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcTygBxBtnlvqcQR5GmFCvD1rvPUJJeHLm6lH2zckeYQ6XScoutwN0NgaG0");
movieList.add("http://www.botherland.com/wp-content/uploads/2017/04/Samsung-Galaxy-S4-Black-Edition-Wallpaper-14.jpg");
View v = inflater.inflate(R.layout.fragment_art, container, false);
recyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);
mAdapter = new MoviesAdapter(movieList,getContext());
final StaggeredGridLayoutManager stg= new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(stg);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
return v;
}
public class MoviesAdapter extends RecyclerView.Adapter<MoviesAdapter.MyViewHolder> {
private List<String> moviesList;
class MyViewHolder extends RecyclerView.ViewHolder {
public ImageView pic ;
public MyViewHolder(View view) {
super(view);
pic = (ImageView)view.findViewById(R.id.imageView);
}
}
public Context context;
int screenWidth,screenHeight;
MoviesAdapter(List<String> moviesList, Context context) {
this.moviesList = moviesList;
this.context=context;
WindowManager wm = (WindowManager) getActivity().getSystemService(getActivity().WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
screenWidth = size.x;
screenHeight = size.y;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.picture_item, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
String pic = moviesList.get(position);
Glide.with(context).load(pic).placeholder(R.drawable.j).override(screenWidth/2,screenHeight/4).centerCrop().into(holder.pic);
}
#Override
public int getItemCount() {
return moviesList.size();
}
}
xml :
<LinearLayout 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"
android:orientation="vertical"
android:background="#fff"
tools:context="maa.a4kandhdwallpapers.Art">
<!-- TODO: Update blank fragment layout -->
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recycler_view"/>
</LinearLayout>
picture_item.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/imageView" />
</LinearLayout>

Change your picture_item.xml as:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/imageView" />
</LinearLayout>
But if your image height is large, you will have same problem. I suggest you to fix the height if that does not create problem.

Related

Horizontal recyclerview into vertical recyclerview at specific position

I want to insert/show horizontal recyclerview into specific position (at this example position:1)
Vertical recyclerview - displays TextView + shape (rectange)
Horizontal recyclerview - displays ImageViews (next to each other)
.XML's
horizontal_layout.xml
<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:id="#+id/horizontal_recyclerview"
android:layout_width="368dp"
android:layout_height="wrap_content"
android:layout_marginBottom="60dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="2dp"
android:padding="0dp"/>
<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:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mkalejs.training.MainActivity">
vertical_layout.xml
<android.support.v7.widget.RecyclerView
android:id="#+id/vertical_recyclerview"
android:layout_width="368dp"
android:layout_height="wrap_content"
android:layout_marginBottom="60dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="2dp"
android:padding="0dp"/>
</android.support.constraint.ConstraintLayout>
vertical_layout_item.xml
<EditText
android:id="#+id/lbl_url_text"
android:layout_width="100dp"
android:layout_height="50dp"
android:background="#android:color/transparent"
android:inputType="textPersonName"
android:padding="5dp"
android:layout_gravity="center"
android:text="Name" />
<View
android:id="#+id/rectangle"
android:layout_width="195dp"
android:layout_height="100dp"
android:background="#drawable/rectangle" />
horizontal_layout_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:visibility="visible"
android:weightSum="1">
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/img_horizontal_slider"
android:layout_width="33dp"
android:layout_height="33dp"
android:adjustViewBounds="true"
android:visibility="visible" />
</LinearLayout>
There might be a small misunderstanding in recyclerview concept. I'm not sure where I can call horizontal_recyclerview" to not return it as a null (as I am now) and populate it with data. Is the XML structure wrong? Feel free to update code parts.
THIS SHOULD BE OKAY
MainActivity.class
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.vertical_layout);
ArrayList<PictureData> bundleOfData = prepareData();
RecyclerView rv = (RecyclerView) findViewById(R.id.vertical_recyclerview);
VerticalAdapter adapter = new VerticalAdapter(bundleOfData, this);
rv.setAdapter(adapter);
//rv.setNestedScrollingEnabled(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
rv.setLayoutManager(layoutManager);
}
VerticalAdapter.class
public class VerticalAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private ArrayList<PictureData> pictures;
Context context;
public VerticalAdapter(ArrayList<PictureData> pictures, Context context)
{
this.pictures = pictures;
this.context = context;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView;
switch (viewType) {
case 1:
itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.horizontal_layout_item, parent, false);
return new HorizontalViewHolder(itemView);
default:
itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.vertical_layout_item, parent, false);
return new VerticalViewHolder(itemView);
}
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
switch (holder.getItemViewType()){
case 1:
HorizontalViewHolder hVh = (HorizontalViewHolder) holder;
HorizontalAdapter horizontal_adapter = new HorizontalAdapter(pictures, context);
hVh.horizontal_rec.setAdapter(horizontal_adapter);
break;
default:
VerticalViewHolder vVh = (VerticalViewHolder) holder;
vVh.textView.setText(pictures.get(position).getPictureUrl());
break;
}
}
#Override
public int getItemCount() {return pictures.size();}
#Override
public int getItemViewType(int position) {
if(position == 1){
return 1;
}else{
return 0;
}
}
}
HorizontalViewHolder.class
I GET A ERROR HERE. OBVIOUSLY, I DON'T HAVE (img_horizontal_slider) IN HORIZONTAL_LAYOUT, but putting ImageView + RecyclerView in one layout file and call here would feel wrong (?)
public class HorizontalViewHolder extends RecyclerView.ViewHolder{
public ImageView imagePositionInLayout;
public RecyclerView horizontal_rec;
public HorizontalViewHolder(View itemView) {
super(itemView);
LinearLayoutManager layoutManager = new LinearLayoutManager(itemView.getContext(), LinearLayoutManager.HORIZONTAL, false);
horizontal_rec = itemView.findViewById(R.id.vertical_recyclerview);
imagePositionInLayout = itemView.findViewById(R.id.img_horizontal_slider);
horizontal_rec.setLayoutManager(layoutManager);
}
public ImageView getView() {
return imagePositionInLayout;
}
}
HorizontalAdapter.class
ublic class HorizontalAdapter extends RecyclerView.Adapter<HorizontalViewHolder> {
private ArrayList<PictureData> pictures;
Context context;
public HorizontalAdapter(ArrayList<PictureData> pictures, Context context) {
this.pictures = pictures;
this.context = context;
}
#Override
public HorizontalViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.horizontal_layout_item, parent, false);
return new HorizontalViewHolder(view);
}
#Override
public void onBindViewHolder(HorizontalViewHolder holder, int position)
{
Picasso.Builder builder = new Picasso.Builder(context);
Picasso picasso = builder.build();
picasso
.load("http://nuclearpixel.com/content/icons/2010-02-09_stellar_icons_from_space_from_2005/earth_128.png")
.placeholder(R.mipmap.ic_launcher_round)
.into(holder.imagePositionInLayout);
}
#Override
public int getItemCount() {
return pictures.size();
}
}
I'm new to Android so I hope people will give some good explanation on what is wrong and how to fix it. Every suggestion will be gladly accepted. I will highly appreciate if someone will actually look into this and explain, because I feel pretty lost right now.

Why would a RecyclerView not show on the device but be visible in Layout Inspector?

I want to display a slide with pictures on my app and I implemented a little horizontal RecyclerView.
The screen on the left is a runtime screenshot from the same screen on the right which is shown in Layout Inspector, after I added a photo to the recyclerview
The RecyclerView's original place is inside the CardView and I moved it out because it didn't show there either. Any ideas of why?
Some code:
activity.xml
<FrameLayout
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"
tools:context=".TheActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="TheFragment"
android:tag="#string/the_tag"
tools:layout="#layout/the_layout"
android:id="#+id/the_id"/>
</FrameLayout>
fragment.xml
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:bind="http://schemas.android.com/apk/res-auto"
tools:context="TheActivity" >
<data>
<!-- Some vars -->
</data>
<LinearLayout android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView android:visibility="visible"
android:id="#+id/frag_add_property_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView android:id="#+id/the_rv"
android:layout_width="match_parent"
android:layout_height="150dp"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
android:orientation="horizontal"
android:layout_marginBottom="#dimen/activity_half_vertical_margin"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
public class TheFragment extends BaseFragment implements TheView {
private PictureFilesAdapter adapter;
#BindView(R.id.rv_add_property_pics)
RecyclerView imageRv;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
bind = DataBindingUtil.inflate(inflater, getLayout(), container, false);
unbinder = ButterKnife.bind(this, bind.getRoot());
Drawable dividerDrawable = ContextCompat.getDrawable(getActivity(), R.drawable.rv_item_hor_divider);
imageRv.addItemDecoration(new CustomItemDecoration(dividerDrawable));
return bind.getRoot();
}
class PictureFilesAdapter extends RecyclerView.Adapter<PropertyPicturesViewHolder> {
Context context;
public ArrayList<File> files;
public ArrayList<File> getFiles() {
return files;
}
AddPropertyView view;
PictureFilesAdapter(AddPropertyView view, Context context) {
this.context = context;
this.files = new ArrayList<>();
this.view = view;
}
#Override
public PropertyPicturesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.item_rv_add_property, parent, false);
return new AddPropertyFragment.PropertyPicturesViewHolder(v);
}
#Override
public void onBindViewHolder(PropertyPicturesViewHolder holder, int position) {
holder.textView.setText(files.get(position).getAbsolutePath());
Picasso.with(context)
.load(files.get(position))
.into(holder.imageView);
}
#Override
public int getItemCount() {
return files.size();
}
void addFile(File file) {
this.files.add(file);
notifyDataSetChanged();
}
}
class PropertyPicturesViewHolder extends RecyclerView.ViewHolder {
private Uri uri;
#BindView(R.id.item_rv_add_prop_image) ImageView imageView;
#BindView(R.id.item_rv_add_prop_image_src) TextView textView;
PropertyPicturesViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
view.setOnClickListener((v) -> {
// some logic
});
}
}
}
Turns out the bitmap was too large even for Picasso. Downscaling the bitmap prior to showing it did the magic.
Can you provide your code?
There are several possibilities why it isn't showing up.
It has no layout manager
It has no adapter
The adapter.getCount() return 0

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"
/>

Only one item displaying in recycler view

I am having problems displaying all the items in my recyclerview. My code only shows one item (Home. I have 3 items: Home, Browse Photos and My Travels)
fragment_navigation_drawer.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragment_navigation_drawer"
android:background="#EEE"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.traveldiaries.ztir.traveldiaries.NavigationDrawerFragment">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="220dp"
android:background="#16a085"
android:id="#+id/linearLayout">
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/drawerList"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
RecyclerAdapter.java
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
private LayoutInflater inflater;
List<MenuList> data = Collections.emptyList();
public RecyclerAdapter(Context context, List<MenuList> data){
inflater= LayoutInflater.from(context);
this.data = data;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=inflater.inflate(R.layout.itemlist,parent,false);
MyViewHolder holder= new MyViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
MenuList current=data.get(position);
holder.title.setText(current.title);
holder.image.setImageResource(current.icon);
}
#Override
public int getItemCount() {
return data.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView title;
ImageView image;
public MyViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.item_list);
image = (ImageView) itemView.findViewById(R.id.icons);
}
}
}
NavigationDrawerFragment.java (posted code only related to recyclerview)
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View layout = inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
recyclerView = (RecyclerView) layout.findViewById(R.id.drawerList);
adapter = new RecyclerAdapter(getActivity(),getData());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return layout;
}
public static List<MenuList> getData(){
List<MenuList> data = new ArrayList<>();
String[] titles={"Home","My Travels","Browse Photos"};
int[] icons ={R.drawable.home,R.drawable.sunglasses,R.drawable.tooltip_image};
for(int i=0;i<3;i++){
MenuList current = new MenuList();
current.title=titles[i];
current.icon=icons[i];
Log.d("Data",titles[i]);
Log.d("Data",""+icons[i]);
data.add(current);
}
return data;
}
itemlist.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:padding="8dp"
android:id="#+id/icons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_menu_camera"
android:layout_gravity="center_vertical"
/>
<TextView
android:id="#+id/item_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dummy Text"
android:layout_gravity="center_vertical"
android:padding="8dp"
/>
</LinearLayout>
What I was able to do is to check all the passed data (all seems to be fine). Also tried to change width to wrap content (fragment_navigation_drawer.xml) in both my relative and linear layout but I just lose my background color instead and still only one item appear.
Change height of LinearLayout to wrap_content in itemlist.xml
android:layout_height="wrap_content"
Just change the height of your itemlist from "match_parent" to "wrap_content".

Using Fresco with StaggeredGridLayoutManager cause moving items

Here is the issues on github
I want to make a gallery with using Fresco and recyclerview.
But when I notifyDataSetChange() and then scroll to the top of the list that I can see the items are moving. Like this.
What I have tried:
I have tried to solve it by using recyclerview.setItemAnimator(null) and holder.pic.setLayoutParams(). None of them have worked.
I found an issue which is similar to mine, but I still have no idea of the problem.
Here is my key code:
MyAdapter
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder> {
private List<ImageModel> mItems;
Context mContext;
private WindowManager windowManager;
private int itemWidth;
public MyRecyclerAdapter(Context context,List<ImageModel> objects,int gap) {
mContext = context;
mItems = objects;
windowManager=(WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
itemWidth=(windowManager.getDefaultDisplay().getWidth()-gaps*2)/2
}
static class ViewHolder extends RecyclerView.ViewHolder{
private ImageView picture;
private TextView mTextView;
private View rootView;
public ViewHolder(View itemView) {
super(itemView);
rootView = itemView;
picture =(ImageView)itemView.findViewById(R.id.image);
mTextView =(TextView)itemView.findViewById(R.id.title);
}
}
#Override
public int getItemCount() {
return mItems.size();
}
#Override
public void onBindViewHolder(ViewHolder holder, int position,Picture picture) {
holder.mTextView.setText(picture.getTitle());
ViewGroup.LayoutParams params = (ViewGroup.LayoutParams)holder.picture.getLayoutParams();
float ratio = picture.getHeight()/picture.getWidth();
int width=itemWidth;
int height=(int)(width * ratio);
params.height = height;
holder.picture.setLayoutParams(params);
ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(picture.getPath()))
.setResizeOptions(new ResizeOptions(width, height))
.build();
PipelineDraweeController controller = Fresco.newDraweeControllerBuilder()
.setOldController(holder.picture.getController())
.setImageRequest(request)
.build();
holder.picture.setController(controller);
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int arg1) {
LayoutInflater inflater =
(LayoutInflater) mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View convertView = inflater.inflate(R.layout.item, parent, false);
return new ViewHolder(convertView);
}
}
My item layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:id="#+id/root_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.facebook.drawee.view.SimpleDraweeView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
fresco:actualImageScaleType="fitXY"
fresco:placeholderImage="#mipmap/default_image"
fresco:actualImageScaleType="fitXY"
fresco:roundAsCircle="false"
fresco:roundedCornerRadius="1dp"
fresco:roundTopLeft="true"
fresco:roundTopRight="false"
fresco:roundBottomLeft="false"
fresco:roundBottomRight="true"
fresco:roundingBorderWidth="2dp"
/>
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="28dp"
android:textSize="14dp"
android:textColor="#ffffff"
android:layout_below="#+id/fresco_image"
android:gravity="center"
android:background="#ffffff"/>
</RelativeLayout>
</RelativeLayout>
The above is inflated into the following:
<android.support.v7.widget.RecyclerView
android:id="#+id/view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none">
</RelativeLayout>
I want to get help:
Why does the item of recyclerview always move when I scroll to the top or am loading more?
How to solve this problem? Can you show me the keycode or sample here?

Categories

Resources