我使用RecyclerView
选择所有选项。此选项仅选择屏幕可见项目。不是全部选择。但是我选择后从上到下滚动。有什么问题?
适配器类:
public class FilterBrandAdapter extends RecyclerView.Adapter<FilterBrandAdapter.MyViewHolder> {
private ArrayList<FilterBrandDataModel> mBrandModelArraylist;
private Context mContext;
private TinyDB mPrefDb;
private boolean isSelectedAll;
public static final String PREF_SELECTED_ALL = "selectedAll";
private CartTotalListener mCartListener;
public void selectAll() {
Log.e("onClickSelectAll", "yes");
isSelectedAll = true;
notifyDataSetChanged();
}
public void deselectAll() {
Log.e("onClickSelectAll", "no");
isSelectedAll = false;
notifyDataSetChanged();
}
class MyViewHolder extends RecyclerView.ViewHolder {
CheckBox cbItemTitle;
MyViewHolder(View itemView) {
super(itemView);
cbItemTitle = (CheckBox) itemView.findViewById(R.id.checkBox_filter_brand_title);
itemView.setClickable(true);
}
}
public FilterBrandAdapter(BrandFilterActivity activity, ArrayList<FilterBrandDataModel> mArrayList) {
this.mContext = activity;
this.mBrandModelArraylist = mArrayList;
LayoutInflater inflater = activity.getLayoutInflater();
try {
this.mCartListener = ((CartTotalListener) mContext);
} catch (ClassCastException e) {
throw new ClassCastException("Activity must implement AdapterCallback.");
}
}
@Override
public FilterBrandAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_recycler_brand_items, parent, false);
mPrefDb = new TinyDB(mContext);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
final FilterBrandDataModel brandsData = mBrandModelArraylist.get(position);
holder.cbItemTitle.setText(mBrandModelArraylist.get(position).getCategoryName());
//set adapter classcheckbox change listener for if clicked or not
holder.cbItemTitle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (holder.cbItemTitle.isChecked()) {
//if check box checked selected checkbox saved in tinyDB
int getPosition = (int) buttonView.getTag();
mBrandModelArraylist.get(getPosition).setSelected(buttonView.isChecked());
mPrefDb.putBoolean(brandsData.getCategoryName(), true);
holder.cbItemTitle.setChecked(true);
Log.e("checked item-->", brandsData.getCategoryName());
mCartListener.onMethodCallback();
} else {
//else check box un-checked selected checkbox save checkbox is un-checked in tinyDB
int getPosition = (int) buttonView.getTag();
mBrandModelArraylist.get(getPosition).setSelected(buttonView.isChecked());
mPrefDb.putBoolean(brandsData.getCategoryName(), false);
holder.cbItemTitle.setChecked(false);
Log.e("un-checked item-->", brandsData.getCategoryName());
mCartListener.onMethodCallback();
}
}
});
//select & deselect all checkbox in recyclerview
if (!isSelectedAll) {
holder.cbItemTitle.setTag(position);
holder.cbItemTitle.setChecked(mBrandModelArraylist.get(position).isSelected());
holder.cbItemTitle.setChecked(false);
} else {
holder.cbItemTitle.setTag(position);
holder.cbItemTitle.setChecked(mBrandModelArraylist.get(position).isSelected());
holder.cbItemTitle.setChecked(true);
}
//save selected (or) deselect checkbox with position in tinyDB
holder.cbItemTitle.setTag(position);
holder.cbItemTitle.setChecked(mBrandModelArraylist.get(position).isSelected());
boolean checked = mPrefDb.getBoolean(brandsData.getCategoryName(), true);
holder.cbItemTitle.setChecked(checked);
}
@Override
public int getItemCount() {
return mBrandModelArraylist.size();
}
}
我使用interface(mCartListener.onMethodCallback())来通知所选项目&amp;在文本视图中显示项目计数。
我的界面:
@Override
public void onMethodCallback() {
ArrayList<String> array = new ArrayList<>();
//checking which items are selected in adapter class & save into array list
for (FilterBrandDataModel brandDataModel : mArrayList) {
if (brandDataModel.isSelected()) {
array.add(brandDataModel.getCategoryName());
}
}
int size = array.size();
if (size == mArrayList.size()) {
mPrefDb.putString(PREF_SELECTED_BRANDS_TOT, "true");
String text = String.valueOf(size) + " " + getResources().getString(R.string.msg_selected);
mBrandResults.setText(text);
} else {
mPrefDb.putString(PREF_SELECTED_BRANDS_TOT, "false");
String text = String.valueOf(size) + " " + getResources().getString(R.string.msg_selected);
mBrandResults.setText(text);
}
}
答案 0 :(得分:0)
RecyclerView
在滚动时加载数据,但我需要其中的所有数据而不滚动。所以,我更改了RecyclerView
中的ScrollView
:
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.85"
android:overScrollMode="never"
android:scrollbars="none">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:overScrollMode="never"
/>
</ScrollView>
然后,在Activity
,
RecyclerView.setNestedScrollEnabled(false);
我使用ScrollView
滚动项目。那就是它。
答案 1 :(得分:0)
问题是您只在selectAll()
( isSelectedAll = true; )中切换标记。在您的适配器中将有例如3个可见项目(我不知道它们在您的屏幕上有多大)。一个项目由一个视图持有者表示。切换标记并调用notifyDataSetChanged()
后,将为每个可见项调用函数onBindViewHolder()
- &gt;在我的例子中3次。在此功能中,如果选中该复选框,则使用if (!isSelectedAll) {
决定。因此永远不会要求所有项目。
<强>解决方案:强>
更改
public void selectAll() {
Log.e("onClickSelectAll", "yes");
isSelectedAll = true;
notifyDataSetChanged();
}
要
public void selectAll() {
Log.e("onClickSelectAll", "yes");
for(FilterBrandDataModel brandsData : mBrandModelArraylist) {
mPrefDb.putBoolean(brandsData.getCategoryName(), true);
}
notifyDataSetChanged();
}
同样适用于deselectAll()
。