android:DataBinding使用小结(四)(1)

package tsou.cn.databinding.bean;

/**

  • Created by Administrator on 2018/5/31 0031.

*/

public class LoadMore {

private String name;

private String imaggUrl;

public LoadMore(String name, String imaggUrl) {

this.name = name;

this.imaggUrl = imaggUrl;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getImaggUrl() {

return imaggUrl;

}

public void setImaggUrl(String imaggUrl) {

this.imaggUrl = imaggUrl;

}

}

  • item布局item_load_more
<?xml version="1.0" encoding="utf-8"?>

<layout xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:bind=“http://schemas.android.com/apk/res-auto”>

<variable

name=“loadMore”

type=“tsou.cn.databinding.bean.LoadMore” />

<LinearLayout

android:layout_width=“match_parent”

android:layout_height=“200dp”

android:layout_marginBottom=“5dp”

android:orientation=“vertical”>

<TextView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_gravity=“center”

android:layout_margin=“10dp”

android:textSize=“16sp”

android:textColor=“@android:color/black”

android:text=“@{loadMore.name,default=标题}” />

<ImageView

android:layout_width=“match_parent”

android:layout_height=“match_parent”

bind:url=“@{loadMore.imaggUrl}”

android:src=“@mipmap/ic_launcher_round” />

  • LoadMoreAdapter

package tsou.cn.databinding.adapter;

import android.databinding.DataBindingUtil;

import android.support.annotation.Nullable;

import android.view.LayoutInflater;

import android.view.ViewGroup;

import android.widget.ImageView;

import com.bumptech.glide.Glide;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

import tsou.cn.databinding.R;

import tsou.cn.databinding.bean.LoadMore;

import tsou.cn.databinding.databinding.ItemLoadMoreBinding;

import tsou.cn.databinding.viewholder.MyBaseViewHolder;

/**

  • Created by Administrator on 2018/5/31 0031.

*/

public class LoadMoreAdapter extends BaseQuickAdapter<LoadMore, MyBaseViewHolder> {

public LoadMoreAdapter(@Nullable List data) {

super(data);

}

@Override

protected MyBaseViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType) {

ItemLoadMoreBinding binding = DataBindingUtil.inflate(LayoutInflater.from(mContext),

R.layout.item_load_more, parent, false);

return new MyBaseViewHolder(binding.getRoot());

}

@Override

protected void convert(MyBaseViewHolder holder, LoadMore item) {

ItemLoadMoreBinding binding = DataBindingUtil.getBinding(holder.itemView);

binding.setLoadMore(item);

binding.executePendingBindings();

}

}

  • MyBaseViewHolder

package tsou.cn.databinding.viewholder;

import android.view.View;

import com.chad.library.adapter.base.BaseViewHolder;

public class MyBaseViewHolder extends BaseViewHolder {

public MyBaseViewHolder(View view) {

super(view);

}

}

  • Activity调用

package tsou.cn.databinding;

import android.graphics.Color;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.support.v4.widget.SwipeRefreshLayout;

import android.support.v7.widget.LinearLayoutManager;

import android.support.v7.widget.RecyclerView;

import android.view.View;

import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.lang.ref.WeakReference;

import java.util.ArrayList;

import tsou.cn.databinding.adapter.LoadMoreAdapter;

import tsou.cn.databinding.bean.LoadMore;

import tsou.cn.databinding.databinding.ActivityLoadMoreRecyclerViewBinding;

import tsou.cn.databinding.view.CustomLoadMoreView;

public class LoadMoreRecyclerViewActivity extends BaseRecyclerViewActivity

implements SwipeRefreshLayout.OnRefreshListener,

BaseQuickAdapter.RequestLoadMoreListener, BaseQuickAdapter.OnItemClickListener {

private LoadMoreAdapter adapter;

private ArrayList loadMores = new ArrayList<>();

private int mLastIndex;

private MyHandler myHandler;

@Override

public int getLayoutId() {

return R.layout.activity_load_more_recycler_view;

}

@Override

public RecyclerView getRecyclerView() {

return binding.recyclerView;

}

protected void initData(Bundle savedInstanceState) {

binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));

binding.swipeRefresh.setColorSchemeColors(Color.rgb(63, 81,

181));

adapter = new LoadMoreAdapter(loadMores);

adapter.isFirstOnly(false);

//慢慢淡进

//adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);

//慢慢放大进入

//adapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);

//从下滑入

adapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);

//从左滑入

//adapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT);

//从有滑入

//adapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_RIGHT);

adapter.setLoadMoreView(new CustomLoadMoreView());

binding.recyclerView.setAdapter(adapter);

adapter.setEnableLoadMore(false);

adapter.setEmptyView(R.layout.loading_view, (ViewGroup) binding.recyclerView.getParent());

binding.swipeRefresh.setEnabled(false);

myHandler = new MyHandler(this);

fetchData(true);

}

@Override

protected void initListener() {

super.initListener();

binding.swipeRefresh.setOnRefreshListener(this);

adapter.setOnLoadMoreListener(this, binding.recyclerView);

adapter.setOnItemClickListener(this);

}

@Override

public void onRefresh() {

if (adapter.getData().isEmpty())

adapter.setEmptyView(R.layout.loading_view, (ViewGroup) binding.recyclerView.getParent());

adapter.setEnableLoadMore(false);

fetchData(true);

}

@Override

public void onLoadMoreRequested() {

if (binding.swipeRefresh != null) {

binding.swipeRefresh.setEnabled(false);

mLastIndex++;

fetchData(false);

}

}

@Override

public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

}

private void fetchData(boolean isRefresh) {

if (isRefresh) {

mLastIndex = 1;

}

myHandler.sendEmptyMessageDelayed(0, 3000);

}

private static class MyHandler extends Handler {

private WeakReference nowActivity;

public MyHandler(LoadMoreRecyclerViewActivity nowActivity) {

this.nowActivity = new WeakReference(nowActivity);

}

@Override

public void handleMessage(Message msg) {

super.handleMessage(msg);

LoadMoreRecyclerViewActivity activity = nowActivity.get();

if (activity == null || activity.isFinishing()) {

return;

}

switch (msg.what) {

case 0:

ArrayList loadMores = activity.loadMores;

int mLastIndex = activity.mLastIndex;

ActivityLoadMoreRecyclerViewBinding binding = activity.binding;

LoadMoreAdapter adapter = activity.adapter;

ArrayList loadMoresCurrent = new ArrayList<>();

if (mLastIndex == 1) {

loadMores.clear();

for (int i = 0; i < 10; i++) {

loadMoresCurrent.add(new LoadMore(“晓果” + i, “https://unsplash.it/200/200?random&” + i));

}

loadMores.addAll(loadMoresCurrent);

} else {

for (int i = 11; i < 20; i++) {

loadMoresCurrent.add(new LoadMore(“晓果” + i, “https://unsplash.it/200/200?random&” + i));

}

}

if (mLastIndex == 1) {

adapter.setNewData(loadMores);

binding.swipeRefresh.setEnabled(true);

binding.swipeRefresh.setRefreshing(false);

adapter.setEnableLoadMore(true);

} else {

if (loadMoresCurrent.size() == 0) {

adapter.loadMoreEnd();

} else {

adapter.addData(loadMoresCurrent);

adapter.loadMoreComplete();

}

binding.swipeRefresh.setEnabled(true);

}

break;

}

}

}

}

  • BindingAdapter处理图片显示

package tsou.cn.databinding.utils;

import android.databinding.BindingAdapter;

import android.databinding.BindingConversion;

import android.widget.ImageView;

import android.widget.TextView;

import com.bumptech.glide.Glide;

/**

  • Created by Administrator on 2018/5/30 0030.

*/

public class ImageUtil {

@BindingAdapter({“url”})

public static void loadImage(ImageView view, String url) {

Glide.with(UIUtils.getContext()).load(url).into(view);

}

@BindingAdapter(“android:text”)

public static void setText(TextView view, String text) {

view.setText(text + “-追加的数据”);

}

@BindingConversion

public static String conversionString(String text) {

return text + “-conversionString”;

}

}

  • 加载更多样式CustomLoadMoreView

package tsou.cn.databinding.view;

import com.chad.library.adapter.base.loadmore.LoadMoreView;

import tsou.cn.databinding.R;

/**

  • Created by BlingBling on 2016/10/15.

*/

public final class CustomLoadMoreView extends LoadMoreView {

@Override

public int getLayoutId() {

return R.layout.view_load_more;

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助

因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
de

public int getLayoutId() {

return R.layout.view_load_more;

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助

因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

[外链图片转存中…(img-8wcP6vKe-1715497473719)]

[外链图片转存中…(img-VMx3bMf6-1715497473721)]

[外链图片转存中…(img-Pi2BfrdU-1715497473722)]

[外链图片转存中…(img-rOy19RHK-1715497473723)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值