一.okUtils
public class OkHttpUtils {
//1.单例模式必须有私有构造器
private static OkHttpUtils okHttpUtils;
private final OkHttpClient okHttpClient;
private Handler myhandler;
private static final String TAG = "OkHttpUtils------";
//2.类的单例
private OkHttpUtils(){
myhandler=new Handler(Looper.getMainLooper());
okHttpClient = new OkHttpClient.Builder()
.connectTimeout(5000, TimeUnit.MILLISECONDS) //设置连接超时
.readTimeout(5000,TimeUnit.MILLISECONDS) //设置读取超时
.writeTimeout(5000, TimeUnit.MILLISECONDS)//写的时候超时
.build();
}
//3.有一个公共的方法
public static OkHttpUtils getInstance(){
if(okHttpUtils ==null){
synchronized (OkHttpUtils.class){
if(okHttpUtils ==null){
return okHttpUtils=new OkHttpUtils();
}
}
}
return okHttpUtils;
}
//5 d oGET
public void doGet(String url, final OkCallback okCallback){
//联网请求
Request request = new Request.Builder()
.get()
.url(url)
.build();
//用request对象去声明一个call对象 ,call 去执行
Call call=okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
//失败
if(okCallback !=null){
okCallback.getError(e);
}
}
@Override
public void onResponse(Call call, Response response) throws IOException {
//成功
if(response!=null && response.isSuccessful()){
if(okCallback!=null){
//解析
final String json = response.body().string();
//开启线程
myhandler.post(new Runnable() {
@Override
public void run() {
okCallback.getSuccess(json);
}
});
}
}
}
});
}
//6.doPost
public void doPost(String url, Map<String,String> map, final OkCallback okCallback){
FormBody.Builder formbody=new FormBody.Builder();
for (String key:map.keySet()){
formbody.add(key,map.get(key));
}
//生成formbody
FormBody formBody=formbody.build();
Request request=new Request.Builder()
.post(formBody)
.url(url)
.build();
Call call=okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
//失败
if(okCallback!=null){
okCallback.getError(e);
}
}
@Override
public void onResponse(Call call, Response response) throws IOException {
//成功
if(response!=null && response.isSuccessful()){
if(okCallback!=null){
final String json = response.body().string();
myhandler.post(new Runnable() {
@Override
public void run() {
okCallback.getSuccess(json);
}
});
}
}
}
});
}
//4.定义接口
public interface OkCallback{
void getError(Exception e);//失败
void getSuccess(String json);//成功
}
}
二.app
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
initImageLoader();
}
private void initImageLoader() {
//创建默认的ImageLoader 配置参数
ImageLoaderConfiguration configuration = ImageLoaderConfiguration.createDefault(this);
ImageLoader.getInstance().init(configuration);
}
public static DisplayImageOptions getOptions() {
DisplayImageOptions Options = new DisplayImageOptions.Builder()
.cacheInMemory(true) // default
.cacheOnDisk(true) // default
.bitmapConfig(Bitmap.Config.RGB_565) // default
.displayer(new SimpleBitmapDisplayer()) // default
.displayer(new FadeInBitmapDisplayer(500))
.handler(new Handler()) // default
.build();
return Options;
}
}
三,base (IView,BasePresenter,BaseActivity)
base Presenter:
public abstract class BasePresenter<V extends IView> {
protected V view;
public BasePresenter(V view) {
this.view = view;
initModel();
}
protected abstract void initModel();
void onDestory(){
view = null;
}
}
baseActivity:
public abstract class BaseActivity<P extends BasePresenter> extends AppCompatActivity{
protected P presenter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(setView());
presenter = presenterById();
initViews();
initDatas();
initListener();
}
protected abstract void initListener();
protected abstract void initDatas();
protected abstract void initViews();
protected abstract P presenterById();
protected abstract int setView();
@Override
protected void onDestroy() {
presenter.onDestory();
super.onDestroy();
}
}
四:Model
public class NewsModel {
public void newsModel(String url, final MOkCallback mOkCallback){
OkHttpUtils instance = OkHttpUtils.getInstance();
instance.doGet(url, new OkHttpUtils.OkCallback() {
@Override
public void getError(Exception e) {
//失败
if(mOkCallback!=null){
mOkCallback.getError(e);
}
}
@Override
public void getSuccess(String json) {
//成功
if(mOkCallback!=null){
mOkCallback.getSuccess(json);
}
}
});
}
public interface MOkCallback{
void getError(Exception e);//失败
void getSuccess(String json);//成功
}
}
五,Presenter
public class NewsPresenter extends BasePresenter<NewsView>{
private NewsModel newsModel;
public NewsPresenter(NewsView view) {
super(view);
}
public void newsPresenter(String url){
newsModel.newsModel(url, new NewsModel.MOkCallback() {
@Override
public void getError(Exception e) {
//失败
if(view!=null){
view.getError(e);
}
}
@Override
public void getSuccess(String json) {
//成功
if(view!=null){
view.getSuccess(json);
}
}
});
}
@Override
protected void initModel() {
newsModel = new NewsModel();
}
}
六,Activity
public class NewsActivity extends BaseActivity<NewsPresenter> implements NewsView{
private static final String TAG = "NewsActivity------";
private RecyclerView recyclerView;
private List<NewsBean.DataBeanX.DataBean> list=new ArrayList<>();
private RecyclerViewAdapter adapter;
@Override
protected void initListener() {
}
@Override
protected void initDatas() {
presenter.newsPresenter(HttpConfig.NEWS_URL);
}
@Override
protected void initViews() {
//获取资源ID
recyclerView = findViewById(R.id.RecyclerView);
}
@Override
protected NewsPresenter presenterById() {
return new NewsPresenter(this);
}
@Override
protected int setView() {
return R.layout.activity_main;
}
@Override
public void getError(Exception e) {
//失败
}
@Override
public void getSuccess(String json) {
//成功
Log.d(TAG, "成功: "+json);
Gson gson=new Gson();
NewsBean newsBean = gson.fromJson(json, NewsBean.class);
List<NewsBean.DataBeanX.DataBean> data = newsBean.getData().getData();
list.addAll(data);
Log.d(TAG, "getSuccess: "+list);
//创建适配器
adapter = new RecyclerViewAdapter(NewsActivity.this,list);
LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(adapter);
adapter.setOnItemClickListener(new RecyclerViewAdapter.OnItemClickListener() {
@Override
public void OnItemClickListener(View v, int position) {
ObjectAnimator alpha=ObjectAnimator.ofFloat(v,"alpha",1f,0f,1f);
alpha.setDuration(3000);
alpha.start();
}
});
adapter.setOnLongClickListener(new RecyclerViewAdapter.OnLongClickListener() {
@Override
public void onLongClick(View v, int position) {
list.remove(position);
adapter.notifyDataSetChanged();
}
});
}
@Override
public Context context() {
return this;
}
}
七:多条目适配器
public class RecyclerViewAdapter extends RecyclerView.Adapter{
private Context context;
private List<NewsBean.DataBeanX.DataBean> list;
private static final int one=1;
private static final int two=2;
public RecyclerViewAdapter(Context context, List<NewsBean.DataBeanX.DataBean> list) {
this.context=context;
this.list = list;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if(viewType==one){
View inflate1= LayoutInflater.from(context).inflate(R.layout.one_item,parent,false);
ViewHolder1 holder1=new ViewHolder1(inflate1);
return holder1;
}else{
View inflate2=LayoutInflater.from(context).inflate(R.layout.two_item,parent,false);
ViewHolder2 holder2=new ViewHolder2(inflate2);
return holder2;
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
if(holder instanceof ViewHolder1){
String url = "http://365jia.cn/uploads/";
ViewHolder1 viewHolder1= (ViewHolder1) holder;
viewHolder1.t1.setText(list.get(position).getTitle());
String pic1 = list.get(position).getPics().get(0);
//加载图片
ImageLoader.getInstance().displayImage(url+pic1,viewHolder1.item1_imageView, MyApp.getOptions());
viewHolder1.t3.setText(list.get(position).getViews()+"评论");
viewHolder1.t4.setText(list.get(position).getViews_label()+"赞");
//点击的时候调接口
((ViewHolder1) holder).item1_imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onItemClickListener.OnItemClickListener(v,position);
}
});
//长按的时候调接口
((ViewHolder1) holder).itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
onLongClick.onLongClick(v,position);
return false;
}
});
}else if(holder instanceof ViewHolder2){
String url = "http://365jia.cn/uploads/";
ViewHolder2 holder2= (ViewHolder2) holder;
holder2.item2_t1.setText(list.get(position).getTitle());
String i1 = list.get(position).getPics().get(0);
String i2 = list.get(position).getPics().get(1);
String i3 = list.get(position).getPics().get(2);
ImageLoader.getInstance().displayImage(url+i1,holder2.item2_image1,MyApp.getOptions());
ImageLoader.getInstance().displayImage(url+i2,holder2.item2_image2,MyApp.getOptions());
ImageLoader.getInstance().displayImage(url+i3,holder2.item2_image3,MyApp.getOptions());
((ViewHolder2) holder).item2_image1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onItemClickListener.OnItemClickListener(v,position);
}
});
((ViewHolder2) holder).item2_image2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onItemClickListener.OnItemClickListener(v,position);
}
});
((ViewHolder2) holder).item2_image3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onItemClickListener.OnItemClickListener(v,position);
}
});
((ViewHolder2) holder).item2_t1.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
onLongClick.onLongClick(v,position);
return false;
}
});
}
}
@Override
public int getItemCount() {
return list ==null ? 0:list.size();
}
@Override
public int getItemViewType(int position) {
List<String> pics=list.get(position).getPics();
if(pics.size()==one){
return one;
}
return two;
}
class ViewHolder1 extends RecyclerView.ViewHolder{
private TextView t1,t2,t3,t4;
private ImageView item1_imageView;
public ViewHolder1(View itemView) {
super(itemView);
t1=itemView.findViewById(R.id.tv1_title1);
t3=itemView.findViewById(R.id.tv1_title3);
t4=itemView.findViewById(R.id.tv1_title4);
item1_imageView=itemView.findViewById(R.id.img1_image1);
}
}
class ViewHolder2 extends RecyclerView.ViewHolder{
private TextView item2_t1,item2_t2;
private ImageView item2_image1,item2_image2,item2_image3;
public ViewHolder2(View itemView) {
super(itemView);
item2_t1=itemView.findViewById(R.id.tv2_title1);
item2_image1=itemView.findViewById(R.id.image1);
item2_image2=itemView.findViewById(R.id.image2);
item2_image3=itemView.findViewById(R.id.image3);
}
}
//长按
private OnLongClickListener onLongClick;
public void setOnLongClickListener(OnLongClickListener onLongClickListener) {
this.onLongClick = onLongClickListener;
}
public interface OnLongClickListener{
void onLongClick(View v,int position);
}
//点击条目
private OnItemClickListener onItemClickListener;
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
public interface OnItemClickListener{
void OnItemClickListener(View v,int position);
}
}