更多动画请访问:
https://blog.csdn.net/qq_40881680/article/details/82377850
OkHttp MVP层:
https://blog.csdn.net/qq_42902175/article/details/84972040
所需权限:
//okhttp依赖
implementation 'com.squareup.okhttp3:okhttp:3.11.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.11.0'
// glide加载图片依赖
implementation 'com.github.bumptech.glide:glide:4.8.0'
//recyclerview依赖
implementation 'com.android.support:recyclerview-v7:28.0.0'
//gson
implementation 'com.google.code.gson:gson:2.8.5'
//Xre
implementation 'com.jcodecraeer:xrecyclerview:1.5.9'
build.gradle中写入 使用xrecyclerview必须写
configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
def requested = details.requested
if (requested.group == 'com.android.support') {
if (!requested.name.startsWith("multidex")) {
details.useVersion '28.0.0'//根据你的底下appcompat-v7版本
}
}
}
}
1.布局
<?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"
tools:context=".MainActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<ImageView
android:id="@+id/image"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/umeng_socialize_qq"/>
<TextView
android:id="@+id/name"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="昵称"
/>
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"/>
</LinearLayout>
<com.jcodecraeer.xrecyclerview.XRecyclerView
android:id="@+id/xrecview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
1. 多条目布局
//布局1
<?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/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sss"/>
</LinearLayout>
//布局2
<?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">
<TextView
android:id="@+id/title"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="sss"/>
<ImageView
android:id="@+id/icon1"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/ic_launcher_background"/>
</LinearLayout>
//布局3
<?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/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sss"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/icon13"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/ic_launcher_background"/>
<ImageView
android:id="@+id/icon23"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="10dp"
android:src="@drawable/ic_launcher_background"/>
<ImageView
android:id="@+id/icon33"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="10dp"
android:src="@drawable/ic_launcher_background"/>
</LinearLayout>
</LinearLayout>
2.主页面
package com.bw.ymy.recyclerview_week2;
import android.animation.ObjectAnimator;
import android.support.v4.view.PagerAdapter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import com.bw.ymy.recyclerview_week2.adapter.MyBase;
import com.bw.ymy.recyclerview_week2.bean.UserBean;
import com.bw.ymy.recyclerview_week2.presenter.PresenterImpl;
import com.bw.ymy.recyclerview_week2.vie.IView;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity implements IView {
private XRecyclerView xrecview;
private int page=1;
private PresenterImpl presenter;
private MyBase adapter;
private Button b;
private ImageView image;
//第二种
ObjectAnimator objectAnimator;
//第一种 动画 需要布局自己写效果
Animation animation;
// public static final String TYPE_TITLE="http://www.xieast.com/api/news/news.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取资源id
xrecview=findViewById(R.id.xrecview);
image=findViewById(R.id.image);
//第二种
image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
objectAnimator= ObjectAnimator.ofFloat(image, "scaleX", 1f,3f,1f);
objectAnimator.setDuration(3000);
objectAnimator.start();
}
});
//点击头像 放大在缩小 //第一种 动画
/* animation=AnimationUtils.loadAnimation(this,R.anim.animmm);
image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
image.startAnimation(animation);
}
});*/
presenter=new PresenterImpl(this);
//创建一个管理器
LinearLayoutManager layoutManager=new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
xrecview.setLayoutManager(layoutManager);
//适配器
adapter=new MyBase(this);
xrecview.setAdapter(adapter);
//是否下拉刷新
xrecview.setPullRefreshEnabled(true);
//上啦加载
xrecview.setLoadingMoreEnabled(true);
//回调
xrecview.setLoadingListener(new XRecyclerView.LoadingListener() {
@Override
public void onRefresh() {
page=1;
lodata();
}
@Override
public void onLoadMore() {
lodata();
}
});
lodata();
}
private void lodata()
{
Map<String,String> map=new HashMap<>();
map.put("page",page+"");
presenter.getRequest(RPKS.TYPE_TITLE,map,UserBean.class);
}
@Override
public void onsuccess(Object data) {
if(data instanceof UserBean)
{
UserBean userBean= (UserBean) data;
if(page==1)
{
adapter.setlist(userBean.getData());
}else
{
adapter.addlist(userBean.getData());
}
page++;
xrecview.refreshComplete();
xrecview.loadMoreComplete();
}
}
//解绑
@Override
protected void onDestroy() {
super.onDestroy();
presenter.deteach();
}
}
3.Adapter
package com.bw.ymy.recyclerview_week2.adapter;
import android.animation.ObjectAnimator;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
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.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.bw.ymy.recyclerview_week2.R;
import com.bw.ymy.recyclerview_week2.bean.UserBean;
import java.util.ArrayList;
import java.util.List;
public class MyBase extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<UserBean.DataBean> mdata;
private Context context;
public MyBase(Context context) {
mdata = new ArrayList<>();
this.context=context;
}
//栓新
public void setlist(List<UserBean.DataBean> datas)
{
mdata.clear();
mdata.addAll(datas);
notifyDataSetChanged();
}
//加载
public void addlist(List<UserBean.DataBean> datas)
{
mdata.addAll(datas);
notifyDataSetChanged();
}
// private AdapterView.OnItemClickListener onItemClickListener;
//删除
private static final int TYPE_TEXT=0;
private static final int TYPE_IMAGE=1;
private static final int TYPE_III=2;
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
RecyclerView.ViewHolder viewHolder=null;
if(i==TYPE_TEXT)
{
View view=LayoutInflater.from(context).inflate(R.layout.text_item,viewGroup,false);
viewHolder=new ViewHolderOne(view);
}else if(i==TYPE_IMAGE)
{
View view=LayoutInflater.from(context).inflate(R.layout.image_item,viewGroup,false);
viewHolder=new ViewHolderTwo(view);
}else
{
View view=LayoutInflater.from(context).inflate(R.layout.image3_item,viewGroup,false);
viewHolder=new ViewHolderThree(view);
}
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder viewHolder, final int i) {
//点击吐司
/* viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context,mdata.get(i).getTitle(),Toast.LENGTH_LONG).show();
}
});*/
//长按删除
viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(final View v) {
AlertDialog.Builder builder=new AlertDialog.Builder(context);
builder.setTitle("删除");
builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//获取删除的下标
int pos=viewHolder.getLayoutPosition();
//从数组里面删除
mdata.remove(i);
//刷新
notifyDataSetChanged();
}
});
builder.setNeutralButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
return true;
}
});
int type=getItemViewType(i);
switch (type)
{
case TYPE_TEXT:
ViewHolderOne holderOne= (ViewHolderOne) viewHolder;
holderOne.title.setText(mdata.get(i).getTitle());
break;
case TYPE_IMAGE:
final ViewHolderTwo holderTwo= (ViewHolderTwo) viewHolder;
holderTwo.title.setText(mdata.get(i).getTitle());
Glide.with(context).load(mdata.get(i).getThumbnail_pic_s()).into(holderTwo.icon1);
//点击动画
holderTwo.icon1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ObjectAnimator alpha = ObjectAnimator.ofFloat(holderTwo.icon1, "alpha", 1f, 0f,1f);
alpha.setDuration(2000);
alpha.setRepeatCount(0);
alpha.start();
}
});
break;
case TYPE_III:
final ViewHolderThree holderThree= (ViewHolderThree) viewHolder;
holderThree.title.setText(mdata.get(i).getTitle());
Glide.with(context).load(mdata.get(i).getThumbnail_pic_s02()).into(holderThree.icon13);
Glide.with(context).load(mdata.get(i).getThumbnail_pic_s()).into(holderThree.icon23);
Glide.with(context).load(mdata.get(i).getThumbnail_pic_s03()).into(holderThree.icon33);
//点击第一张图片动画 无到有
holderThree.icon13.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ObjectAnimator alpha = ObjectAnimator.ofFloat(holderThree.icon13, "alpha", 1f, 0f,1f);
alpha.setDuration(2000);
alpha.setRepeatCount(0);
alpha.start();
}
});
//点击第2张图片动画 无到有
holderThree.icon23.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ObjectAnimator alpha = ObjectAnimator.ofFloat(holderThree.icon23, "alpha", 1f, 0f,1f);
alpha.setDuration(2000);
alpha.setRepeatCount(0);
alpha.start();
}
});
//点击第3张图片动画 无到有
holderThree.icon33.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ObjectAnimator alpha = ObjectAnimator.ofFloat(holderThree.icon33, "alpha", 1f, 0f,1f);
alpha.setDuration(2000);
alpha.setRepeatCount(0);
alpha.start();
}
});
break;
default:
break;
}
}
//条目
@Override
public int getItemCount() {
return mdata.size();
}
//多条目
@Override
public int getItemViewType(int position) {
UserBean.DataBean bean=mdata.get(position);
String thumbnail_pic_s = bean.getThumbnail_pic_s();
String thumbnail_pic_s02 = bean.getThumbnail_pic_s02();
String thumbnail_pic_s03 = bean.getThumbnail_pic_s03();
if(thumbnail_pic_s!=null&&thumbnail_pic_s02==null&&thumbnail_pic_s03==null)
{
return TYPE_TEXT;
}else if(thumbnail_pic_s!=null&&thumbnail_pic_s02!=null&&thumbnail_pic_s03==null)
{
return TYPE_IMAGE;
}
else
{
return TYPE_III;
}
}
class ViewHolderOne extends RecyclerView.ViewHolder {
private TextView title;
public ViewHolderOne(@NonNull View itemView) {
super(itemView);
title=itemView.findViewById(R.id.title);
}
}
class ViewHolderTwo extends RecyclerView.ViewHolder {
private TextView title;
private ImageView icon1;
public ViewHolderTwo(@NonNull View itemView) {
super(itemView);
title=itemView.findViewById(R.id.title);
icon1=itemView.findViewById(R.id.icon1);
}
}
class ViewHolderThree extends RecyclerView.ViewHolder {
private TextView title;
private ImageView icon13,icon23,icon33;
public ViewHolderThree(@NonNull View itemView) {
super(itemView);
title=itemView.findViewById(R.id.title);
icon13=itemView.findViewById(R.id.icon13);
icon23=itemView.findViewById(R.id.icon23);
icon33=itemView.findViewById(R.id.icon33);
}
}
}
效果: