activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="horizontal" tools:context="com.bwie.fenleimodel.MainActivity"> <com.jcodecraeer.xrecyclerview.XRecyclerView android:id="@+id/xlv" android:layout_width="match_parent" android:layout_height="wrap_content"></com.jcodecraeer.xrecyclerview.XRecyclerView> </LinearLayout>
news_item_layout.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" android:padding="10dp"> <TextView android:id="@+id/title3" android:text="我是标题" android:layout_width="match_parent" android:layout_height="wrap_content"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="10dp"> <ImageView android:id="@+id/img1" android:src="@mipmap/ic_launcher" android:layout_width="0dp" android:layout_weight="1" android:scaleType="centerCrop" android:layout_height="70dp"/> <ImageView android:id="@+id/img2" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:src="@mipmap/ic_launcher" android:layout_width="0dp" android:scaleType="centerCrop" android:layout_weight="1" android:layout_height="70dp"/> <ImageView android:id="@+id/img3" android:src="@mipmap/ic_launcher" android:layout_width="0dp" android:layout_weight="1" android:scaleType="centerCrop" android:layout_height="70dp"/> </LinearLayout> </LinearLayout>
news_item2_layout.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="horizontal" android:padding="10dp"> <ImageView android:id="@+id/img1" android:src="@mipmap/ic_launcher" android:layout_width="70dp" android:scaleType="centerCrop" android:layout_height="70dp"/> <LinearLayout android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:orientation="vertical" android:layout_marginTop="10dp"> <TextView android:id="@+id/title1" android:layout_marginLeft="10dp" android:text="我是标题" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> </LinearLayout>
activity_three_color.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" tools:context="com.bwie.fenleimodel.baseview.ThreeColorActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp"> <TextView android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center" android:text="三色梯" android:textSize="20sp" /> <TextView android:id="@+id/add" android:onClick="add" android:layout_width="50dp" android:layout_height="50dp" android:text="+" android:textSize="20sp" android:gravity="center" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /> </RelativeLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#000"/> <com.bwie.fenleimodel.baseview.ThreeColorView android:id="@+id/threecolorview" android:layout_width="match_parent" android:layout_height="wrap_content"></com.bwie.fenleimodel.baseview.ThreeColorView> </LinearLayout>
View
public interface NewsView { void getNews(NewsBean newsBean); }
Model
public interface NewsModel { void getNews(int page,OnNetListener onNetListener); }
public class NewsModelImp implements NewsModel { @Override public void getNews(int page,final OnNetListener onNetListener) { Map<String,String> map=new HashMap<>(); map.put("type",page+""); OkhttpUtils.getInstance().doPost(Api.LEFT_URL,map,new OnNetListener() { @Override public void onSuccess(String result) { onNetListener.onSuccess(result); } @Override public void onFailed(Exception e) { onNetListener.onFailed(e); } }); } }
Presenter
public interface NewsPresenter { void getNews(int page); }
public class NewsPresenterImp implements NewsPresenter{ NewsView newsView; NewsModelImp newsModelImp; public NewsPresenterImp(NewsView newsView) { this.newsView = newsView; newsModelImp = new NewsModelImp(); } @Override public void getNews(int page) { newsModelImp.getNews(page,new OnNetListener() { @Override public void onSuccess(String result) { String replace = result.replace("null(", "").replace(")", ""); NewsBean newsBean = new Gson().fromJson(replace, NewsBean.class); if (newsView!=null){ newsView.getNews(newsBean); } } @Override public void onFailed(Exception e) { } }); } }
适配屏幕宽度
public class AppUtil { /** * * @param context * @return 屏幕宽度 */ public static int screenWidth(Context context){ DisplayMetrics metrics = context.getResources().getDisplayMetrics(); return metrics.widthPixels; } }
适配器
public class NewsAdapter extends XRecyclerView.Adapter<RecyclerView.ViewHolder> { Context context; List<NewsBean.DataBean> list; NewsBean newsBean; public NewsAdapter(Context context, List<NewsBean.DataBean> list) { this.context = context; this.list = list; } public void loadMore(List<NewsBean.DataBean> data){ if (list!=null) { list.addAll(data); notifyDataSetChanged(); } } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { if (viewType == Api.TYPE1) { View view = LayoutInflater.from(context).inflate(R.layout.news_item2_layout, parent, false); return new VH1(view); } else { View view = LayoutInflater.from(context).inflate(R.layout.news_item_layout, parent, false); return new VH2(view); } } @Override public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) { final NewsBean.DataBean dataBean = list.get(position); holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("删除"); builder.setNegativeButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { int pos = holder.getAdapterPosition(); list.remove(pos); notifyItemRemoved(pos); } }); builder.setNeutralButton("取消",null); builder.show(); return true; } }); if(holder instanceof VH1){ ((VH1) holder).title.setText(dataBean.getTopic()); }else if(holder instanceof VH2){ if(dataBean.getMiniimg()!=null&&dataBean.getMiniimg().size()>0){ if(dataBean.getMiniimg().size()==1){ Glide.with(context).load(dataBean.getMiniimg().get(0).getSrc()).into(((VH2) holder).iv1); Glide.with(context).load(dataBean.getMiniimg().get(0).getSrc()).into(((VH2) holder).iv2); Glide.with(context).load(dataBean.getMiniimg().get(0).getSrc()).into(((VH2) holder).iv3); } else if (dataBean.getMiniimg().size()==2) { Glide.with(context).load(dataBean.getMiniimg().get(0).getSrc()).into(((VH2) holder).iv1); Glide.with(context).load(dataBean.getMiniimg().get(1).getSrc()).into(((VH2) holder).iv2); Glide.with(context).load(dataBean.getMiniimg().get(1).getSrc()).into(((VH2) holder).iv3); }else { Glide.with(context).load(dataBean.getMiniimg().get(0).getSrc()).into(((VH2) holder).iv1); Glide.with(context).load(dataBean.getMiniimg().get(1).getSrc()).into(((VH2) holder).iv2); Glide.with(context).load(dataBean.getMiniimg().get(2).getSrc()).into(((VH2) holder).iv3); } ((VH2) holder).title.setText(dataBean.getTopic()); } } } @Override public int getItemCount() { return list.size(); } @Override public int getItemViewType(int position) { return position % 3 == 2 ? Api.TYPE1 : Api.TYPE2; } class VH1 extends XRecyclerView.ViewHolder { private TextView title; public VH1(View itemView) { super(itemView); title = itemView.findViewById(R.id.title1); } } class VH2 extends XRecyclerView.ViewHolder { private TextView title; private ImageView iv1, iv2, iv3; public VH2(View itemView) { super(itemView); iv1 = itemView.findViewById(R.id.img1); iv2 = itemView.findViewById(R.id.img2); iv3 = itemView.findViewById(R.id.img3); title = itemView.findViewById(R.id.title3); } } }
API
public interface Api { public static final String URL = "http://ttpc.dftoutiao.com/jsonpc/refresh"; public static final int TYPE1 = 1; //一张图片 public static final int TYPE2 = 3; //三张图片 }
梯形View
public class ThreeColorView extends ViewGroup { public ThreeColorView(Context context) { super(context); } public ThreeColorView(Context context, AttributeSet attrs) { super(context, attrs); } public ThreeColorView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int totalHeight = 0; //此控件的高 int totalWidth = 0; //此控件的宽 //得到View数量 int child = getChildCount(); if (child > 0) { for (int i = 0; i < child; i++) { View view = getChildAt(i); totalHeight += view.getMeasuredHeight(); measureChild(view, widthMeasureSpec, heightMeasureSpec); } } totalWidth = AppUtil.screenWidth(getContext()); //设置宽度和高度给当前view setMeasuredDimension(totalWidth, totalHeight); } @Override protected void onLayout(boolean bo, int left, int top, int right, int bottom) { int l = 0; int t = 0; int r = 0; int b = 0; int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { View view = getChildAt(i); view.layout(l, t, l + view.getMeasuredWidth(), t + view.getMeasuredHeight()); l += view.getMeasuredWidth(); t += view.getMeasuredHeight(); if (l + view.getMeasuredWidth() > AppUtil.screenWidth(getContext())) { l = 0; } final int finalI = i; view.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { TextView textView = (TextView) view; Intent intent = new Intent(getContext(), MainActivity.class); intent.putExtra("id", textView.getText().toString()); getContext().startActivity(intent); } }); view.setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View view) { removeView(view); return true; } }); } } }
梯形Activity
public class ThreeColorActivity extends AppCompatActivity implements View.OnClickListener { private ThreeColorView threeColorView; private TextView add; private int count = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_three_color); initData(); } private void initData() { threeColorView = findViewById(R.id.threecolor); add = findViewById(R.id.add); add.setOnClickListener(this); } @Override public void onClick(View view) { count++; int width = AppUtils.screenWidth(this); TextView textView = new TextView(this); textView.setText(count + ""); textView.setGravity(Gravity.CENTER); textView.setTextColor(Color.WHITE); ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(textView, "translationX", (width - width / 3), 0); objectAnimator.setDuration(1000); objectAnimator.start(); if (count == 1 || count == 4 || count == 7 || count == 10 || count == 13 || count == 16 || count == 19 || count == 22 || count == 25 || count == 28) { textView.setBackgroundColor(Color.RED); } else if (count == 2 || count == 5 || count == 8 || count == 11 || count == 14 || count == 17 || count == 20 || count == 23 || count == 26 || count == 29) { textView.setBackgroundColor(Color.GREEN); } else if (count == 3 || count == 6 || count == 9 || count == 12 || count == 15 || count == 18 || count == 21 || count == 24 || count == 27 || count == 30) { textView.setBackgroundColor(Color.BLUE); } threeColorView.addView(textView); ViewGroup.LayoutParams params = textView.getLayoutParams(); params.width = width / 3; params.height = 70; textView.setLayoutParams(params); } }
MainActivity
public class MainActivity extends AppCompatActivity implements NewsView { private int page = 5010; private XRecyclerView xRecyclerView; private boolean isRefresh = true;//判断是下啦刷新还是上啦加载 private List<NewsBean.DataBean> data; private NewsAdapter newsAdapter; private NewsPresenterImp newsPresenterImp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initData(); } private void initData() { xRecyclerView = findViewById(R.id.xlv); xRecyclerView.setLayoutManager(new LinearLayoutManager(this)); newsPresenterImp = new NewsPresenterImp(this); Intent intent = getIntent(); if (intent.getExtras() != null) { page = page + Integer.parseInt(intent.getExtras().getString("id")); } newsPresenterImp.getNews(page); xRecyclerView.setLoadingMoreEnabled(true); xRecyclerView.setPullRefreshEnabled(true); xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() { @Override public void onRefresh() { isRefresh = true; page = 5010; newsPresenterImp.getNews(page); } @Override public void onLoadMore() { isRefresh = false; page++; newsPresenterImp.getNews(page); } }); } @Override public void getNews(NewsBean newsBean) { data = newsBean.getData(); if (isRefresh) { newsAdapter = new NewsAdapter(MainActivity.this, data); xRecyclerView.setAdapter(newsAdapter); xRecyclerView.refreshComplete(); } else { if (newsAdapter != null) { newsAdapter.loadMore(newsBean.getData()); } xRecyclerView.loadMoreComplete(); } } }
判断联网请求
public class NetWorkUtils { //判断网络是否连接 public static boolean isNetWorkAvailable(Context context) { //网络连接管理器 ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); //网络信息 NetworkInfo info = connectivityManager.getActiveNetworkInfo(); if (info != null) { return true; } return false; }}
权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
依赖
compile 'com.squareup.okio:okio:1.5.0' compile 'com.squareup.okhttp3:okhttp:3.2.0' compile 'com.squareup.okhttp3:logging-interceptor:3.4.1' compile 'com.android.support:recyclerview-v7:26.0.0-alpha1' compile 'com.github.bumptech.glide:glide:3.7.0' compile 'com.google.code.gson:gson:2.2.4' compile 'com.jcodecraeer:xrecyclerview:1.3.2'