How Can I Add Slide Images With Viewpager

35 views Asked by At

I am trying my best to find a way how can I slide images. I have imported images from draw-able now I want to slide to see the next image, here is my MainActivity:

public class MainActivity extends AppCompatActivity {

List<imagefull> lstBook;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    lstBook = new ArrayList<>();
    lstBook.add(new imagefull(R.drawable.thevigitarian));
    lstBook.add(new imagefull(R.drawable.thewildrobot));
    lstBook.add(new imagefull(R.drawable.mariasemples));
    lstBook.add(new imagefull(R.drawable.themartian));
    lstBook.add(new imagefull(R.drawable.hediedwith));
    lstBook.add(new imagefull(R.drawable.thevigitarian));
    lstBook.add(new imagefull(R.drawable.thewildrobot));
    lstBook.add(new imagefull(R.drawable.mariasemples));
    lstBook.add(new imagefull(R.drawable.themartian));
    lstBook.add(new imagefull(R.drawable.hediedwith));

    RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
    RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(this,lstBook);
    recyclerView.setLayoutManager(new GridLayoutManager(this,3));
    recyclerView.setAdapter(recyclerViewAdapter);
 }
}

And here is my activitymain xml file

<LinearLayout 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"
android:padding="8dp">

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

And Here is my RecyclerViewAdapter

public class RecyclerViewAdapter extends RecyclerView.Adapter { private Context mContext ; private List mData ;

public RecyclerViewAdapter(Context mContext, List<imagefull> mData) {
    this.mContext = mContext;
    this.mData = mData;
}


@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View view;
    LayoutInflater minflater =LayoutInflater.from(mContext);
    view = minflater.inflate(R.layout.card_view, parent, false);
    return new MyViewHolder(view);
}

@Override
public void onBindViewHolder( MyViewHolder Holder, final int position) {
    Holder.imageView.setImageResource(mData.get(position).getThumbnail());
    Holder.cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String action;
            Intent intent = new Intent(mContext,FullScreenImagView.class);
            intent.putExtra("Thumbnail",mData.get(position).getThumbnail());
            mContext.startActivity(intent);
        }
    });
}

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

public static class  MyViewHolder extends RecyclerView.ViewHolder
{
    ImageView imageView;
    CardView cardView ;

    public MyViewHolder(View itemView) {
        super(itemView);

        imageView = (ImageView) itemView.findViewById(R.id.img_view);
        cardView = (CardView)itemView.findViewById(R.id.cardview);
    }
 }
}

here is my Fullscreenimageview Activity where is how image in full size when user click

public class FullScreenImagView extends AppCompatActivity {

ImageView full_img_view;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_full_screen_imag_view);

    full_img_view = findViewById(R.id.full_img_view);

    Intent intent = getIntent();
    int image = intent.getExtras().getInt("Thumbnail");
    full_img_view.setImageResource(image);

 }
}

your help will be appreciated gyes i juist want to slide images thx

1

There are 1 answers

1
MEHEDI HASAN On

you can try this when setting layout for the recyclerview

RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recycler_view);

recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL,false));

RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(this,lstBook);

recyclerView.setAdapter(recyclerViewAdapter);

in your item_view.xml (where the layout of image is) you should set

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

  <ImageView
     android:id="@+id/item_image"
     android:layout_width="match_parent"
     android:layout_height="match_parent"/>

</LinearLayout>

anyway be sure to adjust ImageView height and width according to your need. I did not built this but i think i will work.