依赖
implementation 'com.jcodecraeer:xrecyclerview:1.5.9'
布局
适配器
public class RecyclerAdapter extends RecyclerView.Adapter {
private List list;
private Context context;
private static final int ONEIMAGE=0;
private static final int THREEIMAGE=ONEIMAGE+1;
public RecyclerAdapter(Context context) {
this.context = context;
list=new ArrayList<>();
}
public void setList(List list) {
this.list.clear();
this.list.addAll(list);
notifyDataSetChanged();
}
public void addList(List list) {
this.list.addAll(list);
notifyDataSetChanged();
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
RecyclerView.ViewHolder holder=null;
if(i==ONEIMAGE){
View view= LayoutInflater.from(context).inflate(R.layout.oneimage_item,viewGroup,false);
holder = new ViewHolderOneImage(view);
}else {
View view= LayoutInflater.from(context).inflate(R.layout.threeimage_item,viewGroup,false);
holder = new ViewHolderThreeImage(view);
}
return holder;
}
@Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder viewHolder, final int i) {
final int itemViewType = getItemViewType(i);
switch (itemViewType){
case ONEIMAGE:
final ViewHolderOneImage oneImage= (ViewHolderOneImage) viewHolder;
oneImage.textView_title_one.setText(list.get(i).getTitle());
ImageLoader.getInstance().displayImage(list.get(i).getThumbnail_pic_s(),oneImage.imageView_one);
oneImage.imageView_one.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animator_image(oneImage.imageView_one,i);
}
});
break;
case THREEIMAGE:
final ViewHolderThreeImage threeImage= (ViewHolderThreeImage) viewHolder;
threeImage.textView_title_three.setText(list.get(i).getTitle());
ImageLoader.getInstance().displayImage(list.get(i).getThumbnail_pic_s(),threeImage.imageView1);
ImageLoader.getInstance().displayImage(list.get(i).getThumbnail_pic_s02(),threeImage.imageView2);
ImageLoader.getInstance().displayImage(list.get(i).getThumbnail_pic_s03(),threeImage.imageView3);
threeImage.imageView1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animator_image(threeImage.imageView1,i);
}
});
threeImage.imageView2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animator_image(threeImage.imageView2,i);
}
});
threeImage.imageView3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animator_image(threeImage.imageView3,i);
}
});
break;
default:
break;
}
viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if(longitemClickListen!=null){
longitemClickListen.delItem(i);
}
return true;
}
});
}
@Override
public int getItemCount() {
return list.size();
}
@Override
public int getItemViewType(int position) {
return list.get(position).hasImage() ?ONEIMAGE : THREEIMAGE ;
}
static class ViewHolderOneImage extends RecyclerView.ViewHolder {
public TextView textView_title_one;
public ImageView imageView_one;
public ConstraintLayout constraintLayout;
public ViewHolderOneImage(@NonNull View itemView) {
super(itemView);
textView_title_one=itemView.findViewById(R.id.textView_title_one);
imageView_one=itemView.findViewById(R.id.imageView_one);
constraintLayout=itemView.findViewById(R.id.content_layout);
}
}
static class ViewHolderThreeImage extends RecyclerView.ViewHolder{
public TextView textView_title_three;
public ImageView imageView1,imageView2,imageView3;
public ConstraintLayout constraintLayout;
public ViewHolderThreeImage(@NonNull View itemView) {
super(itemView);
textView_title_three=itemView.findViewById(R.id.textView_title_three);
imageView1=itemView.findViewById(R.id.imageView1);
imageView2=itemView.findViewById(R.id.imageView2);
imageView3=itemView.findViewById(R.id.imageView3);
constraintLayout=itemView.findViewById(R.id.content_layout2);
}
}
private OnLongitemClickListen longitemClickListen;
public void setOnLongitemClickListen(OnLongitemClickListen itemClickListen){
longitemClickListen=itemClickListen;
}
//条目点击事件
public interface OnLongitemClickListen{
void delItem(int position);
}
//删除
public void delData(int position){
list.remove(position);
notifyDataSetChanged();
}
public void animator_image(View view,int position){
ObjectAnimator animator=ObjectAnimator.ofFloat(view,"alpha",1f,0f,1f);
animator.setDuration(2000);
animator.start();
}
}
bean类
public class NewsBean {
private String msg;
private int code;
private List data;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public List getData() {
return data;
}
public void setData(List data) {
this.data = data;
}
public static class DataBean {
private String uniquekey;
private String title;
private String date;
private String category;
private String author_name;
private String url;
private String thumbnail_pic_s;
private String thumbnail_pic_s02;
private String thumbnail_pic_s03;
public String getUniquekey() {
return uniquekey;
}
public void setUniquekey(String uniquekey) {
this.uniquekey = uniquekey;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getAuthor_name() {
return author_name;
}
public void setAuthor_name(String author_name) {
this.author_name = author_name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getThumbnail_pic_s() {
return thumbnail_pic_s;
}
public void setThumbnail_pic_s(String thumbnail_pic_s) {
this.thumbnail_pic_s = thumbnail_pic_s;
}
public String getThumbnail_pic_s02() {
return thumbnail_pic_s02;
}
public void setThumbnail_pic_s02(String thumbnail_pic_s02) {
this.thumbnail_pic_s02 = thumbnail_pic_s02;
}
public String getThumbnail_pic_s03() {
return thumbnail_pic_s03;
}
public void setThumbnail_pic_s03(String thumbnail_pic_s03) {
this.thumbnail_pic_s03 = thumbnail_pic_s03;
}
public boolean hasImage(){
return thumbnail_pic_s02==null && thumbnail_pic_s03==null;
}
}
}
activity中
public class MainActivity extends AppCompatActivity implements IView{
private XRecyclerView xRecyclerView;
private IPresenterImpl iPresenter;
private RecyclerAdapter adapter;
private int mPage;
private final int COUNT=31;
private ImageView imageView;
private TextView textView;
private Button button_login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView(savedInstanceState);
//实例化
mPage=1;
iPresenter=new IPresenterImpl(this);
final LinearLayoutManager manager=new LinearLayoutManager(this);
manager.setOrientation(OrientationHelper.VERTICAL);
xRecyclerView.setLayoutManager(manager);
adapter=new RecyclerAdapter(this);
xRecyclerView.setAdapter(adapter);
xRecyclerView.setPullRefreshEnabled(true);
xRecyclerView.setLoadingMoreEnabled(true);
xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
@Override
public void onRefresh() {
mPage=1;
getInfo();
}
@Override
public void onLoadMore() {
getInfo();
}
});
getInfo();
adapter.setOnLongitemClickListen(new RecyclerAdapter.OnLongitemClickListen() {
@Override
public void delItem(final int position) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this)
.setTitle("删除")
.setMessage("确定要删除吗")
.setIcon(R.drawable.ic_launcher_background)
.setPositiveButton("是", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
adapter.delData(position);
}
})
.setNegativeButton("否", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}
});
loginMethod();
imageViewMethod();
}
//属性动画
private void imageViewMethod() {
//补间动画
/* ScaleAnimation scaleAnimation = new ScaleAnimation(0f, 2, 0f, 2, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(2000);
scaleAnimation.setFillAfter(true);
imageView.startAnimation(scaleAnimation);*/
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ScaleAnimation scaleAnimation = new ScaleAnimation(0f, 2, 0f, 2, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(2000);
imageView.startAnimation(scaleAnimation);
}
});
}
//第三方qq登录
private void loginMethod() {
button_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
UMShareConfig config=new UMShareConfig();
config.isNeedAuthOnGetUserInfo(true);
UMShareAPI.get(MainActivity.this).setShareConfig(config);
UMShareAPI umShareAPI = UMShareAPI.get(MainActivity.this);
umShareAPI.getPlatformInfo(MainActivity.this, SHARE_MEDIA.QQ, new UMAuthListener() {
@Override
public void onStart(SHARE_MEDIA share_media) {
}
@Override
public void onComplete(SHARE_MEDIA share_media, int i, Map map) {
String screen_name = map.get("screen_name");
String profile_image_url = map.get("profile_image_url");
ImageLoader.getInstance().displayImage(profile_image_url,imageView);
textView.setText(screen_name);
button_login.setVisibility(View.INVISIBLE);
}
@Override
public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
}
@Override
public void onCancel(SHARE_MEDIA share_media, int i) {
}
});
}
});
}
private void getInfo() {
Map params=new HashMap<>();
iPresenter.startRequest(Path.path_news,params,NewsBean.class);
}
//获取资源ID
private void initView(Bundle savedInstanceState) {
xRecyclerView = findViewById(R.id.x_recyclerview);
imageView = findViewById(R.id.image_tx);
textView = findViewById(R.id.text_name);
button_login = findViewById(R.id.button_login);
}
@Override
public void requestData(Object data) {
NewsBean bean= (NewsBean) data;
List data1 = bean.getData();
if(mPage==1){
adapter.setList(data1);
}else {
adapter.addList(data1);
}
mPage++;
xRecyclerView.refreshComplete();
xRecyclerView.loadMoreComplete();
if(data1.size()