判断网络
public boolean isIntent(){
ConnectivityManager cm= (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = cm.getActiveNetworkInfo();
return activeNetworkInfo!=null&&activeNetworkInfo.isAvailable();
}
APP
package com.example.a09zhokaoermoni;
import android.app.Application;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import com.example.a09zhokaoermoni.bean.greendao.DaoMaster;
import com.example.a09zhokaoermoni.bean.greendao.DaoSession;
import com.facebook.cache.disk.DiskCacheConfig;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.imagepipeline.core.ImagePipelineConfig;
public class APP extends Application {
private DaoSession daoSession;
@Override
public void onCreate() {
super.onCreate();
DiskCacheConfig build = DiskCacheConfig.newBuilder(this)
.setBaseDirectoryName("zker_image")
.setBaseDirectoryPath(Environment.getExternalStorageDirectory())
.build();
ImagePipelineConfig build1 = ImagePipelineConfig.newBuilder(this)
.setMainDiskCacheConfig(build)
.build();
Fresco.initialize(this,build1);
initDreenDao();
}
private void initDreenDao() {
DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(this, "derzho.db");
SQLiteDatabase db = devOpenHelper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(db);
daoSession = daoMaster.newSession();
}
public DaoSession getDaoSession() {
return daoSession;
}
}
GreenDaoUtils
package com.example.a09zhokaoermoni.net;
import android.content.Context;
import com.example.a09zhokaoermoni.APP;
import com.example.a09zhokaoermoni.bean.GreenBean;
import com.example.a09zhokaoermoni.bean.greendao.DaoSession;
import java.util.List;
public class GreenDaoUtils {
private final APP app;
private final DaoSession session;
public GreenDaoUtils(Context context) {
app=(APP)context.getApplicationContext();
session=app.getDaoSession();
}
public void insert(GreenBean greenBean){
session.insertOrReplace(greenBean);
}
public List<GreenBean> seleteAll(){
List<GreenBean> greenBeans = session.loadAll(GreenBean.class);
return greenBeans;
}
}
Show展示页面
package com.example.xsxerdiyiz.activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.TextView;
import com.example.xsxerdiyiz.R;
import com.example.xsxerdiyiz.fragment.FragmentOne;
import com.example.xsxerdiyiz.fragment.FragmentThree;
import com.example.xsxerdiyiz.fragment.FragmentTwo;
public class ShowActivity extends AppCompatActivity {
private TextView mTextMessage;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
pager.setCurrentItem(0);
return true;
case R.id.navigation_dashboard:
pager.setCurrentItem(1);
return true;
case R.id.navigation_notifications:
pager.setCurrentItem(2);
return true;
}
return false;
}
};
private ViewPager pager;
private BottomNavigationView navigation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
initView();
pager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int i) {
switch (i){
case 0:
return new FragmentOne();
case 1:
return new FragmentTwo();
case 2:
return new FragmentThree();
}
return null;
}
@Override
public int getCount() {
return 3;
}
});
mTextMessage = (TextView) findViewById(R.id.message);
final BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
switch (i){
case 0:
navigation.setSelectedItemId(R.id.navigation_home);
break;
case 1:
navigation.setSelectedItemId(R.id.navigation_dashboard);
break;
case 2:
navigation.setSelectedItemId(R.id.navigation_notifications);
break;
}
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
}
private void initView() {
pager = (ViewPager) findViewById(R.id.pager);
navigation = (BottomNavigationView) findViewById(R.id.navigation);
}
}
BaseFragment
package com.example.xsxerdiyiz.fragment;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import butterknife.ButterKnife;
import butterknife.Unbinder;
public abstract class BaseFragment extends Fragment {
private Unbinder bind;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(getLayout(),container,false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
bind = ButterKnife.bind(this, view);
initData();
initView(view);
}
protected abstract int getLayout();
protected abstract void initView(View view);
protected abstract void initData();
@Override
public void onDestroy() {
super.onDestroy();
if (bind!=null){
bind.unbind();
}
}
}
fragmentone
package com.example.lianxidiyizmoni.fragment;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.example.lianxidiyizmoni.R;
import com.example.lianxidiyizmoni.adapter.ShowAdapter;
import com.example.lianxidiyizmoni.contract.ShowContract;
import com.example.lianxidiyizmoni.entity.ShowBean;
import com.example.lianxidiyizmoni.presenter.ShowPresenterZhu;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import java.util.HashMap;
import butterknife.BindView;
import butterknife.OnClick;
public class FragmentOne extends BaseFragment implements ShowContract.ShowView {
@BindView(R.id.xre)
XRecyclerView rv;
@BindView(R.id.but_name)
Button but_name;
@BindView(R.id.et_name)
EditText et_name;
private String page=“1”;
private String count=“20”;
private ShowPresenterZhu showPresenterZhu;
private ShowAdapter showAdapter;
@Override
protected int getlayout() {
return R.layout.fragmentone;
}
@Override
protected void initView(View view) {
showPresenterZhu = new ShowPresenterZhu(this);
showAdapter = new ShowAdapter(getActivity());
rv.setLayoutManager(new GridLayoutManager(getActivity(),2));
rv.setAdapter(showAdapter);
}
@Override
protected void initData() {
}
@OnClick(R.id.but_name)
public void but(View view){
String string = et_name.getText().toString();
HashMap<String,String >params=new HashMap<>();
params.put("keyword",string);
params.put("page",page);
params.put("count",count);
showPresenterZhu.getPresenter(params);
}
@Override
public void Success(ShowBean showBean) {
if (showBean!=null){
showAdapter.setList(showBean.result);
}
}
@Override
public void FrailUre(String msg) {
}
}
Api接口
package com.example.lianxidiyizmoni.api;
public class Api {
public static final String BASE_REG=“http://172.17.8.100/”;
public static final String USER_SHOW=“small/commodity/v1/findCommodityByKeyword”;
}
entityGson的解析数据
package com.example.lianxidiyizmoni.entity;
import java.util.List;
public class ShowBean {
public String message;
public String status;
public List result;
public class shopData{
public String commodityName;
public String commodityId;
public String masterPic;
public String price;
}
}
RequestCallback
package com.example.lianxidiyizmoni.net;
public interface RequestCallback {
void Success(String res);
void FrailUre(String msg);
}
ShowContract契约类
package com.example.lianxidiyizmoni.contract;
import com.example.lianxidiyizmoni.entity.ShowBean;
import com.example.lianxidiyizmoni.net.RequestCallback;
import java.util.HashMap;
public interface ShowContract {
public abstract class ShowPresenter{
public abstract void getPresenter(HashMap<String,String >params);
}
interface ShowModel{
void getModel(HashMap<String,String >params, RequestCallback callback);
}
interface ShowView{
void Success(ShowBean showBean);
void FrailUre(String msg);
}
}
showApiService接口
package com.example.lianxidiyizmoni.net;
import java.util.HashMap;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FieldMap;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.POST;
import retrofit2.http.PUT;
import retrofit2.http.Url;
public interface ShowApiService {
@POST
@FormUrlEncoded
Call postReg(@Url String url, @FieldMap HashMap<String,String> hashMap);
@GET
CallgetReg(@Url String url);
@PUT
Call putReg(@Header(“userId”)String id, @Header(“sessionId”)String sid, @Field(“nickName”)String name);
}
ShowUtils
package com.example.lianxidiyizmoni.net;
import com.example.lianxidiyizmoni.api.Api;
import java.io.IOException;
import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class ShowUtils {
private static ShowUtils ImafitUtils;
private Retrofit retrofit;
private ShowUtils(){
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okHttpClient=new OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.build();
retrofit=new Retrofit.Builder()
.baseUrl(Api.BASE_REG)
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
}
public static ShowUtils getImafitUtils(){
if (ImafitUtilsnull){
synchronized (ShowUtils.class){
if (ImafitUtilsnull){
ImafitUtils=new ShowUtils();
}
}
}
return ImafitUtils;
}
public void doGet(String url, final RequestCallback callback){
ShowApiService showApiService = retrofit.create(ShowApiService.class);
showApiService.getReg(url).enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
if (response.code()==200){
try {
String string = response.body().string();
if (callback!=null){
callback.Success(string);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
if (callback!=null){
callback.FrailUre("网络不稳定,请稍后再试");
}
}
});
}
}
showModel
package com.example.lianxidiyizmoni.model;
import com.example.lianxidiyizmoni.api.Api;
import com.example.lianxidiyizmoni.contract.ShowContract;
import com.example.lianxidiyizmoni.net.RequestCallback;
import com.example.lianxidiyizmoni.net.ShowUtils;
import java.util.HashMap;
public class ShowModelzhu implements ShowContract.ShowModel {
@Override
public void getModel(HashMap<String, String> params, final RequestCallback callback) {
String keyword = params.get(“keyword”);
String page = params.get(“page”);
String count = params.get(“count”);
ShowUtils.getImafitUtils().doGet(Api.USER_SHOW + “?keyword=” + keyword + “&page=” + page + “&count=” + count, new RequestCallback() {
@Override
public void Success(String res) {
if (callback!=null){
callback.Success(res);
}
}
@Override
public void FrailUre(String msg) {
if (callback!=null){
callback.FrailUre(msg);
}
}
});
}
}
showPresenter
package com.example.lianxidiyizmoni.presenter;
import com.example.lianxidiyizmoni.contract.ShowContract;
import com.example.lianxidiyizmoni.entity.ShowBean;
import com.example.lianxidiyizmoni.model.ShowModelzhu;
import com.example.lianxidiyizmoni.net.RequestCallback;
import com.google.gson.Gson;
import java.lang.ref.WeakReference;
import java.util.HashMap;
public class ShowPresenterZhu extends ShowContract.ShowPresenter {
private ShowContract.ShowView view;
//private WeakReference<ShowContract.ShowView>wr;
private ShowModelzhu showModelzhu;
public ShowPresenterZhu(ShowContract.ShowView view) {
this.view = view;
//wr=new WeakReference<>(view);
showModelzhu=new ShowModelzhu();
}
@Override
public void getPresenter(HashMap<String, String> params) {
showModelzhu.getModel(params, new RequestCallback() {
@Override
public void Success(String res) {
if (view!=null){
ShowBean showBean = new Gson().fromJson(res, ShowBean.class);
view.Success(showBean);
}
}
@Override
public void FrailUre(String msg) {
if (view!=null){
view.FrailUre(msg);
}
}
});
}
}
APP(注意,不要忘记注册)
package com.example.lianxidiyizmoni;
import android.app.Application;
import com.facebook.drawee.backends.pipeline.Fresco;
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
Fresco.initialize(this);
}
}
适配器
package com.example.lianxidiyizmoni.adapter;
import android.content.Context;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.lianxidiyizmoni.R;
import com.example.lianxidiyizmoni.entity.ShowBean;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import java.util.ArrayList;
import java.util.List;
public class ShowAdapter extends XRecyclerView.Adapter<ShowAdapter.HolderView> {
private List<ShowBean.shopData> list;
private Context context;
public ShowAdapter(Context context) {
list=new ArrayList<>();
this.context = context;
}
public void setList(List<ShowBean.shopData> list) {
this.list = list;
notifyDataSetChanged();
}
@NonNull
@Override
public HolderView onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view=LayoutInflater.from(context).inflate(R.layout.item,parent,false);
HolderView holderView = new HolderView(view);
return holderView;
}
@Override
public void onBindViewHolder(@NonNull HolderView holder, int position) {
Uri uri=Uri.parse(list.get(position).masterPic);
DraweeController controller=Fresco.newDraweeControllerBuilder()
.setUri(uri)
.setAutoPlayAnimations(true)
.build();
holder.image.setController(controller);
holder.name.setText(list.get(position).commodityName);
holder.price.setText(list.get(position).price);
}
@Override
public int getItemCount() {
return list.size()>0?list.size():0;
}
public class HolderView extends RecyclerView.ViewHolder{
private SimpleDraweeView image;
private TextView name,price;
public HolderView(View itemView) {
super(itemView);
image=itemView.findViewById(R.id.image);
name=itemView.findViewById(R.id.name);
price=itemView.findViewById(R.id.price);
}
}
}
itemXML
<android.support.v7.widget.CardView xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app=“http://schemas.android.com/apk/res-auto”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
app:cardCornerRadius=“20dp”
app:cardElevation=“10dp”>
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="200dp"
fresco:roundedCornerRadius="10dp"
android:layout_margin="20dp"/>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="name" />
<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:textColor="#f00"
android:text="name" />
</LinearLayout>
</android.support.v7.widget.CardView>
详情页面
package com.example.xedierzmn.activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.example.xedierzmn.R;
import com.example.xedierzmn.bean.ShowBean;
import com.example.xedierzmn.bean.ShowXiang;
import com.example.xedierzmn.contract.ShowContract;
import com.example.xedierzmn.presenter.ShowPresenter;
import com.recker.flybanner.FlyBanner;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class SHowXiangActivity extends AppCompatActivity implements ShowContract.ShowView {
private TextView name;
private TextView price;
private Button bt_gwc;
private String id;
private ShowPresenter showPresenter;
private FlyBanner banner;
private WebView web;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_xiang);
initView();
Intent intent=getIntent();
id = intent.getStringExtra("commodityId");
Toast.makeText(this,id,Toast.LENGTH_SHORT).show();
showPresenter = new ShowPresenter(this);
HashMap<String,String>params=new HashMap<>();
params.put("commodityId",id);
showPresenter.XianPresenter(params);
}
@Override
public void onSuccess(ShowBean showBean) {
}
@Override
public void onSuccessX(ShowXiang showXiang) {
String picture = showXiang.getResult().getPicture();
String[] split = picture.split("\\,");
List<String>list=new ArrayList<>();
for (int i=0;i<split.length;i++){
list.add(split[split.length-i-1]);
}
banner.setImagesUrl(list);
name.setText(showXiang.getResult().getCategoryName());
price.setText("¥:"+showXiang.getResult().getPrice()+"");
web.setWebChromeClient(new WebChromeClient());
web.loadDataWithBaseURL(null, showXiang.getResult().getDetails(), "text/html", "utf-8", null);
}
@Override
public void onFailUre(String msg) {
Toast.makeText(this,"请求失败",Toast.LENGTH_SHORT).show();
}
private void initView() {
web = (WebView) findViewById(R.id.web);
banner = (FlyBanner) findViewById(R.id.banner);
name = (TextView) findViewById(R.id.name);
price = (TextView) findViewById(R.id.price);
bt_gwc = (Button) findViewById(R.id.bt_gwc);
bt_gwc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(SHowXiangActivity.this,GwcActivity.class);
intent.putExtra("id",id);
startActivity(intent);
}
});
}
}
package com.example.xedierzmn.fragment;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.example.xedierzmn.R;
import com.example.xedierzmn.activity.SHowXiangActivity;
import com.example.xedierzmn.adapter.SosAdapter;
import com.example.xedierzmn.bean.ShowBean;
import com.example.xedierzmn.bean.ShowXiang;
import com.example.xedierzmn.contract.ShowContract;
import com.example.xedierzmn.presenter.ShowPresenter;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import java.util.HashMap;
import butterknife.BindView;
import butterknife.OnClick;
public class FragmentOne extends BaseFragment implements ShowContract.ShowView,SosAdapter.onClickLister {
private ShowPresenter showPresenter;
@BindView(R.id.xrv)
XRecyclerView xrv;
@BindView(R.id.gimg)
ImageView gimg;
@BindView(R.id.simg)
ImageView simg;
/*@BindView(R.id.ss)
Button ss;*/
@BindView(R.id.sos)
EditText sos;
private String page="1";
private String count="20";
private SosAdapter sosAdapter;
@Override
protected int getlayout() {
return R.layout.fragmentone;
}
@Override
protected void initView(View view) {
showPresenter = new ShowPresenter(this);
sosAdapter = new SosAdapter(getActivity());
sosAdapter.SosAdapter(this);
xrv.setLayoutManager(new GridLayoutManager(getActivity(),2));
xrv.setAdapter(sosAdapter);
}
@Override
protected void initData() {
}
@OnClick(R.id.simg)
public void but(View view){
String string = sos.getText().toString();
HashMap<String,String>params=new HashMap<>();
params.put("keyword",string);
params.put("page",page);
params.put("count",count);
showPresenter.SosPresenter(params);
}
@Override
public void onSuccess(ShowBean showBean) {
if (showBean!=null){
sosAdapter.setList(showBean.getResult());
}
}
@Override
public void onSuccessX(ShowXiang showXiang) {
}
@Override
public void onFailUre(String msg) {
}
@Override
public void onclick(int positions) {
Toast.makeText(getActivity(),positions+"",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getContext(), SHowXiangActivity.class);
intent.putExtra("commodityId",positions+"");
startActivity(intent);
}
}