Membuat ImageSlider dengan Android Studio

Agustus 26, 2017 0 Comments



Pertama Siapkan tiga buah gambar atau lebih, untuk gambar slidernya.

kedua buat ViewPager
<?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="id.giviews.imageslider.MainActivity">

    <android.support.v4.view.ViewPager
       
android:id="@+id/viewPager"
       
android:layout_width="match_parent"
       
android:layout_height="150dp"/>

</RelativeLayout>

ketiga bikin layout baru dengan nama custom_layout, didalamnya tambahkan imageview untuk menampung gambar slidernya nanti.
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical" android:layout_width="match_parent"

    android:layout_height="match_parent">



    <ImageView

        android:id="@+id/imageView"

        android:layout_width="match_parent"

        android:layout_height="match_parent" />



</LinearLayout>

keempat buat class baru viewPagerAdapter di java
package id.giviews.imageslider;



import android.content.Context;

import android.support.v4.view.LayoutInflaterCompat;

import android.support.v4.view.PagerAdapter;

import android.support.v4.view.ViewPager;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.ImageView;



/**

 * Created by asus on 18/07/2017.

 */



public class ViewPagerAdapter extends PagerAdapter{



    private Context context;

    private LayoutInflater layoutInflater;

    private Integer[] images = {R.drawable.slide1, R.drawable.slide2, R.drawable.slide3};



    public ViewPagerAdapter(Context context) {

        this.context = context;

    }



    @Override

    public int getCount() {

        return images.length;

    }



    @Override

    public boolean isViewFromObject(View view, Object object) {

        return view == object;

    }



    @Override

    public Object instantiateItem(ViewGroup container, int position) {

        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View view = layoutInflater.inflate(R.layout.custom_layout, null);

        ImageView imageView = (ImageView) view.findViewById(R.id.imageView);

        imageView.setImageResource(images[position]);



        ViewPager vp = (ViewPager) container;

        vp.addView(view, 0);

        return view;

    }



    @Override

    public void destroyItem(ViewGroup container, int position, Object object) {



        ViewPager vp = (ViewPager) container;

        View view = (View) object;

        vp.removeView(view);

    }

}

kelima ubah script MainActivity seperti berikut
package id.giviews.imageslider;



import android.support.v4.view.ViewPager;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;



import java.util.Timer;

import java.util.TimerTask;



public class MainActivity extends AppCompatActivity {



    ViewPager viewPager;



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        viewPager = (ViewPager) findViewById(R.id.viewPager);

        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(this);

        viewPager.setAdapter(viewPagerAdapter);



        Timer timer = new Timer();

        timer.scheduleAtFixedRate(new MyTimerTask(), 2000, 4000);

    }



    public class MyTimerTask extends TimerTask{

        @Override

        public void run() {

            MainActivity.this.runOnUiThread(new Runnable() {

                @Override

                public void run() {

                    if (viewPager.getCurrentItem() == 0) {

                        viewPager.setCurrentItem(1);

                    }else if (viewPager.getCurrentItem() == 1) {

                        viewPager.setCurrentItem(2);

                    }else {

                        viewPager.setCurrentItem(0);

                    }

                }

            });

        }

    }

}


Selanjutnya jalankan aplikasinya jika slidernya nampil berarti aplikasi anda telah berhasil, jika ada yang error silakan ditanyakan pada kolom komentar dibawah. jika artikel ini bermanfaat silakan share ke teman anda. terimakasih sudah berkunjung ke blog kami.

Juanas Smith Shared

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard.

0 komentar :