无限轮播+商品展示+下拉刷新

//页面布局


    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=".GoodsActivity"
    android:orientation="vertical"
    >

        android:id="@+id/goods_scrollview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
            android:layout_width="match_parent"
        android:layout_height="240dp"
        android:orientation="vertical"
        >

                    android:layout_width="match_parent"
            android:layout_height="240dp">

                            android:id="@+id/goods_viewpager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#0f0"
                >

           

                            android:id="@+id/goods_line"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_alignParentBottom="true"
                android:background="#ff5656"
                android:gravity="center"
                android:orientation="horizontal">

           


       




   


            android:id="@+id/goods_listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">





//java代码



public class GoodsActivity extends AppCompatActivity {


    private static final String TAG = "GoodsActivity---";
    private MyListView listview;
    private PullToRefreshScrollView pull;
    private MyGoodsAdapter adapter;
    private ViewPager viewpager;
    private LinearLayout line;
    private List lunbo = new ArrayList<>();
    private List cir = new ArrayList<>();
    private List list = new ArrayList<>();

    private int page = 1;

    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            viewpager.setCurrentItem(viewpager.getCurrentItem() + 1);
            for (int i = 0; i < cir.size(); i++) {

                if (i == viewpager.getCurrentItem() % cir.size()) {

                    cir.get(i).setSelected(true);
                } else {
                    cir.get(i).setSelected(false);
                }
            }

            handler.sendEmptyMessageDelayed(0, 2000);


        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods);

        //初始化页面
        initViews();
        //请求轮播图
        getDataFormNet();

        // getGoods();

    }

    //轮播图
    public void getDataFormNet() {

        HttpUtils httpUtils = HttpUtils.getHttpUtils();
        httpUtils.get(HttpConfig.URL4);

        httpUtils.setHttpListener(new HttpUtils.HttpListener() {

            private MyPageAdapter adapter;

            @Override
            public void getSuccess(String json) {
                Log.d(TAG, "getSuccess: " + json);

                Gson gson = new Gson();
                GoodsBean goods = gson.fromJson(json, GoodsBean.class);
                //将数据添加到集合

                final List data = goods.getData();

                for (int i = 0; i < data.size(); i++) {

                    String icon = data.get(i).getIcon();
                    ImageView imageView = new ImageView(GoodsActivity.this);
                    //缩放
                    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    ImageLoader.getInstance().displayImage(icon, imageView, MyApp.getOptions());

                    lunbo.add(imageView);

                    //点击标题进行吐司
                    final int j = i;
                    imageView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Toast.makeText(GoodsActivity.this, data.get(j).getTitle(), Toast.LENGTH_SHORT).show();
                        }
                    });

                    //添加小圆点
                    ImageView img = new ImageView(GoodsActivity.this);
                    img.setImageResource(R.drawable.select_end);
                    cir.add(img);

                    //img.setPadding(5,0,5,0);
                    LinearLayout.LayoutParams li = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

                    li.setMargins(10, 0, 10, 0);
                    img.setLayoutParams(li);
                    line.addView(img, i);
                }

                //默认显示第一个
                cir.get(0).setSelected(true);

                //设置适配器
                adapter = new MyPageAdapter(lunbo);
                viewpager.setAdapter(adapter);
                //请求商品
                getGoods();
                //发送hndler请求开始自动轮播
                handler.sendEmptyMessageDelayed(0, 2000);

            }

            @Override
            public void getError(String error) {

            }
        });
    }

    //请求商品数据
    private void getGoods() {

        HttpUtils httpUtils = HttpUtils.getHttpUtils();

        httpUtils.get(HttpConfig.URL5 + page);

        httpUtils.setHttpListener(new HttpUtils.HttpListener() {
            @Override
            public void getSuccess(String json) {
                Log.d(TAG, "商品数据: " + json);
                Gson gson = new Gson();
                ShopBean shop = gson.fromJson(json, ShopBean.class);

                List sh = shop.getData();
                Log.d(TAG, "数--------- " + sh);
                if (page == 1) {

                    list.clear();
                }
                if (sh.size() == 0) {


                    Toast.makeText(GoodsActivity.this, "数据一倒头,没有了", Toast.LENGTH_SHORT).show();
                }
                if (sh != null) {

                    list.addAll(sh);
                }

                //请求商品数据
                adapter = new MyGoodsAdapter(GoodsActivity.this, list);
                listview.setAdapter(adapter);
                //刷新适配器
                adapter.notifyDataSetChanged();
                pull.onRefreshComplete();


            }

            @Override
            public void getError(String error) {

            }
        });
    }


    //初始化页面
    private void initViews() {

        pull = findViewById(R.id.goods_scrollview);
        viewpager = findViewById(R.id.goods_viewpager);
        line = findViewById(R.id.goods_line);
        listview = findViewById(R.id.goods_listview);
//刷新
        pull.setMode(PullToRefreshBase.Mode.BOTH);


        pull.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase refreshView) {

                page = 1;
                //调用请求数据的方法
                getGoods();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase refreshView) {

                page++;
                //调用请求数据的方法
                getGoods();
            }
        });

viewpager.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {

        switch (event.getAction()){

            case MotionEvent.ACTION_DOWN:
                handler.removeCallbacksAndMessages(null);
                break;

                case MotionEvent.ACTION_UP:
                    handler.sendEmptyMessageDelayed(0,2000);
                    break;

                    case MotionEvent.ACTION_MOVE:
                        handler.removeCallbacksAndMessages(null);
                        break;
        }
        return false;
    }
});



    }





}


//轮播的适配器






public class MyPageAdapter extends PagerAdapter{

   private List list;

    public MyPageAdapter(List list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view==object;
    }


    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {


        ImageView imageView = list.get(position%list.size());
        container.addView(imageView);
        return imageView;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
       container.removeView((ImageView) object);

    }
}


//商品展示适配器





public class MyGoodsAdapter extends BaseAdapter {

    private Context context;
    private List list;

    public MyGoodsAdapter(Context context, List list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @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) {

        ViewHolder holder;
        if(convertView==null){


       convertView=View.inflate(context,R.layout.item_goods,null);
       holder = new ViewHolder();
       holder.img=convertView.findViewById(R.id.g_img);
       holder.text=convertView.findViewById(R.id.g_text);

       convertView.setTag(holder);

   }else{
            holder=(ViewHolder) convertView.getTag();
        }

        String pic = list.get(position).getImages();
        String s = pic.split("\\|")[0];
        ImageLoader.getInstance().displayImage(s,holder.img, MyApp.getOptions());

        holder.text.setText(list.get(position).getTitle());
        return convertView;
    }


class ViewHolder{
        ImageView img;
        TextView text;
}

}

//商品展示的适配器



public class MyGoodsAdapter extends BaseAdapter {

    private Context context;
    private List list;

    public MyGoodsAdapter(Context context, List list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @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) {

        ViewHolder holder;
        if(convertView==null){


       convertView=View.inflate(context,R.layout.item_goods,null);
       holder = new ViewHolder();
       holder.img=convertView.findViewById(R.id.g_img);
       holder.text=convertView.findViewById(R.id.g_text);

       convertView.setTag(holder);

   }else{
            holder=(ViewHolder) convertView.getTag();
        }

        String pic = list.get(position).getImages();
        String s = pic.split("\\|")[0];
        ImageLoader.getInstance().displayImage(s,holder.img, MyApp.getOptions());

        holder.text.setText(list.get(position).getTitle());
        return convertView;
    }


class ViewHolder{
        ImageView img;
        TextView text;
}

}







你可能感兴趣的:(无限轮播+商品展示+下拉刷新)