diff --git a/ZBLibrary(AndroidStudio)/ZBLibrary/src/main/java/zuo/biao/library/base/BaseAdapter.java b/ZBLibrary(AndroidStudio)/ZBLibrary/src/main/java/zuo/biao/library/base/BaseAdapter.java index 7689cac..41f322f 100755 --- a/ZBLibrary(AndroidStudio)/ZBLibrary/src/main/java/zuo/biao/library/base/BaseAdapter.java +++ b/ZBLibrary(AndroidStudio)/ZBLibrary/src/main/java/zuo/biao/library/base/BaseAdapter.java @@ -23,7 +23,7 @@ *
适用于几乎所有列表、表格,包括: *
1.RecyclerView及其子类 *
2.ListView,GridView等AbsListView的子类 - *
3.RecyclerView刷新用 refresh 或 notifyDataSetChanged; AbsListView刷新用 refresh 或 notifyListDataSetChanged + *
3.刷新用 refresh 或 notifyListDataSetChanged,notifyDataSetChanged 可能无效 *
4.出于性能考虑,里面很多方法对变量(比如list)都没有判断,应在adapter外判断 * @author SCWANG * @author Lemon @@ -91,23 +91,41 @@ public void setOnLoadListener(OnLoadListener onLoadListener) { *
> 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回调中: + *
adapter.selectedPosition = position; + *
adapter.notifyListDataSetChanged(); + */ + public int selectedPosition = -1; + /**是否已被选中,默认实现单选,重写可自定义实现多选 + * @param position + * @return + */ + public boolean isSelected(int position) { + return selectedPosition == position; + } @@ -122,14 +140,12 @@ public List getList() { */ public synchronized void refresh(List list) { this.list = list == null ? null : new ArrayList(list); - notifyDataSetChanged(); //仅对 RecyclerView 有效 notifyListDataSetChanged(); //仅对 AbsListView 有效 } - - //RecyclerAdapter <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + @Override public BV onCreateViewHolder(final ViewGroup parent, int viewType) { final BV bv = createView(viewType, parent); @@ -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(); } diff --git a/ZBLibrary(AndroidStudio)/ZBLibrary/src/main/java/zuo/biao/library/base/BaseView.java b/ZBLibrary(AndroidStudio)/ZBLibrary/src/main/java/zuo/biao/library/base/BaseView.java index fe46d10..134ea12 100755 --- a/ZBLibrary(AndroidStudio)/ZBLibrary/src/main/java/zuo/biao/library/base/BaseView.java +++ b/ZBLibrary(AndroidStudio)/ZBLibrary/src/main/java/zuo/biao/library/base/BaseView.java @@ -157,6 +157,10 @@ public V findViewById(int id, OnClickListener listener) { * 视图类型,部分情况下需要根据viewType使用不同layout,对应Adapter的itemViewType */ public int viewType = 0; + /** + * 是否已被选中 + */ + public boolean selected = false; /**创建一个新的View * @return @@ -204,6 +208,8 @@ public void bindView(T data_) { Log.w(TAG, "bindView data_ == null"); } this.data = data_; + + //不一定要用单选功能,实现也不一定要用这种方式,这里写会影响所有BaseView子类的性能,子类写更好 itemView.setSelected(selected); } /**获取可见性 diff --git a/ZBLibrary(AndroidStudio)/app/src/main/java/zblibrary/demo/DEMO/DemoListFragment.java b/ZBLibrary(AndroidStudio)/app/src/main/java/zblibrary/demo/DEMO/DemoListFragment.java index 610924d..87648b9 100644 --- a/ZBLibrary(AndroidStudio)/app/src/main/java/zblibrary/demo/DEMO/DemoListFragment.java +++ b/ZBLibrary(AndroidStudio)/app/src/main/java/zblibrary/demo/DEMO/DemoListFragment.java @@ -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));// } //示例代码>>>>>>>>>>>>>>>>>>> diff --git a/ZBLibrary(AndroidStudio)/app/src/main/java/zblibrary/demo/DEMO/DemoView.java b/ZBLibrary(AndroidStudio)/app/src/main/java/zblibrary/demo/DEMO/DemoView.java index a3d7970..f8b4beb 100755 --- a/ZBLibrary(AndroidStudio)/app/src/main/java/zblibrary/demo/DEMO/DemoView.java +++ b/ZBLibrary(AndroidStudio)/app/src/main/java/zblibrary/demo/DEMO/DemoView.java @@ -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; @@ -72,13 +71,11 @@ public View createView() { @Override - public void bindView(Entry data){ + public void bindView(Entry data_){ //示例代码<<<<<<<<<<<<<<<< - if (data == null) { - Log.e(TAG, "bindView data == null >> data = new Entry<>(); "); - data = new Entry(); - } - this.data = data; + super.bindView(data_ != null ? data_ : new Entry()); + + itemView.setBackgroundResource(selected ? R.drawable.alpha3 : R.drawable.white_to_alpha); tvDemoViewName.setText(StringUtil.getTrimedString(data.getKey())); tvDemoViewNumber.setText(StringUtil.getTrimedString(data.getValue()));