美文网首页
Banner实现轮播

Banner实现轮播

作者: Meimaobing | 来源:发表于2017-10-23 08:54 被阅读0次

package com.example.day16;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.day16.Bean.User;
import com.example.day16.okhttp.OkHttpUtils;
import com.example.day16.okhttp.OnUiCallback;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.youth.banner.Banner;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Call;

public class MainActivity extends AppCompatActivity {
    Banner banner;
    User user;
    RecyclerView rv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        banner= (Banner) findViewById(R.id.bn);
        rv= (RecyclerView) findViewById(R.id.rv);
        LinearLayoutManager mgr=new LinearLayoutManager(this);
        mgr.setOrientation(LinearLayoutManager.VERTICAL);
        rv.setLayoutManager(mgr);

        ImageLoaderConfiguration con=ImageLoaderConfiguration.createDefault(this);
        ImageLoader.getInstance().init(con);
        getData();

    }

    private void getData() {
        OkHttpUtils.getInstance().doGet("http://m.yunifang.com/yunifang/mobile/home", new OnUiCallback() {
            @Override
            public void onFailed(Call call, IOException e) {

            }

            @Override
            public void onSuccess(String result) throws IOException {
                Gson gson=new Gson();
                java.lang.reflect.Type type=new TypeToken<User>(){}.getType();
                user=gson.fromJson(result,type);
                Myadpter myadpter=new Myadpter();
                rv.setAdapter(myadpter);
                ArrayList<String> pic=new ArrayList<>();
                for (int i=0;i<user.getData().getSubjects().size();i++)
                {
                    pic.add(user.getData().getSubjects().get(i).getImage());
                }
                banner.setImageLoader(new ImageBanner());
                    banner.setImages(pic);
                    banner.start();


            }
        });
    }
    class Myadpter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
    {
        public static final int TYPE_ONE=1;
        public static final int TYPE_TWO=2;

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
           if (viewType==TYPE_ONE){
               View view=View.inflate(MainActivity.this,R.layout.item,null);
               MyViewHolder holder=new MyViewHolder(view);
               return  holder;
           }else {
               View view1=View.inflate(MainActivity.this,R.layout.item2,null);
               MyViewHolder1 holder1=new MyViewHolder1(view1);
               return holder1;
           }


        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            if (holder instanceof MyViewHolder)
            {
                ((MyViewHolder) holder).tv1.setText(user.getData().getSubjects().get(position).getTitle());
                ((MyViewHolder) holder).tv2.setText(user.getData().getSubjects().get(position).getDetail());
                ImageLoader.getInstance().displayImage(user.getData().getSubjects().get(position).getImage(),((MyViewHolder) holder).iv);
            }
            if (holder instanceof MyViewHolder1)
            {
                ((MyViewHolder1) holder).tv3.setText(user.getData().getSubjects().get(position).getEnd_time());
            }
        }

        @Override
        public int getItemCount() {
            return user.getData().getSubjects().size();
        }

        @Override
        public int getItemViewType(int position) {
            if (position%2==0)
            {
                return TYPE_ONE;
            }else {
                return TYPE_TWO;
            }
        }

        public class MyViewHolder extends RecyclerView.ViewHolder {
            TextView tv1,tv2;
            ImageView iv;
            public MyViewHolder(View itemView) {
                super(itemView);
                tv1=itemView.findViewById(R.id.tv1);
                tv2=itemView.findViewById(R.id.tv2);
                iv=itemView.findViewById(R.id.iv);
            }
        }
        class MyViewHolder1 extends RecyclerView.ViewHolder{
            TextView tv3;
            public MyViewHolder1(View itemView) {
                super(itemView);
                tv3=itemView.findViewById(R.id.tv3);
            }
        }
    }

}



相关文章

网友评论

      本文标题:Banner实现轮播

      本文链接:https://www.haomeiwen.com/subject/gasouxtx.html