Skip to content

Commit

Permalink
BaseAdapter和BaseView新增单选
Browse files Browse the repository at this point in the history
  • Loading branch information
TommyLemon committed Jul 1, 2018
1 parent aae41e1 commit cdff10e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* <br> 适用于几乎所有列表、表格,包括:
* <br> 1.RecyclerView及其子类
* <br> 2.ListView,GridView等AbsListView的子类
* <br> 3.RecyclerView刷新用 refresh 或 notifyDataSetChanged; AbsListView刷新用 refresh 或 notifyListDataSetChanged
* <br> 3.刷新用 refresh 或 notifyListDataSetChanged,notifyDataSetChanged 可能无效
* <br> 4.出于性能考虑,里面很多方法对变量(比如list)都没有判断,应在adapter外判断
* @author SCWANG
* @author Lemon
Expand Down Expand Up @@ -91,23 +91,41 @@ public void setOnLoadListener(OnLoadListener onLoadListener) {
* <br > > 0 - 列表滚到倒数第preloadCount个Item View显示时加载更多
* @use 可在子类getView被调用前(可以是在构造器内)赋值
*/
protected int preloadCount = 0;
public int preloadCount = 0;


//预加载,可不使用 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>





/**bv的显示方法
* @param position
* @param bv
*/
@Override
public void bindView(int position, BV bv) {
bv.selected = isSelected(position);
bv.bindView(getItem(position), position, getItemViewType(position));
if (SettingUtil.preload && onLoadListener != null && position >= getCount() - 1 - preloadCount) {
onLoadListener.onLoadMore();
}
}

/**
* 已选中项的位置,一般可在onItemClick回调中:
* <br /> adapter.selectedPosition = position;
* <br /> adapter.notifyListDataSetChanged();
*/
public int selectedPosition = -1;
/**是否已被选中,默认实现单选,重写可自定义实现多选
* @param position
* @return
*/
public boolean isSelected(int position) {
return selectedPosition == position;
}



Expand All @@ -122,14 +140,12 @@ public List<T> getList() {
*/
public synchronized void refresh(List<T> list) {
this.list = list == null ? null : new ArrayList<T>(list);
notifyDataSetChanged(); //仅对 RecyclerView 有效
notifyListDataSetChanged(); //仅对 AbsListView 有效
}




//RecyclerAdapter <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

@Override
public BV onCreateViewHolder(final ViewGroup parent, int viewType) {
final BV bv = createView(viewType, parent);
Expand Down Expand Up @@ -195,9 +211,9 @@ public void unregisterDataSetObserver(DataSetObserver observer) {
/**
* Notifies the attached observers that the underlying data has been changed
* and any View reflecting the data set should refresh itself.
* 仅对 AbsListView 有效
*/
public void notifyListDataSetChanged() {
notifyDataSetChanged(); //仅对 RecyclerView 有效
mDataSetObservable.notifyChanged();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ public <V extends View> V findViewById(int id, OnClickListener listener) {
* 视图类型,部分情况下需要根据viewType使用不同layout,对应Adapter的itemViewType
*/
public int viewType = 0;
/**
* 是否已被选中
*/
public boolean selected = false;

/**创建一个新的View
* @return
Expand Down Expand Up @@ -204,6 +208,8 @@ public void bindView(T data_) {
Log.w(TAG, "bindView data_ == null");
}
this.data = data_;

//不一定要用单选功能,实现也不一定要用这种方式,这里写会影响所有BaseView子类的性能,子类写更好 itemView.setSelected(selected);
}

/**获取可见性
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ public void initEvent() {//必须在onCreateView方法内调用
//示例代码<<<<<<<<<<<<<<<<<<<
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//实现单选
adapter.selectedPosition = adapter.selectedPosition == position ? -1 : position;
adapter.notifyListDataSetChanged();

toActivity(UserActivity.createIntent(context, position));//一般用id,这里position仅用于测试 id));//
}
//示例代码>>>>>>>>>>>>>>>>>>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package zblibrary.demo.DEMO;

import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
Expand Down Expand Up @@ -72,13 +71,11 @@ public View createView() {


@Override
public void bindView(Entry<String, String> data){
public void bindView(Entry<String, String> data_){
//示例代码<<<<<<<<<<<<<<<<
if (data == null) {
Log.e(TAG, "bindView data == null >> data = new Entry<>(); ");
data = new Entry<String, String>();
}
this.data = data;
super.bindView(data_ != null ? data_ : new Entry<String, String>());

itemView.setBackgroundResource(selected ? R.drawable.alpha3 : R.drawable.white_to_alpha);

tvDemoViewName.setText(StringUtil.getTrimedString(data.getKey()));
tvDemoViewNumber.setText(StringUtil.getTrimedString(data.getValue()));
Expand Down

0 comments on commit cdff10e

Please sign in to comment.