I added a selector background for my RecyclerView items, but I need to allow only single item selection at a time. i.e if one item is selected all the rest of the items must not be selected. How can I achieve this?
Here is my adapter class
a.values_at(1..3).map! {|i| i = 0}
答案 0 :(得分:4)
This is my sample solution:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellId = @"itemCell";
NSDictionary *dictDetailList = [_arrItemList objectAtIndex:indexPath.row];
HomeRestuTableViewCell *cell = (HomeRestuTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellId];
if(!cell) {
[tableView registerNib:[UINib nibWithNibName:@"itemCell" bundle:nil] forCellReuseIdentifier:cellId];
cell = (HomeRestuTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellId];
}
[cell setupHomeRestuCell:dictDetailList];
return cell;
}
Hope it helps you... Thanks
答案 1 :(得分:2)
您还可以考虑使用 SelectionTracker 扩展的RecyclerView。然后,必须将一些代码添加到当前的实现中,以实现所有必需的方法。最后,在构建SelectionTracker的地方,您可以将如下所示的代码放置到Adapter类中。
构造SelectionTracker的示例代码:
SelectionTracker selectionTracker = new SelectionTracker.Builder<>(
"example-unique-id",
mRecyclerView,
new UsersListAdapter.UserItemKeyProvider(list),
new UsersListAdapter.UserItemLookup(mRecyclerView),
StorageStrategy.createStringStorage()
).withSelectionPredicate(SelectionPredicates.<String>createSelectSingleAnything()).build();
方法:
withSelectionPredicate(SelectionPredicates.<String>createSelectSingleAnything())
在这里至关重要,因为它定义了“选择方法”。这里的字符串是可能的键之一。有关更多信息,请遵循: