一般Demo 的框架

在 Project 的gradle中添加
allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
}

在 gradle中添加
compile 'com.google.code.gson:gson:2.2.4'
compile 'com.squareup.picasso:picasso:2.3.2'
compile 'com.android.support:recyclerview-v7:25.0.0'
compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'
compile 'com.github.bumptech.glide:glide:4.0.0'

//必须使用
compile 'com.lzy.net:okgo:3.0.4'
compile 'com.lzy.net:okserver:2.0.5'
compile 'com.elvishew:xlog:1.4.0'

//新版本预览版-发现bug请加群提出,并切换 1.0.3 版本
compile 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.4-alpha-9'

compile 'com.android.support:appcompat-v7:25.3.1'//版本随意(必须)
compile 'com.android.support:design:25.3.1' 

清单文件

几个类
public class MyApplication extends Application {

static {
    SmartRefreshLayout.setDefaultRefreshHeaderCreater(new DefaultRefreshHeaderCreater() {
        @Override
        public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
            return new ClassicsHeader(context);//指定为经典Header,默认是 贝塞尔雷达Header
        }
    });
    //设置全局的Footer构建器
    SmartRefreshLayout.setDefaultRefreshFooterCreater(new DefaultRefreshFooterCreater() {
        @Override
        public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
            //指定为经典Footer,默认是 BallPulseFooter
            return new ClassicsFooter(context).setDrawableSize(20);
        }
    });
}

@Override
public void onCreate() {
    super.onCreate();
    OkGo.getInstance().init(this);
    XLog.init(LogLevel.ALL);

}

}

public class GsonUtils {
private static Gson gson = null;
static {
if (gson == null) {
gson = new Gson();
}
}

private GsonUtils() {
}


public static String GsonString(Object object) {
    String gsonString = null;
    if (gson != null) {
        gsonString = gson.toJson(object);
    }
    return gsonString;
}


public static  T GsonToBean(String gsonString, Class cls) {
    T t = null;
    if (gson != null) {
        t = gson.fromJson(gsonString, cls);
    }
    return t;
}


public static  List GsonToList(String gsonString, Class cls) {
    List list = null;
    if (gson != null) {
        list = gson.fromJson(gsonString, new TypeToken>() {
        }.getType());
    }
    return list;
}


public static  List jsonToList(String json, Class cls) {
    Gson gson = new Gson();
    List list = new ArrayList();
    JsonArray array = new JsonParser().parse(json).getAsJsonArray();
    for(final JsonElement elem : array){
        list.add(gson.fromJson(elem, cls));
    }
    return list;
}

public static  List> GsonToListMaps(String gsonString) {
    List> list = null;
    if (gson != null) {
        list = gson.fromJson(gsonString,
                new TypeToken>>() {
                }.getType());
    }
    return list;
}

public static  Map GsonToMaps(String gsonString) {
    Map map = null;
    if (gson != null) {
        map = gson.fromJson(gsonString, new TypeToken>() {
        }.getType());
    }
    return map;
}

public static String jsonToString(String json,String key) {
    JSONObject obj = null;
    String name = "";
    try {
        obj = new JSONObject(json);
        name =  obj.getString(key);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return name;
}

public static int jsonToInt(String json,String key) {
    JSONObject obj = null;
    int code = -1000;
    try {
        obj = new JSONObject(json);
        code =  obj.getInt(key);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return code;
}

}

/**

  • Created by JsonQiu on 2017/11/3 12:01.
    */
    public class FuLiAdapter extends BaseQuickAdapter {
    public FuLiAdapter(@LayoutRes int layoutResId, @Nullable List data) {
    super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, FuLiDataBean.ResultsBean item) {

     helper.setText(R.id.item_tv, item.who);
     Glide.with(mContext).load(item.url).into((ImageView) helper.getView(R.id.item_pic));
    

    }
    }

public class MainActivity extends AppCompatActivity {

private RecyclerView myRecycle;
private String url;
private MainActivity mContext;
private SmartRefreshLayout mSmartrefresh;
private int page = 1;
private FuLiAdapter adapter;
private List listDatas = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mContext = this;
    initView();

    initData(url, page);

    getData();
}

private void getData() {

}

private void initData(String url, int page) {
    url = "http://gank.io/api/data/福利/10/" + page;
    XLog.e(url);
    OkGo.get(url)
            .tag(mContext)
            .cacheKey("cacheKey")
            .cacheMode(CacheMode.DEFAULT)
            .execute(new StringCallback() {
                @Override
                public void onSuccess(Response response) {
                    // XLog.e(response.body().toString());
                    FuLiDataBean fuLiDataBean = GsonUtils.GsonToBean(response.body().toString(), FuLiDataBean.class);
                    if (fuLiDataBean != null) {
                        List results = fuLiDataBean.results;
                        if (results != null) {
                            showData(results);
                        }
                    }
                }
            });
}

private void showData(List fuLiDataBean) {
    GridLayoutManager manager = new GridLayoutManager(mContext, 3, GridLayoutManager.VERTICAL, false);
    myRecycle.setLayoutManager(manager);
    if (listDatas == null || listDatas.size() == 0) {
        listDatas = fuLiDataBean;
    } else {
        listDatas.addAll(fuLiDataBean);
    }
    if (adapter == null) {
        XLog.e("111111111111");
        adapter = new FuLiAdapter(R.layout.recy_item, listDatas);
    } else {
        XLog.e("22222222222");
        adapter.notifyDataSetChanged();
    }
    myRecycle.setAdapter(adapter);
}

private void initView() {
    myRecycle = (RecyclerView) findViewById(R.id.recycleview);
    mSmartrefresh = (SmartRefreshLayout) findViewById(R.id.smartrefresh);
    mSmartrefresh.setOnRefreshListener(new OnRefreshListener() {
        @Override
        public void onRefresh(RefreshLayout refreshlayout) {
            XLog.e("888888888888");
            listDatas.clear();
            adapter=null;
            initData(url, 1);
            refreshlayout.finishRefresh(1500);
        }
    });
    mSmartrefresh.setOnLoadmoreListener(new OnLoadmoreListener() {
        @Override
        public void onLoadmore(RefreshLayout refreshlayout) {
            XLog.e("99999999999");
            ++page;
            initData(url, page);
            refreshlayout.finishLoadmore(1500);
        }
    });
}

}

你可能感兴趣的:(一般Demo 的框架)