public class MainActivity extends AppCompatActivity implements Coregoods {
@BindView(R.id.but_zh)
ImageView butZh;
@BindView(R.id.check_zh)
CheckBox checkZh;
@BindView(R.id.check_xl)
CheckBox checkXl;
@BindView(R.id.check_jg)
CheckBox checkJg;
@BindView(R.id.check_sx)
CheckBox checkSx;
@BindView(R.id.xrecycler)
XRecyclerView xrecycler;
ArrayList<Goods> list = new ArrayList<>();
private GoodsAdapter goodsAdapter;
private Presentergoods presentergoods;
private GridLayoutManager gridLayoutManager;
private LinearLayoutManager linearlayoutManager;
int page =1;
String phone = "手机";
int sort = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
//线性布局
linearlayoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL, false);
//设置默认布局
xrecycler.setLayoutManager(linearlayoutManager);
//网格布局
gridLayoutManager = new GridLayoutManager(this,2,LinearLayoutManager.VERTICAL, false);
presentergoods = new Presentergoods(this);
getpage(page,phone,sort);
checkXl.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (checkXl.isChecked()){
list.clear();
getpage(1,phone,1);
}else {
list.clear();
getpage(1,phone,0);
}
}
});
}
private void getpage(int page,String phone,int sort) {
presentergoods.getgoodsMsg(page,phone,sort);
}
Boolean isgrild = false;
@OnClick({R.id.but_zh})
public void onViewClicked(View view) {
if (!isgrild)
{
isgrild = true;
goodsAdapter.setViewType(GoodsAdapter.GRID_VIEW);
xrecycler.setLayoutManager(gridLayoutManager);
}
else {
isgrild = false;
goodsAdapter.setViewType(GoodsAdapter.LINEAR_VIEW);
xrecycler.setLayoutManager(linearlayoutManager);
}
goodsAdapter.notifyDataSetChanged();
}
@Override
public void GoodsSuccess(Result result) {
final List<Goods> data = (List<Goods>) result.getData();
list.addAll(data);
goodsAdapter = new GoodsAdapter(this);
goodsAdapter.add(list);
xrecycler.setAdapter(goodsAdapter);
//上下拉
xrecycler.setLoadingListener(new XRecyclerView.LoadingListener() {
@Override
public void onRefresh() {
getpage(1,phone,sort);
page = 1;
list.clear();
xrecycler.refreshComplete();
}
@Override
public void onLoadMore() {
page++;
getpage(page,phone,sort);
list.addAll(data);
xrecycler.loadMoreComplete();
}
});
}
@Override
public void GoodError(Result result) {
}
@Override
protected void onDestroy() {
super.onDestroy();
presentergoods.unPresentergoods();
}
}
Presenter 层
public class Presentergoods {
private static Coregoods coregoods;
public Presentergoods(Coregoods coregoods) {
this.coregoods = coregoods;
}
public void unPresentergoods() {
this.coregoods = null;
}
public static void getgoodsMsg(int page, String phone, int sort) {
new GoodsAsyncTask().execute(page,phone,sort);
}
private static class GoodsAsyncTask extends AsyncTask<Object,Void,Result> {
@Override
protected Result doInBackground(Object... strings) {
Result goodsData = ModelGoods.getGoodsData(strings[0],strings[1],strings[2]);
return goodsData;
}
@Override
protected void onPostExecute(Result result) {
super.onPostExecute(result);
if (result.getCode().equals("0"))
{
coregoods.GoodsSuccess(result);
}
else {
coregoods.GoodError(result);
}
}
}
在Model层
public class ModelGoods {
public static Result getGoodsData(Object page,Object phone,Object sort) {
String urlString ="http://www.zhaoapi.cn/product/searchProducts?keywords="+phone+"&page="+page+"&sort="+sort;
HttpUtils httpUtils = new HttpUtils();
String json = httpUtils.get(urlString);
Gson gson = new Gson();
Type type = new TypeToken<Result<List<Goods>>>(){}.getType();
Result result = gson.fromJson(json, type);
return result;
}
}
在Adapter层
public class GoodsAdapter extends XRecyclerView.Adapter<GoodsAdapter.VH>{
public Context context;
public ArrayList<Goods> lists = new ArrayList<>();
public final static int LINEAR_VIEW = 0;//线性
public final static int GRID_VIEW = 1;//网格
int ViewType = LINEAR_VIEW;//设置默认线性布局
@Override
public int getItemViewType(int position) {
return ViewType;
}
//设置布局
public void setViewType(int viewType) {
ViewType = viewType;
}
public GoodsAdapter(Context context) {
this.context = context;
}
@NonNull
@Override
public VH onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = null;
if (ViewType == LINEAR_VIEW)
{
view = View.inflate(context, R.layout.linear_item,null);
}
else {
view = View.inflate(context, R.layout.grid_item, null);
}
return new VH(view);
}
@Override
public void onBindViewHolder(@NonNull VH vh, int i) {
// Log.e("QWQWQWQW","-----------"+showbean);
vh.textview.setText(lists.get(i).getTitle()+"");
//由于我们的数据图片提供的不标准,所以我们需要切割得到图片
//将Https转换成http
String replace = lists.get(i).getImages().replace("https", "http");
Log.e("===============",replace+"");
//分割字符串
String[] split = replace.split("!");
//分割出来的是多张图片 下标为0就ok
Glide.with(context).load(split[0]).into(vh.imageview);
}
@Override
public int getItemCount() {
return lists.size();
}
public void add(ArrayList<Goods> list) {
if (list != null)
{
lists.addAll(list);
}
}
public class VH extends RecyclerView.ViewHolder {
private final TextView textview;
private final ImageView imageview;
public VH(@NonNull View itemView) {
super(itemView);
textview = itemView.findViewById(R.id.textView);
imageview = itemView.findViewById(R.id.imageView);
}
}
在HttpUtils 层 工具类
public class HttpUtils {
private static HttpUtils httpUtils = new HttpUtils();
public HttpUtils() {
}
public static HttpUtils getHttpUtils() {
synchronized (httpUtils)
{
if (httpUtils == null)
{
httpUtils = new HttpUtils();
}
return httpUtils;
}
}
public String get(String urlString) {
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url(urlString).get().build();
try {
Response response = okHttpClient.newCall(request).execute();
return response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
接口
public interface Coregoods {
void GoodsSuccess(Result result);
void GoodError(Result result);
}
WebView点击跳转网页
public class WebActivity extends AppCompatActivity {
WebView mWebView;
@SuppressLint("JavascriptInterface")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
mWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = mWebView.getSettings();
// 设置与Js交互的权限
webSettings.setJavaScriptEnabled(true);
// 设置允许JS弹窗
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
String url = getIntent().getStringExtra("url");
Log.i("dt", url);
//如果不设置WebViewClient,请求会跳转系统浏览器
mWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//该方法在Build.VERSION_CODES.LOLLIPOP以前有效,从Build.VERSION_CODES.LOLLIPOP起,建议使用shouldOverrideUrlLoading(WebView, WebResourceRequest)} instead
//返回false,意味着请求过程里,不管有多少次的跳转请求(即新的请求地址),均交给webView自己处理,这也是此方法的默认处理
//返回true,说明你自己想根据url,做新的跳转,比如在判断url符合条件的情况下
return false;
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
//返回false,意味着请求过程里,不管有多少次的跳转请求(即新的请求地址),均交给webView自己处理,这也是此方法的默认处理
//返回true,说明你自己想根据url,做新的跳转,比如在判断url符合条件的情况下
return false;
}
});
// 先载入JS代码
// 格式规定为:file:///android_asset/文件名.html
mWebView.loadUrl(url);
// webview只是载体,内容的渲染需要使用webviewChromClient类去实现
// 通过设置WebChromeClient对象处理JavaScript的对话框
//设置响应js 的Alert()函数
mWebView.setWebChromeClient(new WebChromeClient());
}