我实现了一个分区回收视图,效果很好。我想在一个小节下显示的所有项目中都包含一个复选框。我现在要实现的功能是允许用户一次选中一个复选框。我也尝试过单选按钮,但是问题仍然存在,因为我无法抓住听众。
我在stackoverflow上进行了搜索,但是无法正常工作。 getTag()给我一个空指针异常,我也尝试过保留一个selectedPosition变量并尝试使用,但我无法使getTag()正常工作。
public class HeaderRecyclerViewSection extends StatelessSection {
private static final String TAG = HeaderRecyclerViewSection.class.getSimpleName();
private String title;
private List<RestaurauntMenuItemDetail> list;
private List<RestaurauntMenuItemDetail> checkedList;
private int selectedPosition = -1;
public HeaderRecyclerViewSection(String title, List<RestaurauntMenuItemDetail> list) {
super(R.layout.section_ex1_header, R.layout.layout_section_item);
this.title = title;
this.list = list;
}
@Override
public int getContentItemsTotal() {
return list.size();
}
@Override
public RecyclerView.ViewHolder getItemViewHolder(View view) {
return new ItemViewHolder(view);
}
@Override
public void onBindItemViewHolder(RecyclerView.ViewHolder holder, final int position) {
final ItemViewHolder iHolder = (ItemViewHolder)holder;
iHolder.variationItemName.setText(list.get(position).getVariationItemName());
iHolder.variationItemPrice.setText(Double.toString(list.get(position).getVariationItemPrice()));
iHolder.checkBox.setOnCheckedChangeListener(null);
iHolder.checkBox.setChecked(list.get(position).isSelected());
Log.i("EXECUTED AGAIN","AGAIN");
iHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(selectedPosition == position){
list.get(selectedPosition).setSelected(false);
iHolder.checkBox.setChecked(list.get(selectedPosition).isSelected());
}
list.get(position).setSelected(b);
Log.i("POSITION is",Integer.toString(position));
Log.i("B Is",Boolean.toString(b));
selectedPosition = position;
}
});
}
@Override
public RecyclerView.ViewHolder getHeaderViewHolder(View view) {
return new HeaderViewHolder(view);
}
@Override
public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) {
HeaderViewHolder hHolder = (HeaderViewHolder)holder;
hHolder.headerTitle.setText(title);
}
}
我的ItemHolder类:
public class ItemViewHolder extends RecyclerView.ViewHolder{
public TextView variationItemName;
public TextView variationItemPrice;
CheckBox checkBox;
public ItemViewHolder(View itemView) {
super(itemView);
variationItemName = (TextView) itemView.findViewById(R.id.variationItemName);
variationItemPrice = (TextView) itemView.findViewById(R.id.variationItemPrice);
checkBox = itemView.findViewById(R.id.checkBox2);
}
}
我要实现的目的是允许用户能够在特定部分下选择一个复选框。
编辑1:
这是当前带有单选按钮或复选框的屏幕外观。
编辑2: 我更改了代码,如下所示,这使我可以一次检查一个单选按钮。问题是,当您单击“回收者”视图列表中的最后一个项目时,单选按钮不会被选中,如果选中,则在向上滚动时消失。
@Override
public void onBindItemViewHolder(final RecyclerView.ViewHolder holder, final int position) {
final ItemViewHolder iHolder = (ItemViewHolder)holder;
iHolder.variationItemName.setText(list.get(position).getVariationItemName());
iHolder.variationItemPrice.setText(Double.toString(list.get(position).getVariationItemPrice()));
iHolder.variationItemPrice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i("I WAS CLICKED","ITEM PRICE WAS CLICKED");
}
});
iHolder.radioButton.setChecked(list.get(position).isSelected());
iHolder.radioButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i("I was pressed","BUtton");
RadioButton rb = (RadioButton) view;
if(rb.isChecked()){
Log.i("CONTAINS",Boolean.toString(radioButtonList.contains(rb)));
if(position >= radioButtonList.size()){
radioButtonList.add(rb);
rb.setChecked(true);
list.get(position).setSelected(true);
}else{
}
//list.get(position).setSelected(true);
}
for (int i = 0; i < radioButtonList.size(); i++) {
RadioButton nrb = radioButtonList.get(i);
if(i != position){
if(nrb.isChecked()){
nrb.setChecked(false);
list.get(i).setSelected(false);
}
}
}
Log.i("RADIO LIST",Integer.toString(radioButtonList.size()));
for (int i = 0; i < radioButtonList.size(); i++) {
Log.i("RADIO CHECKED",Boolean.toString(radioButtonList.get(i).isChecked()));
}
Log.i("CURRENT POST",Integer.toString(position));
for (int i = 0; i < list.size() ; i++) {
Log.i("POST",Boolean.toString(list.get(i).isSelected()));
}
}
});
}
我的活动班级:
public class RestaurauntMenuItemDetailActivity extends AppCompatActivity implements RestaurauntMenuItemDetailInterfaceHandler{
private static List<RestaurauntMenuItemDetail> restaurauntMenuItemDetailList = new LinkedList<>();
RestaurauntMenuItemDetailFetchAPI restaurauntMenuItemDetailFetchAPI;
String restaurauntSlug;
String menuSlug;
int menuId;
int variationCount;
List<String> variationNamesList;
private RecyclerView sectionHeader;
private SectionedRecyclerViewAdapter sectionAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_restauraunt_menu_item_detail);
//
// sectionAdapter = new SectionedRecyclerViewAdapter();
restaurauntMenuItemDetailFetchAPI = new RestaurauntMenuItemDetailFetchAPI(this,this);
restaurauntSlug = getIntent().getExtras().getString("restaurauntSlug");
menuSlug = getIntent().getExtras().getString("menuSlug");
menuId = getIntent().getExtras().getInt("menuId");
restaurauntMenuItemDetailFetchAPI.prepareURL(restaurauntSlug,menuSlug,menuId);
restaurauntMenuItemDetailFetchAPI.fetchMenuItemDetail();
}
@Override
public void restaurauntMenuItemDetailListDownloadTaskCompleted(List<RestaurauntMenuItemDetail> result,int variationCount,List<String> variationNames) {
restaurauntMenuItemDetailList = result;
this.variationCount = variationCount;
variationNamesList = variationNames;
sectionHeader = (RecyclerView)findViewById(R.id.menuItemDetailRecyclerView);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(RestaurauntMenuItemDetailActivity.this);
sectionHeader.setLayoutManager(linearLayoutManager);
sectionHeader.setHasFixedSize(true);
// HeaderRecyclerViewSection secondSection = new HeaderRecyclerViewSection("Second Section", restaurauntMenuItemDetailList);
// HeaderRecyclerViewSection thirdSection = new HeaderRecyclerViewSection("Third Section", restaurauntMenuItemDetailList);
sectionAdapter = new SectionedRecyclerViewAdapter();
for(int i=0; i < variationNamesList.size(); i++){
String variationName = variationNames.get(i);
addSectionToAdapter(variationName,sectionAdapter);
sectionHeader.setAdapter(sectionAdapter);
}
// sectionAdapter.addSection(secondSection);
// sectionAdapter.addSection(thirdSection);
// initRecyclerView();
}
private void addSectionToAdapter(String variationName, SectionedRecyclerViewAdapter sectionAdapter){
List<RestaurauntMenuItemDetail> l1 = new ArrayList<>();
for (int i = 0; i < restaurauntMenuItemDetailList.size() ; i++) {
if(restaurauntMenuItemDetailList.get(i).getVariationCategory().equals(variationName)){
l1.add(restaurauntMenuItemDetailList.get(i));
}
}
HeaderRecyclerViewSection firstSection = new HeaderRecyclerViewSection(variationName, l1,sectionAdapter);
sectionAdapter.addSection(firstSection);
}
}
答案 0 :(得分:1)
尝试一下。它为我工作。也可能为您工作。只需用以下代码替换您的OnCheckedChangeListener
代码:
iHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton compoundButton,boolean b){
if(compoundButton.isPressed()){
// click is performed
for(RestaurauntMenuItemDetail item:list){
item.setSelected(false);
}
list.get(holder.getAdapterPosition()).setSelected(b);
notifyDataSetChanged();
}
}
});
答案 1 :(得分:0)
我查看了用户发布的许多其他问题,并通过此处提供的评论获得了一些帮助。我真正想要的是能够一次检查一个单选框,其次,当我们在回收站视图中向下滚动时,单选按钮上的复选框消失了或错了。
我们需要在类中保留一个布尔变量,该变量用于保存对象,并且默认情况下可以将其设置为false。生成获取器和设置器。代码如下:
以防万一,列表位于顶部。
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div class="conatainer-fluid">
<ol class="breadcrumb" style="padding: 0rem 1rem 0rem; background-color: transparent">
<li class="breadcrumb-item active">Files</li>
<li class="pull-right"><i class="fa fa-sort-amount-asc"></i></li>
<li class="pull-right"><i class="fa fa-th-large"></i></li>
<li class="pull-right"><i class="fa fa-list-ul"></i></li>
</ol>
</div>
<hr id="breadcrumb_hr">