如何使用ViewPager

时间:2016-05-28 10:42:41

标签: android android-studio

这里我做了一个简单的滑块,但我想知道如何在我的代码中应用简单的点指示器。另外我想应用如何在我的图像滑块中应用循环过渡..

这是我的代码:

imageslider.java

       import android.support.v7.app.AppCompatActivity;
       import android.os.Bundle;
       import android.app.Activity;
       import android.support.v4.view.PagerAdapter;
       import android.support.v4.view.ViewPager;
       import android.view.Menu;
       import android.view.View;
       import android.widget.AdapterView;

       public class imageslider extends Activity {
      ViewPager viewPager;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_imageslider);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
         PagerAdapter adapter = new SliderImage(imageslider.this);


        viewPager.setAdapter(adapter);


    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }
    }

SliderImage.java

     import android.app.Activity;
     import android.content.Context;
     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;
     import android.widget.LinearLayout;
     import android.widget.TextView;
/**
 * Created by Administrator on 27/05/2016.
 */

          public class SliderImage extends PagerAdapter{
         Context context;

      int[] imageId = {R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5};

        public SliderImage(Context context){
        this.context = context;

      }


       @Override
       public Object instantiateItem(ViewGroup container, int position) {
        // TODO Auto-generated method stub

        LayoutInflater inflater = ((Activity)context).getLayoutInflater();

        View viewItem = inflater.inflate(R.layout.image_item, container, false);
        ImageView imageView = (ImageView) viewItem.findViewById(R.id.imageView);
        imageView.setImageResource(imageId[position]);
        //TextView textView1 = (TextView) viewItem.findViewById(R.id.textView1);
        //textView1.setText("hi");
        ((ViewPager)container).addView(viewItem);

        return viewItem;
      }

      @Override
     public int getCount() {
         // TODO Auto-generated method stub
        return imageId.length;
     }

      @Override
      public boolean isViewFromObject(View view, Object object) {
        // TODO Auto-generated method stub

        return view == ((View)object);
      }


    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // TODO Auto-generated method stub
        ((ViewPager) container).removeView((View) object);
    }



    }

**activity_imageslider.xml**

         <?xml version="1.0" encoding="utf-8"?>
         <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical" >

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

         </LinearLayout>

    **image_item.xml**
          <?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="match_parent"
           android:orientation="vertical"
           >

        <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="300dp"

        />
        </LinearLayout>     

给我简要说明详细信息:

0 个答案:

没有答案