码迷,mamicode.com
首页 > 其他好文 > 详细

studio无限轮播

时间:2017-08-19 10:47:14      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:www.   apk   ant   puts   cut   pager   pixel   state   override   

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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="com.example.b_week3.MainActivity">
    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:id="@+id/pager"></android.support.v4.view.ViewPager>
    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/ll"
    android:orientation="vertical">

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/rg"
        android:orientation="horizontal"
        android:gravity="center"
        android:layout_marginTop="50dp">
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/rb1"
            android:checked="true"/>

        <RadioButton
            android:id="@+id/rb2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/rb3"/>
    </RadioGroup>
</LinearLayout>
    <com.limxing.xlistview.view.XListView
        android:layout_below="@+id/pager"
        android:id="@+id/xlv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></com.limxing.xlistview.view.XListView>


</RelativeLayout>
public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener{
    private boolean flag;
    private ViewPager pager;
    private RadioGroup rg;
    private int con=0;
    private int page=1;
    private List<String > imagelist=new ArrayList<String>();
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            int what=msg.what;
            pager.setCurrentItem(what);
        }
    };
    private XListView xlv;
    private List<Bean.ResultBean.DataBean> list;
    private MyBaseadapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pager = (ViewPager) findViewById(R.id.pager);
        rg = (RadioGroup) findViewById(R.id.rg);
        imagelist.add("http://cdn.duitang.com/uploads/blog/201308/18/20130818150526_Ru2Bk.thumb.600_0.png");
        imagelist.add("http://www.bkill.com/u/info_img/2012-09/02/2012083116140522302.jpg");
        imagelist.add("http://www.it165.net/uploadfile/2011/1218/20111218070928328.jpg");

        pager.setAdapter(new My());
        jian();
        new Thread(){
            @Override
            public void run() {
                while(true) {
                    try {
                        Thread.sleep(1000);
                        con++;
                        handler.sendEmptyMessage(con);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
        xlv = (XListView) findViewById(R.id.xlv);
        xlv.setPullLoadEnable(true);
        xlv.setXListViewListener(this);
        aa();
    }
    public void aa(){
        jiexi("http://apis.juhe.cn/cook/query?key=900eb2e99f3c7b21f3914aefa914a327&menu=%E8%A5%BF%E7%BA%A2%E6%9F%BF&rn=10&pn="+page+"");
    }
    public void jiexi(String path){
        new AsyncTask<String ,Void,String>(){
            @Override
            protected void onPostExecute(String s) {
                if (s!=null){
                    Gson gson=new Gson();
                    Bean bean = gson.fromJson(s, Bean.class);
                    List<Bean.ResultBean.DataBean> list = bean.getResult().getData();
                    if (adapter==null){
                        adapter= new MyBaseadapter(MainActivity.this,list);
                        xlv.setAdapter(adapter);
                    }else{
                        adapter.sss(list,flag);
                    }
                }
            }

            @Override
            protected String doInBackground(String... params) {
                try {
                    URL url=new URL(params[0]);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setReadTimeout(3000);
                    connection.setConnectTimeout(3000);
                    int code = connection.getResponseCode();
                    if (code==200){
                        InputStream is = connection.getInputStream();
                        return info.readFromNetWork(is);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        }.execute(path);
    }

    @Override
    public void onRefresh() {
       flag=false;
        ++page;
        aa();
        xlv.stopRefresh(true);
    }

    @Override
    public void onLoadMore() {
       flag=true;
        ++page;
        aa();
        xlv.stopLoadMore();
    }

    class My extends PagerAdapter{

        @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(MainActivity.this);
            ImageLoader.getInstance().displayImage(imagelist.get(position%imagelist.size()),imageView,appliction.getImageOptions());
            container.addView(imageView);
            return imageView;
        }

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

            container.removeView((View) object);
        }
    }
    public void jian(){
        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                   switch (position%imagelist.size()){
                       case 0:
                           rg.check(R.id.rb1);
                           break;
                       case 1:
                           rg.check(R.id.rb2);
                           break;
                       case 2:
                           rg.check(R.id.rb3);
                           break;
                   }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId%imagelist.size()){
                    case R.id.rb1:
                        pager.setCurrentItem(0);
                        break;
                    case R.id.rb2:
                        pager.setCurrentItem(1);
                        break;
                    case R.id.rb3:
                        pager.setCurrentItem(2);
                        break;
                }
            }
        });
    }
}
public class MyBaseadapter extends BaseAdapter {
    private Context context;
    private List<Bean.ResultBean.DataBean> list;
    public MyBaseadapter(Context context, List<Bean.ResultBean.DataBean> list){
        this.context=context;
        this.list=list;
    }
    public void sss(List<Bean.ResultBean.DataBean> data, boolean flag){
        for (Bean.ResultBean.DataBean s: data) {
            if (flag){
                list.add(s);
            }else{
                list.add(0,s);
            }

        }
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return list!=null?list.size():0;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position%2==0){
            convertView=View.inflate(context,R.layout.item,null);
            ImageView image= (ImageView) convertView.findViewById(R.id.image);
            TextView tv= (TextView) convertView.findViewById(R.id.tv);
            tv.setText(list.get(position).getTitle());
            ImageLoader.getInstance().displayImage(list.get(position).getAlbums().get(0),image,appliction.getImageOptions());
        }else{
            convertView=View.inflate(context,R.layout.item2,null);
            ImageView image1= (ImageView) convertView.findViewById(R.id.image1);
            TextView tv1= (TextView) convertView.findViewById(R.id.tv1);
            tv1.setText(list.get(position).getTitle());
            ImageLoader.getInstance().displayImage(list.get(position).getAlbums().get(0),image1,appliction.getImageOptions());
        }

        return convertView;
    }
}

 

studio无限轮播

标签:www.   apk   ant   puts   cut   pager   pixel   state   override   

原文地址:http://www.cnblogs.com/leshen/p/7395259.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!