美文网首页
一般Demo 的框架

一般Demo 的框架

作者: 自然之秋 | 来源:发表于2017-11-09 09:18 被阅读35次

    在 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' 
    

    清单文件
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:name=".MyApplication"
    

    几个类
    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> T GsonToBean(String gsonString, Class<T> cls) {
        T t = null;
        if (gson != null) {
            t = gson.fromJson(gsonString, cls);
        }
        return t;
    }
    
    
    public static <T> List<T> GsonToList(String gsonString, Class<T> cls) {
        List<T> list = null;
        if (gson != null) {
            list = gson.fromJson(gsonString, new TypeToken<List<T>>() {
            }.getType());
        }
        return list;
    }
    
    
    public static <T> List<T> jsonToList(String json, Class<T> cls) {
        Gson gson = new Gson();
        List<T> list = new ArrayList<T>();
        JsonArray array = new JsonParser().parse(json).getAsJsonArray();
        for(final JsonElement elem : array){
            list.add(gson.fromJson(elem, cls));
        }
        return list;
    }
    
    public static <T> List<Map<String, T>> GsonToListMaps(String gsonString) {
        List<Map<String, T>> list = null;
        if (gson != null) {
            list = gson.fromJson(gsonString,
                    new TypeToken<List<Map<String, T>>>() {
                    }.getType());
        }
        return list;
    }
    
    public static <T> Map<String, T> GsonToMaps(String gsonString) {
        Map<String, T> map = null;
        if (gson != null) {
            map = gson.fromJson(gsonString, new TypeToken<Map<String, T>>() {
            }.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<FuLiDataBean.ResultsBean, BaseViewHolder> {
      public FuLiAdapter(@LayoutRes int layoutResId, @Nullable List<FuLiDataBean.ResultsBean> 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<FuLiDataBean.ResultsBean> 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.<String>get(url)
                .tag(mContext)
                .cacheKey("cacheKey")
                .cacheMode(CacheMode.DEFAULT)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        // XLog.e(response.body().toString());
                        FuLiDataBean fuLiDataBean = GsonUtils.GsonToBean(response.body().toString(), FuLiDataBean.class);
                        if (fuLiDataBean != null) {
                            List<FuLiDataBean.ResultsBean> results = fuLiDataBean.results;
                            if (results != null) {
                                showData(results);
                            }
                        }
                    }
                });
    }
    
    private void showData(List<FuLiDataBean.ResultsBean> 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 的框架

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