RecyclerView show many items and make current item in center - android-recyclerview

Like this.
Show three items at screen, but left and right item show only edge
When index = 0, show first item at center
When index = n, show current item at center
|-----------------------------------|
| |
| |
| |---------------| |----|
| | | | |
| | | | |
| | | | |
| | | | |
| | 1 | | 2 |
| | | | |
| | | | |
| | | | |
| | | | |
| |---------------| |----|
| |
| |
|-----------------------------------|
|-----------------------------------|
| |
| |
|----| |---------------| |----|
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| 1 | | 2 | | 3 |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
|----| |---------------| |----|
| |
| |
|-----------------------------------|
I have try SnapHelper, but I don't known how

Take LinearLayout in your RecyclerView's item row layout then give android:layout_gravity="center" to LinearLayout.

Basically there are two layout files which are required to be created, one is activity_main.xml and another one is the layout that shows each item in the RecyclerView. Consider the name of the layout file to be layout_r.xml.
activity_main.xml
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent" <!--This can be changed to wrap_content or match_parent, even though match_parent makes single view on the screen-->
android:layout_height="wrap_content">
</RecyclerView>
</LinearLayout>
layout_r.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/tvText"
android:text="Add text here"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<!--............And rest of the views..............-->
</LinearLayout>

In order to center the first and the last items, you need to create extra space at the beginning and end of your RecyclerView. An easy way to do so is to use an ItemDecoration, like so:
class BoundsOffsetDecoration : ItemDecoration() {
override fun getItemOffsets(outRect: Rect,
view: View,
parent: RecyclerView,
state: RecyclerView.State) {
super.getItemOffsets(outRect, view, parent, state)
val itemPosition = parent.getChildAdapterPosition(view)
// It is crucial to refer to layoutParams.width (view.width is 0 at this time)!
val itemWidth = view.layoutParams.width
val offset = (parent.width - itemWidth) / 2
if (itemPosition == 0) {
outRect.left = offset
} else if (itemPosition == state.itemCount - 1) {
outRect.right = offset
}
}
}
And for the "center" effect you should use a PagerSnapHelper:
PagerSnapHelper().attachToRecyclerView(recyclerView)
I've written a Medium post describing a step-by-step implementation of this kind of carousels using RecyclerView and SnapHelper, if you need more details.

Related

Suggested naming conventions for Selenium identifiers

I am using selenium page object model to define all the page elements. I am little unconvinced with the naming conventions that I have followed for naming the element and felt too long. Please suggest on this.
#FindBy(xpath = "//tbody[#id='tabview:listComp1_data']/tr/td[1]/div/div[2]")
public WebElement tableCompanyResultsRow;
#FindBy(xpath = ".//*[#id='mttAddId']")
public WebElement buttonAddMap;
#FindBy(xpath = ".//*[#id='ittAddId']")
public WebElement buttonAddItinerary;
#FindBy(xpath = "//div[#id='ajaxStatusPanel_start']/img")
public WebElement imageLoading;
I have been using the below naming conventions for a long time and it helps understand and distinguish the UI elements.
For locators I always prefix loc_ to the variable name. For example: loc_btnExit, etc.
+----------+----------------------------+--------+-----------------+
| Category | UI/Control type | Prefix | Example |
+----------+----------------------------+--------+-----------------+
| Basic | Button | btn | btnExit |
| Basic | Check box | chk | chkReadOnly |
| Basic | Combo box | cbo | cboEnglish |
| Basic | Common dialog | dlg | dlgFileOpen |
| Basic | Date picker | dtp | dtpPublished |
| Basic | Dropdown List / Select tag | ddl | ddlCountry |
| Basic | Form | frm | frmEntry |
| Basic | Frame | fra | fraLanguage |
| Basic | Image | img | imgIcon |
| Basic | Label | lbl | lblHelpMessage |
| Basic | Links/Anchor Tags | lnk | lnkForgotPwd |
| Basic | List box | lst | lstPolicyCodes |
| Basic | Menu | mnu | mnuFileOpen |
| Basic | Radio button / group | rdo | rdoGender |
| Basic | RichTextBox | rtf | rtfReport |
| Basic | Table | tbl | tblCustomer |
| Basic | TabStrip | tab | tabOptions |
| Basic | Text Area | txa | txaDescription |
| Basic | Text box | txt | txtLastName |
| Complex | Chevron | chv | chvProtocol |
| Complex | Data grid | dgd | dgdTitles |
| Complex | Data list | dbl | dblPublisher |
| Complex | Directory list box | dir | dirSource |
| Complex | Drive list box | drv | drvTarget |
| Complex | File list box | fil | filSource |
| Complex | Panel/Fieldset | pnl | pnlGroup |
| Complex | ProgressBar | prg | prgLoadFile |
| Complex | Slider | sld | sldScale |
| Complex | Spinner | spn | spnPages |
| Complex | StatusBar | sta | staDateTime |
| Complex | Timer | tmr | tmrAlarm |
| Complex | Toolbar | tlb | tlbActions |
| Complex | TreeView | tre | treOrganization |
+----------+----------------------------+--------+-----------------+

Changing the orientation of a SplitView

I'm making a UWP (Windows 10) app. I'd like to know, is it possible to change the orientation of a SplitView? Typically, it's ordered like this:
______________________________________________
| | |
| | |
| | |
| | |
| | |
| Pane | Content |
| | |
| | |
| | |
| | |
| | |
----------------------------------------------
Is it possible to change the orientation to:
______________________________________________
| |
| |
| Pane |
| |
| |
| |
----------------------------------------------
| |
| |
| |
| |
| Content |
| |
| |
| |
----------------------------------------------
It is not supported by the platform (SplitVew.PanePlacement property can only be left or right).
You can likely achieve a somewhat similar affect by placing a command bar at the top of your application.

Programmatic autolayout issues

I'm attempting to implement a dynamic layout using autolayout constraints; for that purpose I have created 3 container views. The first one that is 49 height, flexible width. The second one that is 320 height and 320 width and the last one that is 69 height flexible width. So here's the code that is supposed to place each view below the other.
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(searchControlsView, workArtView, playBackControlsContainer);
// Position time search controls
constraints = [NSLayoutConstraint constraintsWithVisualFormat:#"|[searchControlsView]|"
options:0
metrics:nil
views:viewsDictionary];
[self.view addConstraints:constraints];
constraints = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|[searchControlsView]"
options:0
metrics:nil
views:viewsDictionary];
[self.view addConstraints:constraints];
// position workArt
constraints = [NSLayoutConstraint constraintsWithVisualFormat:#"|[workArtView]|"
options:0
metrics:nil
views:viewsDictionary];
[self.view addConstraints:constraints];
constraints = [NSLayoutConstraint constraintsWithVisualFormat:#"V:[searchControlsView]-0-[workArtView]"
options:0
metrics:nil
views:viewsDictionary];
[self.view addConstraints:constraints];
// position bottom controls
constraints = [NSLayoutConstraint constraintsWithVisualFormat:#"|[playBackControlsContainer]|"
options:0
metrics:nil
views:viewsDictionary];
[self.view addConstraints:constraints];
constraints = [NSLayoutConstraint constraintsWithVisualFormat:#"V:[workArtView]-(>=0)-[playBackControlsContainer]|" options:0 metrics:nil views:viewsDictionary];
[self.view addConstraints:constraints];
What I want to achieve is the last view to be below the 2nd one If possible, if not the third view will be aligned at the bottom of entire screen and overlap the 2nd one. However, the third view (playBackControlsContainer) is not being shown in screen.
Previous to this I removed all the constraints from the view. I tried several things but I just starting with programmatic constraints and I am not able to realize where I am failing... besides that the debug log has lots of additional views and without and identifier for the addresses of each control it's really difficult to figure out why my layout might be ambiguous. Here's the debug log in case is useful.
*<UIWindow:0xc1a6510> - AMBIGUOUS LAYOUT
| *<UILayoutContainerView:0xc1b6b50>
| | *<UITransitionView:0xc1a3da0>
| | | *<UIViewControllerWrapperView:0xc3b82b0>
| | | | *<UIView:0xc1b9830>
| | | | | *<UILayoutContainerView:0xc1ba6a0>
| | | | | | *<UINavigationTransitionView:0xc1bf490>
| | | | | | | *<UIViewControllerWrapperView:0xc1c5110>
| | | | | | | | *<UIView:0xb478820> - AMBIGUOUS LAYOUT
| | | | | | | | | *<UIView:0xb4f9d70> - AMBIGUOUS LAYOUT
| | | | | | | | | | *<UIButton:0xb4f3e40>
| | | | | | | | | | | <UIImageView:0xc4e7030>
| | | | | | | | | | *<UIButton:0xb4f5440>
| | | | | | | | | | | <UIImageView:0xc14b0a0>
| | | | | | | | | | | *<UIButtonLabel:0xb4fb610> - AMBIGUOUS LAYOUT
| | | | | | | | | | *<UIButton:0xb4f0e80>
| | | | | | | | | | | <UIImageView:0xc1f00b0>
| | | | | | | | | | | *<UIButtonLabel:0xb4fac70> - AMBIGUOUS LAYOUT
| | | | | | | | | | *<UIButton:0xb4f04f0>
| | | | | | | | | | | <UIImageView:0xc129480>
| | | | | | | | | | | *<UIButtonLabel:0xb4fa2e0> - AMBIGUOUS LAYOUT
| | | | | | | | | *<UIView:0xb478790>
| | | | | | | | | | *<UIImageView:0xb4f9b60>
| | | | | | | | | | *<LVStopWatchLabel:0xb479200>
| | | | | | | | | | | <UIImageView:0xb478920>
| | | | | | | | | *<UIView:0xb479940>
| | | | | | | | | | *<UIButton:0xb4f6ab0>
| | | | | | | | | | | <UIImageView:0xc4e6c00>
| | | | | | | | | | *<UILabel:0xb479db0>
| | | | | | | | | | *<UISlider:0xb4f5e90>
| | | | | | | | | | | <UIView:0xc4e05e0>
| | | | | | | | | | | | <UIView:0xc4e0760>
| | | | | | | | | | | | | <UIView:0xc4e0880>
| | | | | | | | | | | | <UIView:0xc4e5c40>
| | | | | | | | | | | <UIImageView:0xc4e67d0>
| | | | | | | | | | | | <UIImageView:0xc4e5d40>
| | | | | | | | | *<_UILayoutGuide:0xb478440> - AMBIGUOUS LAYOUT
| | | | | | | | | *<_UILayoutGuide:0xb4782a0> - AMBIGUOUS LAYOUT
| | | | | | <UINavigationBar:0xc1c4a10>
| | | | | | | <_UINavigationBarBackground:0xc1c7530>
| | | | | | | | <UIImageView:0xc1c7810>
| | | | | | | <UINavigationItemView:0xc4cf610>
| | | | | | | | <UILabel:0xc4dcae0>
| | | | | | | <UINavigationItemButtonView:0xc4dcf10>
| | | | | | | | <UILabel:0xc4dcfe0>
| | | | | | | <_UINavigationBarBackIndicatorView:0xc3c3880>
| | | | | <GADBannerView:0xc3de310>
| | | | | | <GADWebView:0xc3de440>
| | | | | | | <_UIWebViewScrollView:0xc3e01a0>
| | | | | | | | <UIWebBrowserView:0xd280600>
| | | | | <ADBannerView:0xc19ad20>
| | | | | | <ADRemoteView:0xc19cca0>
| | | | | | <UIView:0xc19d590>
| | <UITabBar:0xb4a47e0>
| | | <_UITabBarBackgroundView:0xc47db60>
| | | | <_UIBackdropView:0xc47d240>
| | | | | <_UIBackdropEffectView:0xc47e490>
| | | | | <UIView:0xc47e530>
| | | <UITabBarButton:0xc19ed40>
| | | | <UITabBarSwappableImageView:0xc17ee00>
| | | | <UITabBarButtonLabel:0xc19f170>
| | | <UITabBarButton:0xc1a4120>
| | | | <UITabBarSwappableImageView:0xc1a3570>
| | | | <UITabBarButtonLabel:0xc1a3e60>
| | | <UITabBarButton:0xc1a62b0>
| | | | <UITabBarSwappableImageView:0xc1a6660>
| | | | <UITabBarButtonLabel:0xc1a6390>
| | | <UITabBarButton:0xc1a8a30>
| | | | <UITabBarSwappableImageView:0xc1a9080>
| | | | <UITabBarButtonLabel:0xc1a8f60>
| | | <UIImageView:0xc47ead0>
Any help to figure out what might be the reason for this I would be really thankful.
Thanks.
I think you're forgetting to set the heights of your views.
For example,
constraints = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|[searchControlsView(49)]" options:0 metrics:nil views:viewsDictionary];

Setting the position of a tab Scrollbar when updaing the VerticalScroll Value

I am updating a number of controls dynamically on a tabpage within a c# application. The tab page has autoscroll enabled. As you may be aware, when the user has scrolled down it sets the location x=0,y=0 to be at the current x=0,y=0.
so:
+-----------------------------------+
| X |---| X is now 0,0 in terms of setting locations.
| | - |
| |---|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| TEXT | |
| | |
+-----------------------------------+
AND
+-----------------------------------+
| X | | X is now 0,0 in terms of setting locations.
| TEXT | |
| | |
| | |
| | |
| |---|
| | - |
| |---|
| | |
| | |
| | |
| | |
+-----------------------------------+
Therefore I have to save the VerticalScroll.Value, set it to 0, update the controls and then set the VerticalScroll.Value back to the original value.
This works to a degree, the position of the tab form is correct, however the scrollbar doesn't update, so it looks like this:
+-----------------------------------+
| |---|
| TEXT | - |
| |---|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
+-----------------------------------+
Is there any easy way to update the position of the scrollbar?
If I leave it as it is, when the user clicks on the scrollbar again it moves the position back to the top of the tab form. Thanks for any help you can provide.
To answer my own question:
http://msdn.microsoft.com/en-us/library/system.windows.forms.scrollablecontrol.autoscrollposition.aspx

What are segments in Lucene?

What are segments in Lucene?
What are the benefits of segments?
The Lucene index is split into smaller chunks called segments. Each segment is its own index. Lucene searches all of them in sequence.
A new segment is created when a new writer is opened and when a writer commits or is closed.
The advantages of using this system are that you never have to modify the files of a segment once it is created. When you are adding new documents in your index, they are added to the next segment. Previous segments are never modified.
Deleting a document is done by simply indicating in a file which document of a segment is deleted, but physically, the document always stays in the segment. Documents in Lucene aren't really updated. What happens is that the previous version of the document is marked as deleted in its original segment and the new version of the document is added to the current segment. This minimizes the chances of corrupting an index by constantly having to modify its content when there are changes. It also allows for easy backup and synchronization of the index across different machines.
However, at some point, Lucene may decide to merge some segments. This operation can also be triggered with an optimize.
A segment is very simply a section of the index. The idea is that you can add documents to the index that's currently being served by creating a new segment with only new documents in it. This way, you don't have to go to the expensive trouble of rebuilding your entire index frequently in order to add new documents to the index.
The segment benefits have been answered already by others. I will include an ascii diagram of a Lucene Index.
Lucene Segment
A Lucene segment is part of an Index. Each segment is composed of several index files. If you look inside any of these files, you will see that it holds 1 or more Lucene documents.
+- Index 5 ------------------------------------------+
| |
| +- Segment _0 ---------------------------------+ |
| | | |
| | +- file 1 -------------------------------+ | |
| | | | | |
| | | +- L.Doc1-+ +- L.Doc2-+ +- L.Doc3-+ | | |
| | | | | | | | | | | |
| | | | field 1 | | field 1 | | field 1 | | | |
| | | | field 2 | | field 2 | | field 2 | | | |
| | | | field 3 | | field 3 | | field 3 | | | |
| | | | | | | | | | | |
| | | +---------+ +---------+ +---------+ | | |
| | | | | |
| | +----------------------------------------+ | |
| | | |
| | | |
| | +- file 2 -------------------------------+ | |
| | | | | |
| | | +- L.Doc4-+ +- L.Doc5-+ +- L.Doc6-+ | | |
| | | | | | | | | | | |
| | | | field 1 | | field 1 | | field 1 | | | |
| | | | field 2 | | field 2 | | field 2 | | | |
| | | | field 3 | | field 3 | | field 3 | | | |
| | | | | | | | | | | |
| | | +---------+ +---------+ +---------+ | | |
| | | | | |
| | +----------------------------------------+ | |
| | | |
| +----------------------------------------------+ |
| |
| +- Segment _1 (optional) ----------------------+ |
| | | |
| +----------------------------------------------+ |
+----------------------------------------------------+
Reference
Lucene in Action Second Edition - July 2010 - Manning Publication