recyclerview网络获取图片展示瀑布流MVP

activity_main布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.myapplication.MainActivity">
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btn"
        />
<android.support.v7.widget.RecyclerView

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/recyclerview"></android.support.v7.widget.RecyclerView>
</LinearLayout>

MainActivity主类

package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.example.myapplication.adapter.RecyclerviewAdapter;
import com.example.myapplication.bean.NewBean;
import com.example.myapplication.presenter.RecyclerviewPresenter;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements RecyclerviewPresenter.viewCallBack {

    private RecyclerView recyclerView;
        List<NewBean.DataBean.ListBean> list=new ArrayList<>();
    List<String> imageList;
    private RecyclerviewAdapter adapter;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        button = (Button) findViewById(R.id.btn);
        RecyclerviewPresenter recyclerviewPresenter = new RecyclerviewPresenter(this);
        recyclerviewPresenter.getinit(" http://120.27.23.105/product/getCarts?uid=100");
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, LinearLayout.VERTICAL));
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(MainActivity.this, SecondActivity.class);
        startActivity(intent);
        finish();
    }
});







    }

    @Override
    public void success(final NewBean newBean) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if(list==null){
                    list=new ArrayList<NewBean.DataBean.ListBean>();
                }
                list.add(newBean.getData().get(0).getList().get(0));
                String images = list.get(0).getImages();
                String[] split = images.split("\\|");
                if(imageList==null){
                    imageList=new ArrayList<>();

                }


                for (int i=0;i<split.length;i++){
                    imageList.add(split[i]);
                }

                adapter = new RecyclerviewAdapter(MainActivity.this,imageList);
                recyclerView.setAdapter(adapter);





            }
        });

    }

    @Override
    public void fail(Exception e) {

    }
}
适配器

package com.example.myapplication.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;

import com.example.myapplication.R;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;
import java.util.Random;

/**
 * Created by Administrator on 2017/11/18.
 */
public class RecyclerviewAdapter extends RecyclerView.Adapter<RecyclerviewAdapter.ImageViewHolder> {
    Context context;
    List<String> imageList;
    private final int itemWidth;
    public RecyclerviewAdapter(Context context, List<String> imageList) {
        this.context = context;
        this.imageList = imageList;
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);

        int width = wm.getDefaultDisplay().getWidth();
        itemWidth = width / 3;
    }







    @Override
    public ImageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view= LayoutInflater.from(context).inflate(R.layout.recyclerview_item,null);
        return new ImageViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ImageViewHolder holder, int position) {
        ViewGroup.LayoutParams params = holder.iamge.getLayoutParams();
        int itemHeight = 300 ;

        itemHeight = new Random().nextInt(500);
        if(itemHeight < 300){
            itemHeight = 300 ;
        }

        params.width = itemWidth ;
        params.height = itemHeight ;
        holder.iamge.setLayoutParams(params);
        ImageLoader.getInstance().displayImage(imageList.get(position),holder.iamge);
        holder.iamge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener==null){
                    listener.onClick(v);
                }
            }
        });
        holder.iamge.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                listener.longClick(v);
                return false;
            }
        });
    }

    @Override
    public int getItemCount() {
        return imageList.size();
    }


    public class ImageViewHolder extends RecyclerView.ViewHolder {

        private final ImageView iamge;

        public ImageViewHolder(View view) {
            super(view);
            iamge = (ImageView) view.findViewById(R.id.image);

        }
    }
    private Listener listener;
    public void setListener(Listener listener){
        this.listener=listener;
    }
    public interface Listener{
        public void onClick(View view);
        public void longClick(View view);
    }
}
适配器布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
<ImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:id="@+id/image"
    android:src="@mipmap/ic_launcher"
    />
</LinearLayout>
iamgeLoger配置

package com.example.myapplication;

import android.app.Application;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

/**
 * Created by Administrator on 2017/11/18.
 */
public class MyApp extends Application{

    @Override
    public void onCreate() {
        super.onCreate();

        ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this).build();
        ImageLoader.getInstance().init(configuration);
    }
}
model层

package com.example.myapplication.model;

import com.example.myapplication.bean.NewBean;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/11/18.
 */
public class RecyclerviewModel {
    public void getData(String url, final modelCallBack modelCallBack){
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String string = response.body().string();
                Gson gson = new Gson();
                NewBean newBean = gson.fromJson(string, NewBean.class);
                modelCallBack.success(newBean);
                //Log.i("111111111111",string);

            }
        });

    }
    public interface modelCallBack{
        public void success(NewBean newBean);
        public void fail(Exception e);
    }
}
presenter层
package com.example.myapplication.presenter;

import android.util.Log;

import com.example.myapplication.bean.NewBean;
import com.example.myapplication.model.RecyclerviewModel;

/**
 * Created by Administrator on 2017/11/18.
 */
public class RecyclerviewPresenter{
    RecyclerviewModel recyclerviewModel=new RecyclerviewModel();
    viewCallBack viewCallBack;



    public RecyclerviewPresenter(viewCallBack viewCallBack){
        this.viewCallBack=viewCallBack;
    }
    public interface viewCallBack{
        public void success(NewBean newBean);
        public void fail(Exception e);
    }

    public void getinit(String url){
        recyclerviewModel.getData(url, new RecyclerviewModel.modelCallBack() {
            @Override
            public void success(NewBean newBean) {
                viewCallBack.success(newBean);
                Log.i("11111111111",newBean+"");
                System.out.println(newBean);
            }

            @Override
            public void fail(Exception e) {
                viewCallBack.fail(e);

            }
            });

        }

    }


展开阅读全文

没有更多推荐了,返回首页