Android 圖片輪播(最簡單的)

來源:互聯網
上載者:User

標籤:

布局檔案

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <FrameLayout        android:layout_width="match_parent"        android:layout_height="300dip" >        <android.support.v4.view.ViewPager            android:id="@+id/vp"            android:layout_width="match_parent"            android:layout_height="match_parent" />        <LinearLayout            android:layout_width="match_parent"            android:layout_height="35dip"            android:layout_gravity="bottom"            android:background="#33000000"            android:gravity="center"            android:orientation="vertical" >            <TextView                android:id="@+id/title"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="圖片標題"                android:textColor="@android:color/white" />            <LinearLayout                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_marginTop="3dip"                android:orientation="horizontal" >                <View                    android:id="@+id/dot_0"                    android:layout_width="5dip"                    android:layout_height="5dip"                    android:layout_marginLeft="2dip"                    android:layout_marginRight="2dip"                    android:background="@drawable/dot_yes"/>                <View                    android:id="@+id/dot_1"                    android:layout_width="5dip"                    android:layout_height="5dip"                    android:layout_marginLeft="2dip"                    android:layout_marginRight="2dip"                    android:background="@drawable/dot_no"/>                <View                    android:id="@+id/dot_2"                    android:layout_width="5dip"                    android:layout_height="5dip"                    android:layout_marginLeft="2dip"                    android:layout_marginRight="2dip"                    android:background="@drawable/dot_no"/>                <View                    android:id="@+id/dot_3"                    android:layout_width="5dip"                    android:layout_height="5dip"                    android:layout_marginLeft="2dip"                    android:layout_marginRight="2dip"                    android:background="@drawable/dot_no"/>                <View                    android:id="@+id/dot_4"                    android:layout_width="5dip"                    android:layout_height="5dip"                    android:layout_marginLeft="2dip"                    android:layout_marginRight="2dip"                    android:background="@drawable/dot_no"/>            </LinearLayout>        </LinearLayout>    </FrameLayout></RelativeLayout>

在一個fragment中使用

package com.example.administrator.yunphone.View;import android.app.Fragment;import android.os.Bundle;import android.os.Handler;import android.support.annotation.Nullable;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.TextView;import com.example.administrator.yunphone.R;import java.util.ArrayList;import java.util.List;import java.util.concurrent.Executors;import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.TimeUnit;/** * Created by Administrator on 2016/7/13. */public class HomeFragment extends Fragment {    private View mView;    private ViewPager mViewPaper;    private List<ImageView> images;    private List<View> dots;    private int currentItem;    //記錄上一次點的位置    private int oldPosition = 0;    //存放圖片的id    private int[] imageIds = new int[]{            R.drawable.home_bg,            R.drawable.home_bg,            R.drawable.home_bg,            R.drawable.home_bg,            R.drawable.home_bg    };    //存放圖片的標題    private String[] titles = new String[]{            "輪播1",            "輪播2",            "輪播3",            "輪播4",            "輪播5"    };    private TextView title;    private ViewPagerAdapter adapter;    private ScheduledExecutorService scheduledExecutorService;    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {        mView=inflater.inflate(R.layout.fragment_home_layout, null);        setView();        return mView;    }    private void setView(){        mViewPaper = (ViewPager)mView.findViewById(R.id.vp);        //顯示的圖片        images = new ArrayList<ImageView>();        for(int i = 0; i < imageIds.length; i++){            ImageView imageView = new ImageView(getActivity());            imageView.setBackgroundResource(imageIds[i]);            images.add(imageView);        }        //顯示的小點        dots = new ArrayList<View>();        dots.add(mView.findViewById(R.id.dot_0));        dots.add(mView.findViewById(R.id.dot_1));        dots.add(mView.findViewById(R.id.dot_2));        dots.add(mView.findViewById(R.id.dot_3));        dots.add(mView.findViewById(R.id.dot_4));        title = (TextView) mView.findViewById(R.id.title);        title.setText(titles[0]);        adapter = new ViewPagerAdapter();        mViewPaper.setAdapter(adapter);        mViewPaper.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {            @Override            public void onPageSelected(int position) {                title.setText(titles[position]);                dots.get(position).setBackgroundResource(R.drawable.dot_yes);                dots.get(oldPosition).setBackgroundResource(R.drawable.dot_no);                oldPosition = position;                currentItem = position;            }            @Override            public void onPageScrolled(int arg0, float arg1, int arg2) {            }            @Override            public void onPageScrollStateChanged(int arg0) {            }        });    }    /*定義的適配器*/    public class ViewPagerAdapter extends PagerAdapter{        @Override        public int getCount() {            return images.size();        }        @Override        public boolean isViewFromObject(View arg0, Object arg1) {            return arg0 == arg1;        }        @Override        public void destroyItem(ViewGroup view, int position, Object object) {            // TODO Auto-generated method stub//          super.destroyItem(container, position, object);//          view.removeView(view.getChildAt(position));//          view.removeViewAt(position);            view.removeView(images.get(position));        }        @Override        public Object instantiateItem(ViewGroup view, int position) {            // TODO Auto-generated method stub            view.addView(images.get(position));            return images.get(position);        }    }    /**     * 利用線程池定時執行動畫輪播     */    @Override    public void onStart() {        // TODO Auto-generated method stub        super.onStart();        scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();        scheduledExecutorService.scheduleWithFixedDelay(                new ViewPageTask(),                2,                2,                TimeUnit.SECONDS);    }    /**     * 圖片輪播任務     * @author liuyazhuang     *     */    private class ViewPageTask implements Runnable{        @Override        public void run() {            currentItem = (currentItem + 1) % imageIds.length;            mHandler.sendEmptyMessage(0);        }    }    /**     * 接收子線程傳遞過來的資料     */    private Handler mHandler = new Handler(){        public void handleMessage(android.os.Message msg) {            mViewPaper.setCurrentItem(currentItem);        };    };    @Override    public void onStop() {        // TODO Auto-generated method stub        super.onStop();        if(scheduledExecutorService != null){            scheduledExecutorService.shutdown();            scheduledExecutorService = null;        }    }}

 

Android 圖片輪播(最簡單的)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.