Can not select lower index in child adapter after compressing higher index - nestedrecyclerview

I have a Parent and a child Adapter...
when lower index of parent adapter is expanded at begining, it works fine..
And if i click on lower index and then click on the higher index... lower index gets compressed and all the index of child adapter selected perfectly...
And if i click on higher index and then click on the lower index of parent adapter... higher index gets compressed and i can not select lower index item, and selection occurs in higher index that got compressed.
final boolean isExpanded = position==mExpandedPosition;
holder.expandableLayout.setVisibility(isExpanded?View.VISIBLE:View.GONE);
holder.itemView.setActivated(isExpanded);
if (isExpanded)
previousExpandedPosition = position;
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mExpandedPosition = isExpanded ? -1:position;
MyList1 = model.getNestedList();
notifyItemChanged(previousExpandedPosition);
notifyItemChanged(position);
}
});
Want to select Child index

Related

How to Get Checked items in CheckBoxTreeView in JavaFx

How to Get Checked items in CheckBox TreeView in JavaFx?
I don't know how to achieve this... You only get the selected items...
ObservableList<TreeItem<String>> items = treeHazardsXmi.getSelectionModel().getSelectedItems();
Assuming you are using CheckBoxTreeItems, you can iterate through the tree and test each one's selected property:
ObservableSet<CheckBoxTreeItem<?>> checkedItems = FXCollections.observableHashSet();
findCheckedItems((CheckBoxTreeItem<?>) tree.getRoot(), checkedItems);
// ...
private void findCheckedItems(CheckBoxTreeItem<?> item, ObservableSet<CheckBoxTreeItem<?>> checkedItems) {
if (item.isSelected()) {
checkedItems.add(item);
}
for (TreeItem<?> child : item.getChildren()) {
findCheckedItems((CheckBoxTreeItem<?>) child, checkedItems);
}
}
If you have a very large tree, for which that is computationally prohibitive, you can maintain a set of checked items, and by observing each item's selectedProperty, keep the set updated at all times. The downside here is that you have to be vigilant to always create the CheckBoxTreeItem so that it updates the set:
private ObservableSet<CheckBoxTreeItem<?>> checkedItems = FXCollections.observableHashSet();
private <T> CheckBoxTreeItem<T> createTreeItem(T value) {
CheckBoxTreeItem<T> item = new CheckBoxTreeItem<>(value);
item.selectedProperty().addListener((obs, wasChecked, isNowChecked) -> {
if (isNowChecked) {
checkedItems.add(item);
} else {
checkedItems.remove(item);
}
});
return item ;
}
Now, as long as your items are created via the createTreeItem() method, the checkedItems set will always contain the checked items.
Note that if your tree is dynamic (you add and remove nodes at runtime), you should go further and deregister the listener if the item is removed from the tree (left as an exercise for the reader...).

Multi select in tableView javafx

I want to multi select row in my TableView. The problem is that my application is a multitouch application, I have not a keyboard, so not a CTRL key.
I have a code follow :
tableViewArticle.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
But I want to select a lot of row only with a mouse click. For exemple, when I selected one row, the row change in blue, and if after I select an other row, I have two rows in blue.
You could use a custom event filter for the TableView that handles the selection, if a click happened on a table row:
tableViewArticle.addEventFilter(MouseEvent.MOUSE_PRESSED, evt -> {
Node node = evt.getPickResult().getIntersectedNode();
// go up from the target node until a row is found or it's clear the
// target node wasn't a node.
while (node != null && node != tableViewArticle && !(node instanceof TableRow)) {
node = node.getParent();
}
// if is part of a row or the row,
// handle event instead of using standard handling
if (node instanceof TableRow) {
// prevent further handling
evt.consume();
TableRow row = (TableRow) node;
TableView tv = row.getTableView();
// focus the tableview
tv.requestFocus();
if (!row.isEmpty()) {
// handle selection for non-empty nodes
int index = row.getIndex();
if (row.isSelected()) {
tv.getSelectionModel().clearSelection(index);
} else {
tv.getSelectionModel().select(index);
}
}
}
});
If you want to handle touch events differently to mouse events, you can also use MouseEvent.isSynthesized to check, if the event is a touch event.

Change css for a new row added in TableView Javafx

I'm sorry for mistake I'm french.
So I have a tableView empty. I have a button "Add" when on click added row in a tableView. And when I select an row in my tableView, a new button "Cancel" show.
And when I click on a button "Cancel", the row's css change on my row selected (added a class css ".cancel").
The problem is that I click on button "Cancel", and after I click in the button "Add", the css ".cancel" is applicated at an other row while I don't clicked in the button "Add".
I think that there is a problem in index row.
In my method initialize :
articleTable.setRowFactory(param -> new TableRow<LigneTicket>() {
#Override
protected void updateItem(LigneTicket paramT, boolean empty) {
super.updateItem(paramT, empty);
if (!isEmpty() && paramT != null && paramT.getArticle().isArticleCanceled()) {
getStyleClass().add("cancel");
}
}
});
my code on button "Cancel" :
public void cancelLigneTicket() {
int indexSelected = articleTable.getSelectionModel().getSelectedIndex();
articleTable.getItems().get(indexSelected).getArticle().setArticleAnnuler(true);
articleTable.getSelectionModel().clearSelection();
List<LigneTicket> items = new ArrayList<>(articleTable.getItems());
articleTable.getItems().setAll(items);
buttonAnnulArt.setVisible(false);
Help !!
Thanks.
TableRows are used to display the table items. That doesn't mean however, that it will be used with only one item.
This can result in the following sequence of events for a row r:
The item of r is updated to a canceled item and thus the cancel CSS class is added.
The item of r is updated to a non-canceled item, but the cancel CSS class is not removed.
You need to remove the class again. Furthermore with your code the style class could be added multiple times leading to unnecessary memory consumption.
boolean canceled = !empty && paramT != null && paramT.getArticle().isArticleCanceled());
if (canceled) {
if (!getStyleClass().contains("cancel"))
getStyleClass().add("cancel");
} else {
getStyleClass().remove("cancel");
}
or using PseudoClass:
private static final PseudoClass CANCELED = PseudoClass.getPseudoClass("cancel");
...
pseudoClassStateChanged(CANCELED, !empty && paramT != null && paramT.getArticle().isArticleCanceled());
Furthermore you should prefer the TableView.refresh (available in JavaFX >= 8u60) method to refresh the cell items instead of copying the list and setting the items.

Why does Qt list pane indicate there is a selection, even when the user does not select an item and no item appears as selected?

I do not understand why a selection is returned by my QListView, even when the user does not select an item, no item appears as selected, and clearSelection() is called on the pane before it is displayed.
Here is the relevant code that creates the list items:
class WidgetInstanceIdentifier {...} // This class is properly registered with Qt
listpane = new QListView(...);
QStandardItemModel * model = new QStandardItemModel(listpane);
int index = 0;
std::for_each(vg_list.cbegin(), vg_list.cend(), [&](WidgetInstanceIdentifier const & vg)
{
QStandardItem * item = new QStandardItem();
std::string text = "...";
item->setText(text.c_str());
item->setEditable(false);
item->setCheckable(false);
QVariant v;
v.setValue(vg);
item->setData(v);
model->setItem( index, item );
++index;
});
model->sort(0);
listpane->setModel(model);
listpane->clearSelection();
Here is a screenshot showing the dialog at the moment I click "OK". Notice that neither item in the list pane is selected:
... And here is the relevant code that tests for the selection:
QItemSelectionModel * listpane_selectionModel = listpane->selectionModel();
QModelIndex selectedIndex = listpane_selectionModel->currentIndex();
if (!selectedIndex.isValid())
{
// This block of code is not hit!!!!!!
// I expect it would be hit
}
QVariant vg_variant = listpaneModel->item(selectedIndex.row())->data();
// The following variable is properly set to correct data representing
// the first item in the list
vg_to_use = vg_variant.value<WidgetInstanceIdentifier>();
As noted in the code, the block of code that I expect to be hit in the case of "no selection" - the if (!selectedIndex.isValid()) {...} block - is not hit. Instead, the first item in the list is returned, as though it is selected. This is not desired! The user has no way to know which item is really being selected.
What am I misunderstanding? Why does Qt seem to report that there is a valid selection, even with no item selected in the list? What is the proper way to check if there is really no item selected?
I think selected item and current item are not the same things. Although, in some cases current item can be selected too, but this is not necessarily. Usually the current item indicated by the dashed outline in the view. Thus, if you want to check selection items count do it with QItemSelectionModel::selectedIndexes() function, i.e.:
QModelIndexList selected = listpane_selectionModel->selectedIndexes();
if (!selected.isEmpty()) {
// do something
}

Eliminate explicit scrolling in a listbox

I have a list box which is populated via migrating items from another list box. I have 2 asp.net buttons namely Up and Down to scroll up and down through the list box.
The maximum number of items visible at a time in the list box is 9, while the maximum number of items are 21. Hence a scrollbar comes eventually. Its pretty evident and ok.
Now if I select the 21st item and click on Up button, the 21st item moves to one place above, but the list shows items from the first. I mean to say that the list is rearranged, and the client has to explicitly scroll down the list box to view the 21st item which has been moved above.
I want that if I select an item and click the Up button, view of the list box should be there itself, it shouldn't show the list box from the very first item.
Any help would be appreciated.
Below is my code:
protected void btnThird_Click(object sender, EventArgs e)
{
if (lstBoxSelectedColumns.SelectedIndex == 0)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "alertScrollUp", "alertMessage('Please select other item to scroll up or click the Down navigation button');", true);
}
else if (lstBoxSelectedColumns.SelectedIndex > 0)
{
for (int i = 0; i < lstBoxSelectedColumns.Items.Count; i++)
{
if (lstBoxSelectedColumns.Items[i].Selected)
{
if (i > 0 && !lstBoxSelectedColumns.Items[i - 1].Selected)
{
ListItem belowItem = lstBoxSelectedColumns.Items[i];
lstBoxSelectedColumns.Items.Remove(belowItem);
lstBoxSelectedColumns.Items.Insert(i-1, belowItem);
lstBoxSelectedColumns.Items[i - 1].Selected = true;
}
}
}
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "alertSelectAtleastOneItem2", "alertMessage('Please select at least one item to scroll above');", true);
}
}
Regards
Anurag

Resources