//Mainactivity
public class MainActivity extends AppCompatActivity implements IView, View.OnClickListener{
private XRecyclerView xRecyclerView,gxRecyclerView;
private MyXecyAdapter adapter;
private int page;
private Button imageView;
private int mCount=2;
private int i=0;
private Presenterimpl presenterimpl;
private String path="http://www.zhaoapi.cn/product/searchProducts";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
presenterimpl=new Presenterimpl(this);
gxRecyclerView=findViewById(R.id.activity_gxrecy);
xRecyclerView=findViewById(R.id.activity_xrecy);
page=1;
adapter=new MyXecyAdapter(this);
imageView=findViewById(R.id.action_image);
imageView.setOnClickListener(this);
adapter.setCilck(new MyXecyAdapter.Cilck() {
@Override
public void Cilckitem(int position) {
adapter.setRemove(position);
}
});
inif();
ginif();
}
private void ginif() {
//布局管理器
final LinearLayoutManager gmanager=new LinearLayoutManager(this);
gmanager.setOrientation(OrientationHelper.VERTICAL);
gxRecyclerView .setLayoutManager(gmanager);
//实例化适配器
gxRecyclerView.setAdapter(adapter);
//支持刷新加载
gxRecyclerView.setPullRefreshEnabled(true);
gxRecyclerView.setLoadingMoreEnabled(true);
gxRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
@Override
public void onRefresh() {
page=1;
load();
gxRecyclerView.refreshComplete();
}
@Override
public void onLoadMore() {
load();
gxRecyclerView.loadMoreComplete();
}
});
load();
}
private void inif() {
//布局管理器
GridLayoutManager manager=new GridLayoutManager(this,mCount);
manager.setOrientation(OrientationHelper.VERTICAL);
xRecyclerView.setLayoutManager(manager);
//实例化适配器
xRecyclerView.setAdapter(adapter);
//支持刷新加载
xRecyclerView.setPullRefreshEnabled(true);
xRecyclerView.setLoadingMoreEnabled(true);
xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
@Override
public void onRefresh() {
page=1;
load();
xRecyclerView.refreshComplete();
}
@Override
public void onLoadMore() {
load();
xRecyclerView.loadMoreComplete();
}
});
load();
}
private void load() {
Map<String,String> mar=new HashMap<>();
mar.put("keywords","手机");
mar.put("page",page+"");
presenterimpl.setRequestData(path,mar,Bean.class);
}
@Override
public void onClick(View v) {
int id = v.getId();
switch (id){
case R.id.action_image:
if (i%2==0){
xRecyclerView.setVisibility(View.VISIBLE);
gxRecyclerView.setVisibility(View.GONE);
}else {
xRecyclerView.setVisibility(View.GONE);
gxRecyclerView.setVisibility(View.VISIBLE);
}
i++;
break;
default:break;
}
}
@Override
public void setData(Object data) {
Bean bean= (Bean) data;
if (page==1){
adapter.setList(bean.getData());
}else {
adapter.addList(bean.getData());
}
page++;
}
}
//OkHttp
public class OkHttpUtils {
private Handler handler=new Handler(Looper.getMainLooper());
private OkHttpClient mCilent;
//单例
private static volatile OkHttpUtils Instance;
public static OkHttpUtils getInstance(){
if (Instance==null){
synchronized (OkHttpUtils.class){
Instance=new OkHttpUtils();
}
}
return Instance;
}
public OkHttpUtils(){
HttpLoggingInterceptor interceptor=new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
mCilent=new OkHttpClient.Builder()
.readTimeout(10,TimeUnit.SECONDS)
.connectTimeout(10,TimeUnit.SECONDS)
.readTimeout(10,TimeUnit.SECONDS)
.addInterceptor(interceptor)
.build();
}
//post异步请求
public void PostEnqueue(String path, Map<String,String> mar, final Class clazz, final ICallback iCallback){
FormBody.Builder builder=new FormBody.Builder();
for (Map.Entry<String,String> entry:mar.entrySet()) {
builder.add(entry.getKey(),entry.getValue());
}
RequestBody body=builder.build();
Request request=new Request.Builder()
.url(path)
.post(body)
.build();
Call call=mCilent.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
handler.post(new Runnable() {
@Override
public void run() {
iCallback.setFills(e);
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String json = response.body().string();
Gson gson=new Gson();
final Object o = gson.fromJson(json, clazz);
handler.post(new Runnable() {
@Override
public void run() {
iCallback.setSuccess(o);
}
});
}
});
}
}
Model
public interface Model {
void setRequestData(String path, Map<String,String> mar, Class clazz, MyCallback myCallback);
}
Modelimpl
public class Modelimpl implements Model{
@Override
public void setRequestData(String path, Map<String, String> mar, Class clazz, final MyCallback myCallback) {
OkHttpUtils.getInstance().PostEnqueue(path, mar, clazz, new ICallback() {
@Override
public void setSuccess(Object Obj) {
myCallback.setData(Obj);
}
@Override
public void setFills(Exception ex) {
myCallback.setData(ex);
}
});
}
}
Presenter
public interface Preenter {
void setRequestData(String path, Map<String,String> mar,Class clazz);
}
Presenterimpl
public class Presenterimpl implements Preenter{
private Modelimpl modelimpl;
private IView iView;
public Presenterimpl(IView iView) {
this.iView = iView;
modelimpl=new Modelimpl();
}
@Override
public void setRequestData(String path, Map<String, String> mar, Class clazz) {
modelimpl.setRequestData(path, mar, clazz, new MyCallback() {
@Override
public void setData(Object data) {
iView.setData(data);
}
});
}
public void onDel(){
if (iView!=null){
iView=null;
}
if (modelimpl!=null){
modelimpl=null;
}
}
}
Callback
public interface ICallback {
void setSuccess(Object Obj);
void setFills(Exception ex);
}
public interface MyCallback {
void setData(Object data);
}
APP
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
JPushInterface.setDebugMode(true);
JPushInterface.init(this);
}
}
MyService
public class MyService extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(JPushInterface.ACTION_NOTIFICATION_OPENED)){
Intent intent1=new Intent(context,MainActivity.class);
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent1);
}
}
}
MyBase
public class MyXecyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<Bean.DataBean> list;
private Context context;
private boolean isLinear;
public MyXecyAdapter(Context context){
this.context=context;
list=new ArrayList<>();
}
public void setList(List<Bean.DataBean> lists) {
list.clear();
list.addAll(lists);
notifyDataSetChanged();
}
public void addList(List<Bean.DataBean> lists) {
list.addAll(lists);
notifyDataSetChanged();
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
/* if (isLinear){*/
View view=LayoutInflater.from(context).inflate(R.layout.item,viewGroup,false);
return new ViewHolder(view);
/* }else {
View view=LayoutInflater.from(context).inflate(R.layout.gitem,viewGroup,false);
return new ViewHolder(view);
}*/
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {
ViewHolder holder= (ViewHolder) viewHolder;
holder.textView.setText(list.get(i).getTitle());
holder.price.setText(list.get(i).getPrice()+"");
String images = list.get(i).getImages();
String[] split = images.split("\\|");
List<String> mlist = Arrays.asList(split);
Glide.with(context).load(mlist.get(0)).into(holder.imageView);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cilck.Cilckitem(i);
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private ImageView imageView;
private TextView textView,price;
public ViewHolder(@NonNull View itemView) {
super(itemView);
imageView=itemView.findViewById(R.id.image);
textView=itemView.findViewById(R.id.text);
price=itemView.findViewById(R.id.price);
}
}
public void setRemove(int position){
list.remove(position);
notifyDataSetChanged();
notifyItemRemoved(position);
}
private Cilck cilck;
public void setCilck(Cilck cilck) {
this.cilck = cilck;
}
public interface Cilck{
void Cilckitem(int position);
}
}