Kotlin ViewPager2 stop scrolling on specific itemId - kotlin

I have ViewPager2 with 4 items. I want to prevent user to scroll right to 4th item when he is trying to swipe right from position 3 to 4, but I want him to allow scrolling left all the time. I cannot remove the last 4-th item because I use it on another fragment.
How can I do that? When I make if like this :
if(position == 3){
binding.viewPager.isUserInputEnabled = false
}
This stops mi scrolling in every direction - right as I expected and left too. I need to detect in which direction user scroll and then stop scrolling right only. Ant tips?

Related

How do I resize an array of squished PyQt5 widgets? [duplicate]

I have a QScrollArea Widget, which starts empty;
It has a vertical layout, with a QGridLayout, and a vertical spacer to keep it at the top, and prevent it from stretching over the whole scroll area;
Elsewhere in the program, there is a QTextEdit, which when changed, has its contents scanned for "species" elements, and then they are added to the QGridLayout. Any species elements which have been removed are removed too. This bit works;
I have turned the vertical scrollbar on all the time, so that when it appears it does not sit on top of the other stuff in there. Note that the scroll bar is larger than the scroll box already though, despite not needing to be.
This is the problem. The scroll area seems to be preset, and i cannot change it. If i add more rows to the QGridLayout, the scroll area doesn't increase in size.
Instead, it stays the same size, and squeezes the QGridLayout, making it look ugly (at first);
And then after adding even more it becomes unusable;
Note that again, the scroll bar is still the same size as in previous images. The first two images are from Qt Designer, the subsequent 3 are from the program running.
If I resize the window so that the QScrollArea grows, then I see this:
Indicating that there's some layout inside the scroll area that is not resizing properly.
My question is; what do I need to do to make the scrollable area of the widget resize dynamically as I add and remove from the QGridLayout?
If you're coming here from Google and not having luck with the accepted answer, that's because you're missing the other secret invocation: QScrollArea::setWidget. You must create and explicitly identify a single widget which is to be scrolled. It's not enough to just add the item as a child! Adding multiple items directly to the ScrollArea will also not work.
This script demonstrates a simple working example of QScrollArea:
from PySide.QtGui import *
app = QApplication([])
scroll = QScrollArea()
scroll.setWidgetResizable(True) # CRITICAL
inner = QFrame(scroll)
inner.setLayout(QVBoxLayout())
scroll.setWidget(inner) # CRITICAL
for i in range(40):
b = QPushButton(inner)
b.setText(str(i))
inner.layout().addWidget(b)
scroll.show()
app.exec_()
The documentation provide an answer :
widgetResizable : bool
This property holds whether the scroll area should resize the view widget.
If this property is set to false (the default), the scroll area honors the size of its widget.
Set it to true.
Why don't you use a QListView for your rows, it will manage all the issues for you? Just make sure that after you add it you click on the Class (top right window of designer) and assign a layout or it wont expand properly.
I use a QLIstWidget inside a QScrollArea to make a scrollable image list
Try this for adding other objects to the list, this is how I add an image to the list.
QImage& qim = myclass.getQTImage();
QImage iconImage = copyImageToSquareRegion(qim, ui->display_image->palette().color(QWidget::backgroundRole()));
QListWidgetItem* pItem = new QListWidgetItem(QIcon(QPixmap::fromImage(iconImage)), NULL);
pItem->setData(Qt::UserRole, "thumb" + QString::number(ui->ImageThumbList->count())); // probably not necessary for you
QString strTooltip = "a tooltip"
pItem->setToolTip(strTooltip);
ui->ImageThumbList->addItem(pItem);
Update on Artfunkel's answer:
Here's a PySide6 demo that uses a "Populate" button to run the for loop adding items to the scroll area. Each button will also delete itself when clicked.
from PySide6.QtWidgets import *
app = QApplication([])
scroll = QScrollArea()
scroll.setWidgetResizable(True) # CRITICAL
inner = QFrame(scroll)
inner.setLayout(QVBoxLayout())
scroll.setWidget(inner) # CRITICAL
def on_remove_widget(button):
button.deleteLater()
def populate():
for i in range(40):
b = QPushButton(inner)
b.setText(str(i))
b.clicked.connect(b.deleteLater)
inner.layout().addWidget(b)
b = QPushButton(inner)
b.setText("Populate")
b.clicked.connect(populate)
inner.layout().addWidget(b)
scroll.show()
app.exec()

Left align some rows, right align others in WKInterfaceTable?

I'm trying to display a user's messages in a watchOS app and I'm using a WKInterfaceTable instance to do so.
I would like outgoing messages to be right-aligned and incoming messages to be left-aligned but I can't get it to work. Here is the code I'm using:
let row = mainTable.rowController(at: index) as! MessagesViewTableRowController
row.rowLabel.setText(messageText!)
row.rowLabel.setSemanticContentAttribute(.forceLeftToRight)
if outgoing == 1 {
row.rowLabel.setSemanticContentAttribute(.forceRightToLeft)
}
The line that sets the semantic to right is being executed but it's having no effect on the alignment of the row. Everything is remaining left-aligned.
I figured it out.
The label needed its width to be set to "Relative to Container" with a value of 1. After setting that in the storyboard, my code is working.

Carouselview: If only one item in view, items is shown indefinatley

So a carouselview is awesome for showing multiple elements. But once ther eis only one element in the view and the element isnt as large as the whole screen is, the result is:
Result
So I would need to make sure that if only one element is inside the view, it isnt repeated.
How can I set this or work around this issue without making two layouts and making only one visible, depending on how many items there are in the view?
Its quite simple. If the list contains only 1 item, just set the peek area to 0, otherwise to whatever value you wanT:
if (contentOfListView.Count == 1)
carousel_myAds.PeekAreaInsets = 0;
else if (contentOfListView.Count == 2)
carousel_mySales.PeekAreaInsets = Constants.PEEKAREINSETSHALF;
else
carousel_myAds.PeekAreaInsets = Constants.PEEKAREINSETS;

ScrollIntoView is not working properly if element is up - Selenium?

Scroll to element works fine if element is lower position ( scroll down ) , but in case of element is upper position ( scroll up ) it don't scroll to element in view rather it scroll one less to element ( count is not fixed may it 2 or 3 , varies ) .
I have to write explicit code for scroll up for element which is up , after ScrollIntoView , this is not consistent as scroll up count is not fixed .
I used this 2 codes one by one , but both gives same behavior
Code1 :
// Scroll to view by Action
Actions actions = new Actions(driver);
actions.MoveToElement(webElement);
actions.Perform();
Code 2:
// Scroll to view by JavaScript executer
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].scrollIntoView(true);", webElement);
Please help me in this case , please suggest what I have to with this .
Referrence: scrollIntoView vs moveToElement You have more control on how the element is scrolled with scrollIntoView than with moveToElement. Selenium is only interested in bringing the element into view so that the mouse can be placed on it. It does not give you any say in how it is going to do it. scrollIntoView however allows you, for instance, to specify whether you want the top or bottom of the element to be align with its scrollable ancestor. Check this for more details: https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView
You can try double MoveToElement(). First scroll to the element parent and than to the element itself
actions.MoveToElement(parentElement).MoveToElement(webElement).Perform();

Widget integrated going out of its own zone

I got a main window (Item) with a 640*480 size.
I integrated a custom widget defined in another qml file, its size is 100*100. This item contains a Pathview that only shows 3 items from a list containing a lot of items.
The thing is that when I add my CustomPathView to my main window at coords (0, 0), the PathView goes to the bottom of my main window, displaying 7/8 items... (?!?)
My CustomPathView.qml file starts with :
Item {
width: 100
height: 100
That's why I really don't understand why it is displaying 8 items...
Did I do something wrong ? Do I have to precise to my CustomPathView that he can't go out of its zone (how ?) ?
Thanks in advance for any help about it !
Just set the clip property to true.