Scrolling in GridViewPager on Android Wear - android

I am currently designing a wearable app that uses a GridViewPager with 4 rows and 5 columns.
I have several questions regarding the scrolling:
Is it possible with a Java function to go directly to a specific line and column? I tried scrollTo but it isn't working.
Is it possible when changing line to always go in the second column or to stay in the same one instead of always coming back to the first one?
Or is it possible to go to another layout when swiping from left to right when on the first column instead of dismissing the app but with the ability to dismiss the app from this new activity?

For part 2 of your question, you can override getCurrentColumnForRow(int row, int currentColumn) in a custom GridPagerAdapter and set it to return currentColumn. That will allow it to stay in the same column when changing rows.

Regarding part 1 of your question:
scrollTo is used to change a view's position on the screen.
You can call GridViewPager.setCurrentItem(row,col) instead.

Related

Android Studio Single activity acting as many

I have looked everywhere but don't seem to be able to find what I am looking for. I have an activity with multiple buttons, each button opens a new activity with an identical recyclerview layout, but different data. I am wondering if it is possible to use one activity and layout instead of multiple? this way instead of having 10+ activities (one for each button) I only have to manage one when a button is clicked and simply pass the necessary list data to it.
I believe you could set the intents for each button to call the same activity but with an integer such as 1-10, in the activity it takes the value and decides which list should be presented? If anyone thinks of how this could be done or a simpler way I would greatly appreciate it!
Yes, there are various ways to do that.
You could use multiple fragments on the same activity. Then add/remove fragments on each button click.
You can have multiple layouts within your activity. Say you have two buttons, and you have three layouts layout1, layout2, layout3, sequentially one after the another. So if initially, layout1 is visible and the rest are gone using layout.setVisiblity(View.GONE), if you click button1, ypu can do layout1.setVisiblity(View.GONE); layout3.setVisiblity(View.GONE); layout2.setVisiblity(View.VISIBLE) and vice-versa for pressing button2.
Are all the activities opened by the buttons similar? If so, you could only take care of the changes in the elements of the layout & specify conditions.
For instance, if you click a button, instead of changing the whole thing, you only go into the buttons & change their texts with btn.setText("..."). You could define different conditional statements inside the onClickListener of that button.
It could be something like:
if(btn.getText().equals("a certain text that you set to the button")){
doSomething();
else if(btn.getText().equals("another option")){
doSomethingElse();
Following this logic, you could continually update the elements in your layout & your code will decide what to do depending on what's stored in these elements.
The second option that comes to my mind would be creating different xml layout files & simply changing the layout of your MainActivity to the appropriate one depending on what stage of your process you are at.
I hope this helps,

Access Adapter items of Recyclerview

I'm using Recyclerview to show a list. I want to delete some items like IOS. In my listview template I have added a button to delete item which is invisible by default. In my activity I have another button attached at bottom (Not part of listview) and on tap of this button I want to make all delete buttons of listview visible.
My Question is how can I get reference to all delete buttons of listview in activity and is it the right way to do this?
Thanks
Assuming you have ViewHolders set up, you already have references to all the buttons in your list. All you have to do is to make them visible for every item in the list with a simple loop.
In case you haven't implemented ViewHolders I suggest you check out the documentation and take a look at some simple tutorials on how to use them.
On a side note. If I understood correctly you're making a bottom tab for your app and since you referenced iOS I gotta say this; Remember that Android and iOS are two unique operating systems with their own ways of handling things. Check out Googles pure Android documentation.
In your question title you say RecyclerView, but in your text you say ListView. The solution is similar either way, but it's best to be perfectly clear what you're doing.
In either case, there are at least two different solutions.
First, you could use a boolean flag to determine if all the the item buttons should be showing or not. You check this flag at the time the item view is inflated or created and toggle the button accordingly. If the boolean flag is ever changed, the easiest thing to do is tell the RecyclerView/ListView that the underlying data has changed and to redraw all the views. Call notifyDatasetChanged on the adapter.
The other thing you can do at the time the item buttons should change is iterate all the visible item views, find the button, and change its visibility. With RecyclerView, you can do this, and with ListView you can do this.

Avoiding lag in displaying complex views with ScrollView

For the development of my app, I realized I needed a complicated view (let's call it foo), it contains three ImageButtons, a progress bar, and three TextViews, all of which are dynamically changed by interacting with the same view's elements. To make this work, I extended foo from RelativeLayout, dynamically created the sub-views then added them to foo (this.addView(...)).
What I planned to do next was add them dynamically to a ScrollView. I did this and put three foos for testing. The result was extreme lag. I'd press an ImageView (which should change its image on press), and it would take 2 seconds to do so.
My final aim would be to support 50 of these foos at a time and have them work smoothly, with the user having the option of loading more (without overwriting the previous ones) if he/she so chooses. All interactions will use the internet (I dunno if that's relevant), but the testing was done with all the network tasks commented out.
My questions are thus:
Is the strategy I was using (ScrollView & add foos to them) viable, and the lag is from some other issue (the specific code in question, in which case I'll provide some code)? Or is it really a bad idea to do that?
What would be the best way to reach my goal here (assuming 1 is bad)?
What I already know:
I've researched my problem a bit, and most online sources recommend using a ListView. I didn't read much into it but from what I got:
I'd have to redo the design using xml rather than dynamically
The different components and their values will be stored each on it's own array which is extremely unacceptable in my situation (changing the sub-view's values should be done very simply and should not appear in the main activity)
I can't (or it's difficult to) set OnClickListener's for the different sub-views (as only the main foo view will get one)
I also tried this method (ScrollView and add to Views) with another View and had 20 of them run at the same time seamlessly, but that one had been extended from View and only used canvas to draw text with no sub-views.
Thanks in advance.

FragmentGridPagerAdapter i and i2 values

I have a question about the FragmentGridPagerAdapter. When I override the public Fragment getFragment(int i, int i2) function and log the i and i2 values, i get these strange results:
When i swipe right i2 get's increased, so it goes 0, 1, 2 etc. That makes sense.
But let's say I swiped 6 times to the right and then 1 below I would expect i2 to be 6 and i to be 1. However, i2 get's reset to the first position immediately to the first position. Does anyone know what causes this or what's the logic behind this is?
This isn't an issue with the getFragment API, this is the actual behavior of the GridViewPager element. It doesn't really behave like a grid - swiping up or down always starts at the first column.
Presumably this was implemented to enforce Google's design guidelines for a 2D picker, which says "Navigation should be vertical-then-horizontal, not horizontal-then-vertical."
Just to make it more readable: the i should be named row and i2 should be called column.
But to the point:
The GridViewPager is implemented to help you reproduce the Android Wear Home stream behavior. As you know, there are notification cards in the first columns and actions to the right hand side. If you have scrolled to the right to second or third action page for particular notification it would be very confusing to the user if (after swiping up or down) he would land in the middle of action pages for completely different notification. He would see the some action but it would be hard to guess what notification it refers to. So they implement it that way so it resets the current column and returning always to the column with index 0.
This behavior is presented in a video about Building Advanced UIs for Android Wear.
About 2:50 this topic is started (the actual explanation is near 3:25 if you want to skip), but I recommend to watch the entire video.
To change this behavior you simply need to override getCurrentColumnForRow(int row, int currentColumn) method in your FragmentGridPagerAdapter (or just GridViewAdapter).
Here is the documentation for that method:
Returns the column to arrive at when navigating vertically to the
specified row.
The default implementation simply returns 0.
Parameters
row the row in question
currentColumn the column which is currently centered
Returns
the column to arrive at
To achieve what you want you just need to return currentColumn, or maybe do some check before returning it to see if this position is valid for the new row.

How to make list view scrollable until the last element is on top of the screen

I am developing an application that needs to show calendar agenda, much like the agenda in the native calendar. I have a list view showing different events (Note: in the context of the question events is entity of my system). I want to provide a 'Today' button on this screen. When the user clicks on this button the events are supposed to be scrolled until the first event of the current's day schedule is on top of the screen. The problem occurs when I have only a few events scheduled from today on - so few that they do not fill a whole screen. Then the list view just scrolls until the last event in the calendar is on the bottom. This usually means that the desired effect of having the first today's event on top is not achieved.
Any suggestions how this can be done? I have thought of adding some blank elements at the end, but this seems ugly workaround, and furthermore it will require special device-specific calculations that will tell me how many elements to insert.
Edit:
Adding some code as requested in comment
Actually I am not sure this code will surprise anyone, but:
public void onTodayClicked(View target) {
// calculate the indexOf. It works and is not related to the question
if (indexOf >= 0) {
ListView list = (ListView) findViewById(R.id.events_list_view);
list.setSelection(indexOf);
}
}
I am not sure the layout definition is important to aid the answering of the question, but if you think so I can add it too.
You can achieve this by two ways:
call smoothScrollToPositionFromTop method
call setSelectionFromTop method
Using the smoothScroll method is better, because it actually does the transition smoothly - that means it really scrolls to it.
The only downside is that it's only available after API level 11.
The setSelectionFromTop method works since API level 1, but instead of smoothly scrolling, it jumps to the row.
If you don't need to position to the top of the screen, only to view the row, you can also use smoothScrollToPosition, which is an API level 8 call.
If you give these methods the position, which is the FIRST in the list, they will work well. (From your description I think you probably calculate the last position, but I can't be sure).

Categories

Resources