Azoft / CarouselLayoutManager

Android Carousel LayoutManager for RecyclerView
Apache License 2.0
2.56k stars 367 forks source link

Add 10 images in adapter using this library #40

Closed manpreetsinghsodhi closed 6 years ago

manpreetsinghsodhi commented 7 years ago

Hi Author,

I have downloaded 10 images from internet and i need to use this carosel for them. How to do so?

Alsoi need to open new activity on clicking any item of the carosel. Please help me in this.

Thanks

Kisty commented 7 years ago

Hi! I'm not the author of this project but I may be able to help.

This library uses a RecyclerView to show a list of items. You will have to make a RecyclerView.Adapter to tell the RecyclerView what you want to display. Have you made an app with a simple list using RecyclerView before? If not I'd suggest you do a simple text list of something, say countries. Check out this page to get started. https://developer.android.com/training/material/lists-cards.html#RecyclerView

As for starting a new activity, you would set an OnClickListener on the item with a line like the following:

startActivity(MyFirstActivity.this, new Intent(this, MySecondActivity.class); 

How new are you to Android development? If you're completely new, I'd suggest that you go through some tutorials first. Walk before you can run. It much easier to get the basics done first, then to try and build upon the that.

I'd suggest the Google Android Udacity course. It's free to watch the videos but to have a certificate, you pay for a plan. Would highly recommend it. https://www.udacity.com/course/new-android-fundamentals--ud851

Hope this helps!

manpreetsinghsodhi commented 7 years ago

Hi Kisty, its not working for me. My recycler view is working fine as a gallery in vertical direction. But when i include the code of this library for my recycler view(to get carousel view) , it is displaying nothing, not even my gallery list of images.

Here is my code

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.azoft.carousellayoutmanager.CarouselZoomPostLayoutListener;
import android.support.v7.widget.RecyclerView;

import com.azoft.carousellayoutmanager.CarouselLayoutManager;
import com.azoft.carousellayoutmanager.CenterScrollListener;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private String TAG = MainActivity.class.getSimpleName();
    private ArrayList<Integer> images;
    private GalleryAdapter mAdapter;
    private RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        images = new ArrayList<>();
        final CarouselLayoutManager layoutManager = new CarouselLayoutManager(CarouselLayoutManager.HORIZONTAL, true);
        layoutManager.setPostLayoutListener(new CarouselZoomPostLayoutListener());
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        mAdapter = new GalleryAdapter(getApplicationContext(), images);

        recyclerView.setAdapter(mAdapter);

        recyclerView.addOnScrollListener(new CenterScrollListener());
        images.add(R.drawable.bourne);
        images.add(R.drawable.bvs);
        images.add(R.drawable.cacw);
        images.add(R.drawable.deadpool);
        images.add(R.drawable.doctor);
        images.add(R.drawable.hunger);
        images.add(R.drawable.xmen);

    }

}

Layout file is as follows :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context="com.example.manpsing.pepperbot.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical" />
</RelativeLayout>

And Gallery Adapter is as follows

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import java.util.List;

public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.MyViewHolder> {

    private List<Integer> images;
    private Context mContext;

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public ImageView thumbnail;

        public MyViewHolder(View view) {
            super(view);
            thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
        }
    }

    public GalleryAdapter(Context context, List<Integer> images) {
        mContext = context;
        this.images = images;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.gallery_thumbnail, parent, false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Integer image = images.get(position);

        Glide.with(mContext).load(images.get(position))
                .thumbnail(0.5f)
                .crossFade()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(holder.thumbnail);
    }

    @Override
    public int getItemCount() {
        return images.size();
    }

    public interface ClickListener {
        void onClick(View view, int position);

        void onLongClick(View view, int position);
    }

    public static class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {

        private GestureDetector gestureDetector;
        private GalleryAdapter.ClickListener clickListener;

        public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final GalleryAdapter.ClickListener clickListener) {
            this.clickListener = clickListener;
            gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
                @Override
                public boolean onSingleTapUp(MotionEvent e) {
                    return true;
                }

                @Override
                public void onLongPress(MotionEvent e) {
                    View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
                    if (child != null && clickListener != null) {
                        clickListener.onLongClick(child, recyclerView.getChildPosition(child));
                    }
                }
            });
        }

        @Override
        public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {

            View child = rv.findChildViewUnder(e.getX(), e.getY());
            if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
                clickListener.onClick(child, rv.getChildPosition(child));
            }
            return false;
        }

        @Override
        public void onTouchEvent(RecyclerView rv, MotionEvent e) {
        }

        @Override
        public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

        }
    }
}

Gallery_thumbnail.xml

<?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:focusable="true"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:clickable="true"
    android:background="?android:attr/selectableItemBackground"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/thumbnail"

        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>

As you can see Recycler code is fine but as soon as i use CarouselLayoutManager, nothing is displayed i.e blank screen . Please resolve issue for me.

Kisty commented 7 years ago

Hmm, thanks for the code. I think if you add the images to the ArrayList before you create the the adapter, you might see it correctly.

Hope this helps.

manpreetsinghsodhi commented 7 years ago

Tried this. Still not working for me.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        images = new ArrayList<>();
        final CarouselLayoutManager layoutManager = new    CarouselLayoutManager(CarouselLayoutManager.HORIZONTAL, true);
        layoutManager.setPostLayoutListener(new CarouselZoomPostLayoutListener());
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        images.add(R.drawable.bourne);
        images.add(R.drawable.bvs);
        images.add(R.drawable.cacw);
        images.add(R.drawable.deadpool);
        images.add(R.drawable.doctor);
        images.add(R.drawable.hunger);
        images.add(R.drawable.xmen);
        mAdapter = new GalleryAdapter(getApplicationContext(), images);
       recyclerView.setAdapter(mAdapter);
       recyclerView.addOnScrollListener(new CenterScrollListener());
  }
Kisty commented 7 years ago

Hmm, try match_parent on ImageView height?

manpreetsinghsodhi commented 7 years ago

Tried. Still not working. Any other idea how can i fix this?

Actually this is really very very strange .I replaced RecyclerView.LayoutManager with CarouselLayoutManager and my list vanished. I guess something is missing in documentation of this library.

Kisty commented 7 years ago

Try downloading the images, then putting the files in the res/drawable-nodpi/ and load the images without Glide just to rule out the problem isn't with Glide.

manpreetsinghsodhi commented 7 years ago

I loaded images without glide. Still not working !!!

holder.thumbnail.setBackgroundResource(images.get(position));

These are images in res/drawable. Point is everything works well with normal LinearLayoutManager but on replacing it with CarouselLayoutManager , nothing works.

manpreetsinghsodhi commented 7 years ago

Finally fixed it .Changed my MainActivity xml to something like this

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context="com.example.manpsing.pepperbot.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:layout_centerInParent="true" />
</RelativeLayout>

Thanks Kisty for the support. C u soon on other threads mate. Cheers !!!

Kisty commented 7 years ago

Hey! Glad you've got it working :).

ram992 commented 7 years ago

Yes there is this issue with this library, I don't know whether it is a bug, but when ever I use variables like wrap_content or match_parent it will not inflate. But will as soon as there is a fixed height to recyclerViews.

isarYMB commented 3 years ago

Yes there is this issue with this library, I don't know whether it is a bug, but when ever I use variables like wrap_content or match_parent it will not inflate. But will as soon as there is a fixed height to recyclerViews.

Can i see your source code?