0
点赞
收藏
分享

微信扫一扫

android:DataBinding使用小结(四,2021Android高级面试题及答案

梅梅的时光 2022-01-25 阅读 61

List tempList;

synchronized (activities) {

tempList = new LinkedList(activities);

}

for (BaseActivity a : tempList) {

a.finish();

}

}

@Override

protected void onDestroy() {

activities.remove(this);

try {

System.gc();

} catch (Exception e) {

}

super.onDestroy();

}

}

  • 创建BaseRecyclerViewActivity处理布局绑定

package tsou.cn.databinding;

import android.databinding.DataBindingUtil;

import android.databinding.ViewDataBinding;

import android.os.Bundle;

import android.support.annotation.Nullable;

import android.support.v7.widget.RecyclerView;

import android.view.View;

import android.view.ViewGroup;

import android.widget.ImageView;

import android.widget.TextView;

public abstract class BaseRecyclerViewActivity extends BaseActivity {

protected View notDataView;

protected View errorView;

protected M binding;

@Override

protected void onCreate(@Nullable Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

binding = DataBindingUtil.setContentView(this, getLayoutId());

setEmptyAndErrorView(getRecyclerView(), R.mipmap.icon_empty, getString(R.string.the_server_did_not_return_data));

initData(savedInstanceState);

initListener();

}

public abstract int getLayoutId();

public abstract RecyclerView getRecyclerView();

protected abstract void initData(Bundle savedInstanceState);

protected void initListener() {

}

protected void setEmptyAndErrorView(RecyclerView mRecyclerView, int emptyIcon, String emptyText) {

notDataView = ge
tLayoutInflater().inflate(R.layout.empty_view, (ViewGroup) mRecyclerView.getParent(), false);

ImageView empty_icon = (ImageView) notDataView.findViewById(R.id.iv_empty_icon);

TextView empty_text = (TextView) notDataView.findViewById(R.id.tv_empty_no_data);

empty_icon.setImageResource(emptyIcon);

empty_text.setText(emptyText);

notDataView.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

onRefresh();

}

});

errorView = getLayoutInflater().inflate(R.layout.error_view, (ViewGroup) mRecyclerView.getParent(), false);

errorView.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

onRefresh();

}

});

}

protected void onRefresh() {

}

}

  • 数据LoadMore

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);

t.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);

举报

相关推荐

0 条评论