-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ac174d7
commit 7851302
Showing
13 changed files
with
693 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<layout> | ||
|
||
<data> | ||
<variable | ||
name="model" | ||
type="com.foretree.baseactivity.MainActivity.Model"/> | ||
</data> | ||
|
||
<TextView xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:id="@+id/text" | ||
android:text="@{model.text}" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content"> | ||
|
||
</TextView> | ||
</layout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
210 changes: 210 additions & 0 deletions
210
base/src/main/java/com/foretree/base/adapter/AbstractRecyclerAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,210 @@ | ||
package com.foretree.base.adapter; | ||
|
||
import android.support.annotation.LayoutRes; | ||
import android.support.v7.widget.RecyclerView; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
/** | ||
* Project AndroidBase | ||
* Created by xieyangxuejun on 17-9-11. | ||
*/ | ||
public abstract class AbstractRecyclerAdapter<T, VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> { | ||
private List<T> mData; | ||
private OnItemClickListener mOnItemClickListener; | ||
private OnItemLongClickListener mItemLongClickListener; | ||
|
||
public AbstractRecyclerAdapter(List<T> data) { | ||
this.mData = data == null ? new ArrayList<T>() : data; | ||
} | ||
|
||
|
||
@Override | ||
public void onBindViewHolder(VH holder, int position) { | ||
T item; | ||
try { | ||
item = mData.get(position); | ||
} catch (IndexOutOfBoundsException e) { | ||
item = null; | ||
} | ||
this.onBindViewHolder(holder, position, item); | ||
if (holder.itemView != null) { | ||
if (mOnItemClickListener != null) { | ||
holder.itemView.setOnClickListener(new OnItemClickListenerImpl(this, position)); | ||
} | ||
if (mItemLongClickListener != null) { | ||
holder.itemView.setOnLongClickListener(new OnItemLongClickListenerImpl(this, position)); | ||
} | ||
} | ||
} | ||
|
||
public abstract void onBindViewHolder(VH holder, int position, T item); | ||
|
||
@Override | ||
public int getItemCount() { | ||
return mData.size(); | ||
} | ||
|
||
/** | ||
* 获取条目数据 | ||
* | ||
* @param position 要获取的对象在适配器中的位置 | ||
* @return position位置的对象 | ||
*/ | ||
public T getItem(int position) { | ||
return mData.get(position); | ||
} | ||
|
||
/** | ||
* 添加一个条目 | ||
* | ||
* @param item 要添加的数据 | ||
*/ | ||
public void addItem(T item) { | ||
mData.add(item); | ||
notifyItemInserted(mData.size() - 1); | ||
} | ||
|
||
/** | ||
* 添加一个条目到position位置 | ||
* | ||
* @param position 指定的位置 | ||
* @param item 要添加的数据 | ||
*/ | ||
public void addItem(int position, T item) { | ||
mData.add(position, item); | ||
notifyItemInserted(position); | ||
} | ||
|
||
/** | ||
* 添加多个条目 | ||
* | ||
* @param collection 要添加的数据集合 | ||
*/ | ||
public void addItemAll(Collection<? extends T> collection) { | ||
int oldSize = mData.size(); | ||
mData.addAll(collection); | ||
notifyItemRangeInserted(oldSize, collection.size()); | ||
} | ||
|
||
/** | ||
* 添加多个条目到position位置 | ||
* | ||
* @param position 指定的位置 | ||
* @param collection 要添加的数据集合 | ||
*/ | ||
public void addItemAll(int position, Collection<? extends T> collection) { | ||
mData.addAll(position, collection); | ||
notifyItemRangeInserted(position, collection.size()); | ||
} | ||
|
||
/** | ||
* 删除一个条目 | ||
* <p>为了避免notifyItemRemoved后导致position错乱,所以再调用notifyItemRangeChanged刷新position</p> | ||
* | ||
* @param position 要删除的位置 | ||
*/ | ||
public void removeItem(int position) { | ||
mData.remove(position); | ||
notifyItemRemoved(position); | ||
notifyItemRangeChanged(position, mData.size()); | ||
} | ||
|
||
|
||
/** | ||
* 删除多个条目 | ||
* | ||
* @param collection 要删除的数据集合 | ||
*/ | ||
public void removeItemAll(Collection<? extends T> collection) { | ||
mData.removeAll(collection); | ||
notifyDataSetChanged(); | ||
} | ||
|
||
/** | ||
* 清空适配器数据 | ||
*/ | ||
public void clear() { | ||
mData.clear(); | ||
//notifyDataSetChanged(); | ||
notifyItemRangeRemoved(0, mData.size()); | ||
} | ||
|
||
|
||
public List<T> getData() { | ||
return mData; | ||
} | ||
|
||
public void setOnItemClickListener(OnItemClickListener onItemClickListener) { | ||
this.mOnItemClickListener = onItemClickListener; | ||
} | ||
|
||
public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) { | ||
this.mItemLongClickListener = onItemLongClickListener; | ||
} | ||
|
||
|
||
/** | ||
* 条目点击 | ||
*/ | ||
public interface OnItemClickListener { | ||
void onItemClick(AbstractRecyclerAdapter adapter, View view, int position); | ||
} | ||
|
||
/** | ||
* 条目长按 | ||
*/ | ||
public interface OnItemLongClickListener { | ||
boolean onItemLongClick(AbstractRecyclerAdapter adapter, View view, int position); | ||
} | ||
|
||
|
||
protected static class OnItemClickListenerImpl implements View.OnClickListener { | ||
private AbstractRecyclerAdapter mAdapter; | ||
private int mPosition; | ||
|
||
public OnItemClickListenerImpl(AbstractRecyclerAdapter adapter, int position) { | ||
this.mAdapter = adapter; | ||
this.mPosition = position; | ||
} | ||
|
||
@Override | ||
public void onClick(View v) { | ||
mAdapter.mOnItemClickListener.onItemClick(mAdapter, v, mPosition); | ||
} | ||
} | ||
|
||
protected static class OnItemLongClickListenerImpl implements View.OnLongClickListener { | ||
private AbstractRecyclerAdapter mAdapter; | ||
private int mPosition; | ||
|
||
public OnItemLongClickListenerImpl(AbstractRecyclerAdapter adapter, int position) { | ||
this.mAdapter = adapter; | ||
this.mPosition = position; | ||
} | ||
|
||
@Override | ||
public boolean onLongClick(View v) { | ||
return mAdapter.mItemLongClickListener.onItemLongClick(mAdapter, v, mPosition); | ||
} | ||
} | ||
|
||
|
||
public static abstract class RecyclerHolder<T> extends RecyclerView.ViewHolder { | ||
|
||
public RecyclerHolder(View itemView) { | ||
super(itemView); | ||
} | ||
|
||
public RecyclerHolder(ViewGroup parent, @LayoutRes int layoutId) { | ||
this(LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false)); | ||
} | ||
|
||
public abstract void onBindViewHolder(int position, T item); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
base/src/main/java/com/foretree/base/adapter/DataBindingRecyclerAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.foretree.base.adapter; | ||
|
||
import android.databinding.DataBindingUtil; | ||
import android.databinding.ViewDataBinding; | ||
import android.view.LayoutInflater; | ||
import android.view.ViewGroup; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Project AndroidBase | ||
* Created by xieyangxuejun on 17-9-11. | ||
*/ | ||
public abstract class DataBindingRecyclerAdapter<T, B extends ViewDataBinding> extends AbstractRecyclerAdapter<T, DataBindingRecyclerHolder<B>> { | ||
|
||
public DataBindingRecyclerAdapter(List<T> data) { | ||
super(data); | ||
} | ||
|
||
@Override | ||
public DataBindingRecyclerHolder<B> onCreateViewHolder(ViewGroup parent, int viewType) { | ||
return new DataBindingRecyclerHolder(DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), getItemLayoutId(), parent, false)); | ||
} | ||
|
||
protected abstract int getItemLayoutId(); | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
base/src/main/java/com/foretree/base/adapter/DataBindingRecyclerHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.foretree.base.adapter; | ||
|
||
import android.databinding.ViewDataBinding; | ||
import android.support.v7.widget.RecyclerView; | ||
import android.view.View; | ||
|
||
/** | ||
* Project AndroidBase | ||
* Created by xieyangxuejun on 17-9-11. | ||
*/ | ||
public class DataBindingRecyclerHolder<B extends ViewDataBinding> extends RecyclerView.ViewHolder { | ||
public B binding; | ||
|
||
public DataBindingRecyclerHolder(B binding) { | ||
this(binding.getRoot()); | ||
this.binding = binding; | ||
} | ||
|
||
|
||
public DataBindingRecyclerHolder(View v) { | ||
super(v); | ||
} | ||
} |
Oops, something went wrong.