无限轮播

来源:互联网 发布:web动画创建软件 编辑:程序博客网 时间:2024/05/19 13:44

首先要有两个


<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">    <solid android:color="#f00"/>    <corners android:radius="8dp"/></shape>

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">    <solid android:color="#88000000"/>    <corners android:radius="8dp"/></shape>


布局

<?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"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.bawei.lian02.MainActivity">    <android.support.v4.view.ViewPager        android:layout_width="match_parent"        android:layout_height="match_parent"        android:id="@+id/vp"></android.support.v4.view.ViewPager>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="40dp"        android:layout_alignParentBottom="true"        android:gravity="center"        android:id="@+id/ll"        android:orientation="horizontal"></LinearLayout></RelativeLayout>
图片的布局
<?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">    <ImageView        android:id="@+id/iv"        android:layout_width="match_parent"        android:layout_height="match_parent" /></LinearLayout>
小圆点的布局
<?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">    <View        android:layout_width="8dp"        android:layout_height="8dp"        android:layout_margin="3dp"        android:id="@+id/dot"        android:background="@drawable/dot_normal"        /></LinearLayout>

主方法
懒得分包全写一起啦
public class MainActivity extends AppCompatActivity {    private static final String str="http://v.juhe.cn/toutiao/index?type=top&key=dbedecbcd1899c9785b95cc2d17131c5";    private ViewPager vp;    private List<Datt> data;    private ArrayList<View> doList=new ArrayList<View>();    private LinearLayout ll;    private int current=0;    private int old_dot=0;    private int i;    private Handler handler=new Handler(){        @Override        public void handleMessage(Message msg) {            super.handleMessage(msg);            vp.setCurrentItem(msg.what);            doList.get(old_dot).setBackgroundResource(R.drawable.dot_normal);            doList.get(msg.what%doList.size()).setBackgroundResource(R.drawable.dot_focused);            old_dot  = msg.what % doList.size();        }    };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        vp = (ViewPager) findViewById(R.id.vp);        ll= (LinearLayout) findViewById(R.id.ll);        new AsyncTask<String,Integer,String>(){            @Override            protected String doInBackground(String... params) {                String url=params[0];                URL ur=null;                HttpURLConnection httpURLConnection=null;                String stt="";                InputStream inputStream=null;                try {                    ur=new URL(url);                    httpURLConnection = (HttpURLConnection) ur.openConnection();                    int responseCode = httpURLConnection.getResponseCode();                    if(responseCode==200){                        inputStream = httpURLConnection.getInputStream();                        byte[] b=new byte[1024];                        int len=0;                        while((len=inputStream.read(b))!=-1){                            stt+=new String(b,0,len);                        }                        inputStream.close();                    }                } catch (Exception e) {                    e.printStackTrace();                }                return stt;            }            @Override            protected void onPostExecute(String s) {                super.onPostExecute(s);                Bean bean = new Gson().fromJson(s, Bean.class);                Result result = bean.getResult();                data = result.getData();                Pageradapter pageradapter = new Pageradapter(MainActivity.this, data);                vp.setAdapter(pageradapter);                vp.setCurrentItem(data.size()*1000);                for(int j=0;j<data.size();j++){                    View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item, null);                    View dot = view.findViewById(R.id.dot);                    doList.add(dot);                    ll.addView(view);                }                doList.get(0).setBackgroundResource(R.drawable.dot_focused);                i=vp.getCurrentItem();                new Thread(){                    @Override                    public void run() {                        while (true){                            try {                                sleep(5000);                                Message obtain = Message.obtain();                                obtain.what=i++;                                handler.sendMessage(obtain);                            } catch (Exception e) {                                e.printStackTrace();                            }                        }                    }                }.start();            }        }.execute(str);    }}
适配器
 
public class Pageradapter extends PagerAdapter {    private Context context;    private List<Datt> list;    public Pageradapter(Context context, List<Datt> list) {        this.context = context;        this.list = list;    }    @Override    public int getCount() {        return Integer.MAX_VALUE;    }    @Override    public boolean isViewFromObject(View view, Object object) {        return view==object;    }    @Override    public Object instantiateItem(ViewGroup container, int position) {        ImageView imageView = new ImageView(context);        ImageLoader.getInstance().displayImage(list.get(position%list.size()).getThumbnail_pic_s(),imageView);        container.addView(imageView);        return imageView;    }    @Override    public void destroyItem(ViewGroup container, int position, Object object) {        container.removeView((View)object);    }}Application显示图片懒得缓存
public class Myapp extends Application {    @Override    public void onCreate() {        super.onCreate();        ImageLoaderConfiguration aDefault = ImageLoaderConfiguration.createDefault(this);        ImageLoader.getInstance().init(aDefault);    }}bean类我就不写啦





原创粉丝点击