Snapping RecyclerView for scrolling one item at a time, using FocusLayoutManager - java

I am using FocusLayoutManager for RecyclerView. Everything is fine but I need to swipe only one item at a time. I have also tried LinearSnapHelper and others too, i.e. SnapHelperOneByOne but didn't helped. Also tried to modify the focus layout manager class itself but all in vain. Any help would be appreciated.
Link for FocusLayoutManager
Link for SnapHelperOneByOne
Link for FocusLayoutManagerLibrary
Note: I have tried to attach the SnapHelper to RecyclerView after layout manager is assigned.
val focusLayoutManager = FocusLayoutManager.Builder()
.focusOrientation(FocusLayoutManager.FOCUS_TOP)
.isAutoSelect(true)
.maxLayerCount(1)
.setOnFocusChangeListener { focusdPosition, lastFocusdPosition -> }
.build()
myRecycler.layoutManager = focusLayoutManager
Using Snap Helper doesn't help also and makes the scrolling lagging (jerks)
val mySnapHelper = SnapHelperOneByOne()
mySnapHelper.attachToRecyclerView(myRecycler)

Got stuck with the same issue. Set the focus change listener code like below. (Found one work around for Java)
.setOnFocusChangeListener((focusdPosition, lastFocusedPosition) -> {
if (focusdPosition == lastFocusedPosition - 1 || focusdPosition == lastFocusedPosition + 1) {
myRecycler.stopScroll();
}
})
This code will stop the scroll for the RecyclerView, if the current focused item is after or before the previous focused item then it will stop the scroll.
Remove the SnapHelper because it creates a scroll animation lag with FocusLayoutManager.

Related

scrollView.fullScroll(View.FOCUS_DOWN); always is triggered

OK,i have ScrollView with multiply recyclerview-s.When user click on some item on my list that item expand itself with some more info. The problem is that although it does expand correctly, my root view(in my case ScrollView) does not follow that expansion automatically ,so i need to scroll manually to see expanded item.So i used scrollView.fullScroll(View.FOCUS_DOWN) in onClick in my recyclerview.And it works fine but after this line of code is triggered every other click on some other recycelerView it scroll down although that recyclerview does not have fullScroll().
I tried to use scrollTo(),smoothScrollTo etc.I dont use ExpandableRecyclerView but cachapa/ExpandableLayout and in this thread developer said that it is rather the parent View "issue".Also tried solution from the same thread but problem persist.
So my question is how to stop triggering fullScroll(View.FOCUS_DOWN) when i click on other recuclerviews items.
my code:
` animaton.setOnClickListener(v -> {
notifyDataSetChanged();
//some stuff
scrollView.post(new Runnable() {
public void run() {
scrollView.fullScroll(View.FOCUS_DOWN);
}
});
});`

Espresso perform click on view in Recyclerview item

I tried everything to perform click on view in recyclerview item but not succesfull. Read every option on internet and tried it but still not working. Lately I used:
onView(ViewMatchers.withId(R.id.live_rw_liveMatchList))
.perform(RecyclerViewActions.actionOnItemAtPosition(2, MyViewAction.clickChildViewWithId(R.id.pick_pw_pickLeft)));
But all i get is:
android.support.test.espresso.AmbiguousViewMatcherException: 'with id:
hr.psk.android:id/ticket_list_list' matches multiple views in the
hierarchy. Problem views are marked with '****MATCHES****' below.
And it matches only one view (my recyclerview with 13 childs)
I know this should work but it is not working in my project. Tried to perform only click on recyclerviw item in other recyclerview to make it more simple like this:
onView(withId(R.id.ticket_list_list))
.perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));
But it gives me the same message back -> Multiple matches problem
somebody help, I really tried everything
onView(withId(R.id.ticket_list_list))
.perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));
The error you're getting means that there's another View somewhere that also uses the id ticket_list_list
Is ticket_list_list your actual list view? (the parent of the things that you want to click)
If it is then is it the only one available on the screen? if it is then use the following
onView(allOf(withId(R.id.ticket_list_list), isDisplayed()))
.perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));
Adding the isDisplayed() Matcher makes the targeted View more specific by targeting only those that are displayed and ignoring those that are existing but not displayed
If there really is another matcher displayed (or the above doesn't work for you) that has the same id as the one you're trying to match then use the following
onView(allOf(withId(R.id.ticket_list_list), withParentIndex(index_of_matcher_here)))
.perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));
Instead of isDisplayed() withParentIndex(some_number) should more specifically target and give you the view that has the id you're looking for along with the index of it
In the very messy instance that the above still doesn't work, maybe because all the returned Views have the same index possibly because they are the children of another View then you can get creative and do something like
onView(allOf(withId(R.id.ticket_list_list), withParent(allOf(withId(if_of_the_parent), withParentIndex(index_of_matcher_here)))))
.perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));
to specify the index of the parent instead.
Just mix 'n match your way through it, just make sure to not go crazy on the Matchers and just keep things readable
Use as little as you can (make sure it's readable and maintainable too and not just made up of indexes), you're not trying to get the coordinates for a missile target- you just want the View
I also face the same problem before few days but finally got the solutions from StackOverflow.
If you want to click recycler custom item view click then implement the following code in your project
You need to write this code in java this will help you a lot and saved your time
ClickOnButtonView is control a view action happened in the list item and
UiController class helps you find custom row item
The code is written in Kotlin
#RunWith(AndroidJUnit4::class)
class CampaignFragmentTest {
#get: Rule
val activityTestRule = ActivityTestRule(TestActivity::class.java)
#Test
fun testCustomListClick() {
clickOnButtonAtRow(0)
}
}
private fun clickOnButtonAtRow(position: Int) {
Espresso.onView(ViewMatchers.withId(R.id.list)).perform(RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>
(position, ClickOnButtonView()))
}
inner class ClickOnButtonView : ViewAction {
internal var click = ViewActions.click()
override fun getConstraints(): Matcher<View> {
return click.constraints
}
override fun getDescription(): String {
return " click on custom button view"
}
override fun perform(uiController: UiController, view: View) {
//btnClickMe -> Custom row item view button
click.perform(uiController, view.findViewById(R.id.btnClickMe))
}
}
That error means that it found multiple views with the id R.id.ticket_list_list. There should also be a view hierarchy log that follows the error message you provided, showing the views that Espresso found with that same id marked with ****MATCHES****.
So you can either change the id of the view you want to test or target the view by providing another ViewMatcher as below:
onView(allOf(withId(recyclerViewID), viewMatcher))
.perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));
You Can also create your own custom click action like this :
fun customActionClickOnItemEvent(
#NonNull targetViewId: Int
): ViewAction {
return object : ViewAction {
val click = ViewActions.click()
override fun getDescription(): String = "Item clicked"
override fun getConstraints(): Matcher<View> = click.constraints
override fun perform(uiController: UiController?, view: View?) {
click.perform(uiController,view?.findViewById(targetViewId))
}
}
}
And Calling this function like this :
onView(withId(R.id.rv_products)).perform(
RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(
1,
customActionClickOnItemEvent(R.id.img_wish_list)
)
)

Android - RecyclerView with one layout, multiple setVisibility

I have a basically all in one layout which has everything needed for my app's main feed. All variable items (images, video thumbnails.. Etc.) are set to GONE at first and set to VISIBLE when it is needed.
The problem is sometimes, might be due to RecyclerView's recycling behavior, the item which is supposedto be GONE is VISIBLE in the wrong places.
Example :
Item no 1 contains Text
Item no 2 contains Image
Item no 3 contains Image
I keep scrolling down to item no x, then scroll back up and here's what I get :
Item no 1 contains Image from item no x, sometimes item no 3
Item no 2 contains Image
Item no 3 contains Image
I'm using a custom ViewHolder which extends RecyclerView.ViewHolder.
The purpose of the CustomViewHolder is for layout declaration and initialization.
ProgressBar progressBar;
View viewDimmer;
RelativeLayout postListWrapper;
...
public ObjectViewHolder(View v) {
super(v);
progressBar = (ProgressBar)v.findViewById(R.id.post_inscroll_progressBar);
viewDimmer = (View)v.findViewById(R.id.post_inscroll_viewDimmer);
postListWrapper = (RelativeLayout)v.findViewById(R.id.post_inscroll_postListWrapper);
}
An example of how I load the image :
Picasso.with(context)
.load(youtubeThumbnailUrl)
.fit()
.centerCrop()
.into(
((ObjectViewHolder) holder).userPostYoutubeImage
);
I've set each visibility to GONE if no url is obtained from the server
((ObjectViewHolder) holder).userPostImageWrapper.setVisibility(View.GONE);
((ObjectViewHolder) holder).userPostYoutubeImageWrapper.setVisibility(View.GONE);
But somehow the image is still reused on the previous items (yes, not only Item no 1). Sometimes image are also in the wrong ImageView. Image D is supposed to be in ImageView D, but it's in ImageView A instead.
Any guide for setting RecyclerView up and going nicely?
If I miss anything, or need to supply more code, please do inform me :D
You need to put the else condition too. Like the example below.
// if no url is found from server
if(url == null){
((ObjectViewHolder) holder).userPostImageWrapper.setVisibility(View.GONE);
((ObjectViewHolder) holder).userPostYoutubeImageWrapper.setVisibility(View.GONE);
} else {
// Some url has found
((ObjectViewHolder) holder).userPostImageWrapper.setVisibility(View.VISIBLE);
((ObjectViewHolder) holder).userPostYoutubeImageWrapper.setVisibility(View.VISIBLE);
}
Do this for each of the items you've got there as the list item in case of you're setting their visibilities in runtime.
All your if conditions in onBindViewHolder() must have an else block too.
Don't leave any if condition without else. You can provide a default behaviour in the else block when if condition becomes false.

Robotium - Trying to click home button in app

I'm new to robotium and i'm trying to write a quick and dirty script to run through all screens in an app.
The problem i have mainly with the 'home button' in the app. I've tried lots of options but i cant seem to get it to click there except with index, which is not what i want.
When i check out the button with the hierarchyviewer it looks like this:
Link
However when i try for example:
assertTrue(
"Wait for text (id: myapp.R.id.home) failed.",
solo.waitForImageById("myapp.R.id.home", 20000));
solo.clickOnImage((ImageView) solo.findViewById("myapp.R.id.home"));
solo.waitForActivity("MenuActivity");
It fails at the waitForImageByID line. Ive tried multiple options like waitForImageButton etc, but i just cant seem to get it clicked. What am i missing here?
junit.framework.AssertionFailedError: View with id: '0' is not found!
at com.jayway.android.robotium.solo.Solo.getView(Solo.java:1990)
at com.jayway.android.robotium.solo.Solo.getView(Solo.java:1970)
at com.bitbar.recorder.extensions.OtherUtils.a(OtherUtils.java:246)
at com.bitbar.recorder.extensions.OtherUtils.b(OtherUtils.java:241)
at com.bitbar.recorder.extensions.v.a(Waiter.java:71)
at com.bitbar.recorder.extensions.ExtSolo.waitForImageButtonById(ExtSolo.java:4176)
at com.example.android.apis.test.Test.testRecorded(Test.java:137)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1740)
Use the following line to press the home button in the action bar
solo.clickOnActionBarHomeButton();
The issue is that the id that it is referring is not in your application, it is in androids default R file, try android.R.id.home and it should work fine. It is worth noting though that if your application uses action bar sherlock to support the action bar pre 4.0 that this will have a different ID there and you will have to handle this in your test.
You can see this for yourself looking at: http://developer.android.com/reference/android/R.id.html
When you are using ActionBarSherlock there are two different Ids you have to check, android.R.id.home for API-Level>11 and abs__home for lower levels (provided by ActionBarSherlock):
View homeButton = activity.findViewById(android.R.id.home);
if (homeButton == null) {
homeButton = activity.findViewById(R.id.abs__home);
}
What about this code:
ArrayList<LinearLayout> ll = solo.getCurrentViews(LinearLayout.class);
//You can change 1 with the ordinal number of LinearLayout you want to click.
solo.clickOnView(ll.get(1));
or also
ArrayList<ImageView> iv = solo.getCurrentViews(ImageView.class);
//You can change 0 with the ordinal number of Image you want to click.
solo.clickOnView(iv.get(0));
I think if you identify the correct id for view or linear layout or image view it should work.
Dave C's answer was working only partially for me. The button was clicked but before the preceding screen was loaded assertions had started and thus were always false. The solution is to run "home click" on the main thread (Robotium 5.2.1):
getInstrumentation().runOnMainSync(new Runnable() {
#Override
public void run() {
solo.clickOnActionBarHomeButton();
}
});
From your question I can see that it is an image view. You can click on any view using the following piece of code.
View view = solo.getView("View_name_from_hierachy_viewer");
solo.clickOnView(view);
View_name_from_hierachy_viewer in your case will be "home".
Let me know if this does not work.

Nested Android SlidingMenus possible?

I'm using the sliding menu library here: https://github.com/jfeinstein10/SlidingMenu/
and I have an activity that inherits from SlidingMenuActivity with a sliding menu that works perfectly, but I also want to add SlidingMenus to each row in a list fragment that is shown as part of this SlidingMenuActivity subclass. It seems that the way I'm doing it doesn't work at all; the touches get intercepted and they don't let me click on a list row, but I can't swipe the sliding menu into appearance, nor can I see the SlidingMenu when it's closed.
This is the code that I'm using to add the sliding menu to each list row:
private void makeSlidingMenu(View view) {
FrameLayout menuClosedFrame = // ... the above view
RelativeLayout menuLayout = // ... the behind view
SlidingMenu slidingMenu = new SlidingMenu(view.getContext());
slidingMenu.setContent(menuClosedFrame);
slidingMenu.setMenu(menuLayout);
slidingMenu.setBackgroundColor(Color.RED);
slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
slidingMenu.setTouchModeBehind(SlidingMenu.TOUCHMODE_FULLSCREEN);
slidingMenu.setBehindScrollScale(1.0f);
slidingMenu.setFadeDegree(0.0f);
RelativeLayout layout = (RelativeLayout)view;
layout.addView(slidingMenu, new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT) {{
addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
}});
}
It's not pretty, but it seems to get the job done on a normal activity that doesn't have a list view and is not a SlidingMenuActivity. It just doesn't work in a nested scenario with a ListView for me. Is there anything else I could be doing wrong? If posting more code would help let me know. Thanks!
Figured it out; the way I was adding the sliding menu to the row was causing it to be sized improperly I think; I fixed it by adding it to a framelayout instead that also contains the contents I want to show since i want the cell's main content to remain static.

Categories