activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/rv"/> </android.support.constraint.ConstraintLayout>
images_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/images_title" android:layout_width="match_parent" android:layout_height="wrap_content" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:layout_marginTop="10dp" android:orientation="horizontal"> <ImageView android:id="@+id/images01" android:layout_width="0dp" android:layout_height="100dp" android:layout_weight="1" android:src="@mipmap/ic_launcher" /> <ImageView android:id="@+id/images02" android:layout_width="0dp" android:layout_height="100dp" android:layout_weight="1" android:src="@mipmap/ic_launcher" /> <ImageView android:id="@+id/images03" android:layout_width="0dp" android:layout_height="100dp" android:layout_weight="1" android:src="@mipmap/ic_launcher" /> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="0dp" android:layout_gravity="right" android:layout_weight="1" android:gravity="center" android:text="1256评论 120赞" /> </LinearLayout>
normal_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="100dp" android:orientation="horizontal"> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="3" android:orientation="vertical"> <TextView android:id="@+id/normal_title" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <TextView android:layout_width="wrap_content" android:layout_height="0dp" android:layout_gravity="right" android:layout_weight="1" android:gravity="center" android:text="1256评论 120赞" /> </LinearLayout> <ImageView android:id="@+id/normal_img" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> </LinearLayout>
MainActivity
package b.com.zhoukaolx3; import android.animation.ObjectAnimator; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; import java.util.List; import b.com.zhoukaolx3.model.DataBeans; import b.com.zhoukaolx3.presenter.DataPresenterImp; public class MainActivity extends AppCompatActivity implements DataView { private RecyclerView mRv; private DataPresenterImp dataPresenterImp; private MyRvAdapter myRvAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mRv = findViewById(R.id.rv); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false); mRv.setLayoutManager(linearLayoutManager); dataPresenterImp = new DataPresenterImp(this); dataPresenterImp.showData("1"); } @Override public void showData(List<DataBeans.DataBeanX.DataBean> list) { //适配器 myRvAdapter = new MyRvAdapter(this, list); mRv.addItemDecoration(new SpacesItemDecoration(10)); mRv.setAdapter(myRvAdapter); myRvAdapter.setOnItemClick(new MyRvAdapter.OnItemClick() { @Override public void onImageClick(View view) { ObjectAnimator alpha = ObjectAnimator.ofFloat(view, "alpha", new float[]{1f, 0.5f, 0.0f, 0.5f, 1.0f}); alpha.setRepeatMode(ObjectAnimator.RESTART); alpha.setDuration(2000); alpha.setRepeatCount(0); alpha.start(); } @Override public void onItemClick(int position) { //弹出框 showAlertDialog(position); } }); } private void showAlertDialog(final int position) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("提示:"); builder.setMessage("请问你要删除吗?"); builder.setIcon(R.mipmap.ic_launcher_round); //点击对话框以外的区域是否让对话框消失 builder.setCancelable(false); //设置正面按钮 builder.setPositiveButton("是的", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { myRvAdapter.deleteItem(position); myRvAdapter.notifyDataSetChanged(); dialog.dismiss(); } }); //设置反面按钮 builder.setNegativeButton("不是", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); AlertDialog dialog = builder.create(); dialog.show(); } @Override protected void onDestroy() { super.onDestroy(); dataPresenterImp.cloneView(); } }
MyRvAdapter
package b.com.zhoukaolx3; import android.content.Context; 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.ImageView; import android.widget.TextView; import com.bumptech.glide.Glide; import java.util.List; import b.com.zhoukaolx3.model.DataBeans; public class MyRvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private String imageSplice = "http://365jia.cn/uploads/"; private Context context; private List<DataBeans.DataBeanX.DataBean> list; private LayoutInflater inflater; private final static int NORMAL = 1; private final static int IMAGES = 2; public MyRvAdapter(Context context, List<DataBeans.DataBeanX.DataBean> list) { this.context = context; this.list = list; inflater = LayoutInflater.from(context); } @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { RecyclerView.ViewHolder holder; if(viewType == NORMAL){ View view = inflater.inflate(R.layout.normal_item,parent,false); holder = new NormalViewHolder(view); }else if(viewType == IMAGES){ View view = inflater.inflate(R.layout.images_item,parent,false); holder = new ImagesViewHolder(view); }else{ holder = null; } return holder; } @Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) { int itemViewType = getItemViewType(position); DataBeans.DataBeanX.DataBean dataBean = list.get(position); if(itemViewType == NORMAL){ NormalViewHolder normalViewHolder = (NormalViewHolder) holder; normalViewHolder.normal_title.setText(dataBean.getTitle()); Glide.with(context).load(imageSplice+dataBean.getPics().get(0)).into(normalViewHolder.normal_img); normalViewHolder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(mOnItemClick != null){ mOnItemClick.onItemClick(position); } } }); }else if(itemViewType == IMAGES){ ImagesViewHolder imagesViewHolder = (ImagesViewHolder) holder; imagesViewHolder.images_title.setText(dataBean.getTitle()); Glide.with(context).load(imageSplice+dataBean.getPics().get(0)).into(imagesViewHolder.images01); Glide.with(context).load(imageSplice+dataBean.getPics().get(1)).into(imagesViewHolder.images02); Glide.with(context).load(imageSplice+dataBean.getPics().get(2)).into(imagesViewHolder.images03); imagesViewHolder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(mOnItemClick != null){ mOnItemClick.onItemClick(position); } } }); } } @Override public int getItemCount() { return list.size(); } @Override public int getItemViewType(int position) { String type = list.get(position).getType(); if("images".equals(type)){ return IMAGES; }else{ return NORMAL; } } class NormalViewHolder extends RecyclerView.ViewHolder { private final TextView normal_title; private final ImageView normal_img; public NormalViewHolder(final View itemView) { super(itemView); normal_title = itemView.findViewById(R.id.normal_title); normal_img = itemView.findViewById(R.id.normal_img); normal_img.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(mOnItemClick != null){ mOnItemClick.onImageClick(v); } } }); } } class ImagesViewHolder extends RecyclerView.ViewHolder{ private final ImageView images01; private final ImageView images02; private final ImageView images03; private final TextView images_title; public ImagesViewHolder(View itemView) { super(itemView); images01 = itemView.findViewById(R.id.images01); images02 = itemView.findViewById(R.id.images02); images03 = itemView.findViewById(R.id.images03); images_title = itemView.findViewById(R.id.images_title); images01.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(mOnItemClick != null){ mOnItemClick.onImageClick(v); } } }); images02.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(mOnItemClick != null){ mOnItemClick.onImageClick(v); } } }); images03.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(mOnItemClick != null){ mOnItemClick.onImageClick(v); } } }); } } interface OnItemClick{ void onImageClick(View view); void onItemClick(int position); } private OnItemClick mOnItemClick; public void setOnItemClick( OnItemClick onItemClick){ this.mOnItemClick = onItemClick; } //删除 public void deleteItem(int poistion){ list.remove(poistion); } }
SpacesItemDecoration
package b.com.zhoukaolx3; import android.graphics.Rect; import android.support.v7.widget.RecyclerView; import android.view.View; public class SpacesItemDecoration extends RecyclerView.ItemDecoration { private int space; public SpacesItemDecoration(int space) { this.space = space; } @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { outRect.left = space; outRect.right = space; outRect.bottom = space; // Add top margin only for the first item to avoid double space between items if (parent.getChildPosition(view) == 0) outRect.top = space; } }
DataView
package b.com.zhoukaolx3; import java.util.List; import b.com.zhoukaolx3.model.DataBeans; public interface DataView { void showData(List<DataBeans.DataBeanX.DataBean> list); }
DataBeans
private int httpStatusCode; private int code; private DataBeanX data; public static DataBeans objectFromData(String str) { return new Gson().fromJson(str, DataBeans.class); } public static DataBeans objectFromData(String str, String key) { try { JSONObject jsonObject = new JSONObject(str); return new Gson().fromJson(jsonObject.getString(str), DataBeans.class); } catch (JSONException e) { e.printStackTrace(); } return null; } public int getHttpStatusCode() { return httpStatusCode; } public void setHttpStatusCode(int httpStatusCode) { this.httpStatusCode = httpStatusCode; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } public DataBeanX getData() { return data; } public void setData(DataBeanX data) { this.data = data; } public static class DataBeanX {
private int page; private int perpage; private int max_page; private int total; private ExtrasBean extras; private String bindtips; private List<DataBean> data; public static DataBeanX objectFromData(String str) { return new Gson().fromJson(str, DataBeanX.class); } public static DataBeanX objectFromData(String str, String key) { try { JSONObject jsonObject = new JSONObject(str); return new Gson().fromJson(jsonObject.getString(str), DataBeanX.class); } catch (JSONException e) { e.printStackTrace(); } return null; } public int getPage() { return page; } public void setPage(int page) { this.page = page; } public int getPerpage() { return perpage; } public void setPerpage(int perpage) { this.perpage = perpage; } public int getMax_page() { return max_page; } public void setMax_page(int max_page) { this.max_page = max_page; } public int getTotal() { return total; } public void setTotal(int total) { this.total = total; } public ExtrasBean getExtras() { return extras; } public void setExtras(ExtrasBean extras) { this.extras = extras; } public String getBindtips() { return bindtips; } public void setBindtips(String bindtips) { this.bindtips = bindtips; } public List<DataBean> getData() { return data; } public void setData(List<DataBean> data) { this.data = data; } public static class ExtrasBean { private List<SliderBean> slider; public static ExtrasBean objectFromData(String str) { return new Gson().fromJson(str, ExtrasBean.class); } public static ExtrasBean objectFromData(String str, String key) { try { JSONObject jsonObject = new JSONObject(str); return new Gson().fromJson(jsonObject.getString(str), ExtrasBean.class); } catch (JSONException e) { e.printStackTrace(); } return null; } public List<SliderBean> getSlider() { return slider; } public void setSlider(List<SliderBean> slider) { this.slider = slider; } public static class SliderBean { /** * id : 3541393 * pic : 18/0526/5b08a55aa0724.jpg * link : lsapp://cn.ahurls.news/news?id=3541393 * t : url * inner_news : 1 * title : 来安徽旅游这5条精品路线不可错过 * type : nomal * type_sign : */ private int id; private String pic; private String link; private String t; private int inner_news; private String title; private String type; private String type_sign; public static SliderBean objectFromData(String str) { return new Gson().fromJson(str, SliderBean.class); } public static SliderBean objectFromData(String str, String key) { try { JSONObject jsonObject = new JSONObject(str); return new Gson().fromJson(jsonObject.getString(str), SliderBean.class); } catch (JSONException e) { e.printStackTrace(); } return null; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getPic() { return pic; } public void setPic(String pic) { this.pic = pic; } public String getLink() { return link; } public void setLink(String link) { this.link = link; } public String getT() { return t; } public void setT(String t) { this.t = t; } public int getInner_news() { return inner_news; } public void setInner_news(int inner_news) { this.inner_news = inner_news; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getType_sign() { return type_sign; } public void setType_sign(String type_sign) { this.type_sign = type_sign; } } } public static class DataBean { /** * id : 3541397 * title : 合肥新定位为全国智能家居研发生产中心 四大件产量连续多年居全国之首 * link : lsapp://cn.ahurls.news/news?id=3541397 * pics : ["appletrecommend/201805/5b08a30fa4712.jpg"] * t : url * type : normal * alias : headline * pic_amount : 0 * style : single_right * comment_amount : 0 * source : * type_sign : * inner_news : 1 * views : 162 * comment_amount_label : 0 * views_label : 162 * pics_new : ["appletrecommend/201805/5b08a3fb804d1.jpg"] */ private int id; private String title; private String link; private String t; private String type; private String alias; private int pic_amount; private String style; private int comment_amount; private String source; private String type_sign; private int inner_news; private int views; private String comment_amount_label; private String views_label; private List<String> pics; private List<String> pics_new; public static DataBean objectFromData(String str) { return new Gson().fromJson(str, DataBean.class); } public static DataBean objectFromData(String str, String key) { try { JSONObject jsonObject = new JSONObject(str); return new Gson().fromJson(jsonObject.getString(str), DataBean.class); } catch (JSONException e) { e.printStackTrace(); } return null; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getLink() { return link; } public void setLink(String link) { this.link = link; } public String getT() { return t; } public void setT(String t) { this.t = t; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getAlias() { return alias; } public void setAlias(String alias) { this.alias = alias; } public int getPic_amount() { return pic_amount; } public void setPic_amount(int pic_amount) { this.pic_amount = pic_amount; } public String getStyle() { return style; } public void setStyle(String style) { this.style = style; } public int getComment_amount() { return comment_amount; } public void setComment_amount(int comment_amount) { this.comment_amount = comment_amount; } public String getSource() { return source; } public void setSource(String source) { this.source = source; } public String getType_sign() { return type_sign; } public void setType_sign(String type_sign) { this.type_sign = type_sign; } public int getInner_news() { return inner_news; } public void setInner_news(int inner_news) { this.inner_news = inner_news; } public int getViews() { return views; } public void setViews(int views) { this.views = views; } public String getComment_amount_label() { return comment_amount_label; } public void setComment_amount_label(String comment_amount_label) { this.comment_amount_label = comment_amount_label; } public String getViews_label() { return views_label; } public void setViews_label(String views_label) { this.views_label = views_label; } public List<String> getPics() { return pics; } public void setPics(List<String> pics) { this.pics = pics; } public List<String> getPics_new() { return pics_new; } public void setPics_new(List<String> pics_new) { this.pics_new = pics_new; } } } }DataModel
package b.com.zhoukaolx3.model; import b.com.zhoukaolx3.net.OnNetListener; public interface DataModel { void showData(String page, OnNetListener onNetListener); }