I am working on a simple eReader app. One of the things I need to do is save the position the user was at last for each ebook. To display the ebook I am using a Recyclerview for the text (it's just a Recyclerview displaying raw text).
I am not very familiar with Recyclerview, but from what I've read it seems saving the scroll position is possible. However, in most examples it shows it being used in a save/restore state context. What I need to do is save the position for each book.
Is it possible to save the position into some variable which can be loaded when the book is opened again? The way I am handling the books is using a custom object that contains the title and the actual text of the book. I would like to save the position into a variable inside these custom objects, for each individual book. So that when that object's text is displayed, it will go back to the last position.
You can get the recycler view position by:
GridLayoutManager layoutManager = ((GridLayoutManager)mRecyclerView.getLayoutManager());
int recyclerPosition = layoutManager.findFirstVisibleItemPosition();
and then you can save the position in sharedpreferences
and in onCreate or onResume you can call:
mRecyclerView.setPosition(recyclerPosition);
First of all, I'm not sure if using Recyclerview just for the mere purpose of displaying text is right. Recyclerview is used for more sophisticated uses like displaying lists with custom views etc. You can directly use TextView for this purpose.
What you can do it, create a ScrollView, then under it create one child vertical Linear Layout and then keep the TextView as child of this LinearLayout.
Something like this :
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
</ScrollView>
Now, to know the position of the ScrollView, you can use getScrollY() on your ScrollView
holder.lay_item.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(holder.lay_item.isSelected()) {
holder.lay_item.setSelected(false);
foodAlergies.get(position).setFlag(false);
} else {
holder.lay_item.setSelected(true);
foodAlergies.get(position).setFlag(true);
}
String type="";
int child_position=-1;
itemClickListener.recyclerViewClicked(type,position,child_position);
}
});
public void recyclerViewClicked(String type, int position, int child_position) {
// String prodctid = searchdata.get(position).getId();
}
happy coding
Related
I am trying to replicate a view from iOS so that user have same look and feel throughout the android application as well.
I am having a RecyclerView with LinearLayoutManager and horizontal orientation. So far so good.
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
tools:listitem="#layout/item_recycler_view" />
With the output design:
However, in case of iOS design we have items starting from center however the horizontal view is completely scrollable (meaning the scrolling can be done to full width even if the item loading from center).
I know there is no use of adding padding/margin or using a different view like HorizontalScrollView. How can we obtain such behaviour so that i give nearly same experience to users.
Let me know if there is anything that i can provide to clarify the problem statement.
Quick solution
Add an empty item on the beginning and one on the end of your list, and make your index access account for those two extra items. That should help you get the desired effect.
Not so quick solution
Android allows us to write our own custom Layout Managers for RecyclerView. It comes with three types that will cover most of the user cases:
LinearLayoutManger (For lists in general);
GridLayoutManager (For grids);
StaggeredGridLayoutManager (For grids with items with custom sizes).
I believe you could write one to always start placing the first item on the center of the screen. That will require more work, but it won't mess with your data indexes.
Read this, and this, on how to create custom Layout Managers. Also, take a look at the docs. That should be a good place to start.
There are two ways you could do this. The simplest by far would be to add horizontal padding to your RecyclerView and set the view to not clip based on padding. Something like this:
android:paddingLeft="100dp"
android:paddingRight="100dp"
android:clipToPadding="false"
The other way would be to create an ItemDecoration and add it to your RecyclerView. You could then override the getItemOffsets() method to add a left-hand offset to your first item and a right-hand offset to your last item.
This second approach is better because it won't affect the RecyclerView's scrollbars, but it is a little more complex. Here's an example to get you started:
private static class MyItemDecoration extends RecyclerView.ItemDecoration {
#Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int parentWidth = parent.getWidth();
int childWidth = view.getWidth();
int margin = (parentWidth - childWidth) / 2;
int position = parent.getChildAdapterPosition(view);
outRect.left = position == 0 ? margin : 0;
outRect.right = position == (parent.getAdapter().getItemCount() - 1) ? margin : 0;
}
}
I think the only way will be adding different layout for first and last position in adapter of recyclerview.
It can be done using viewType parameter in createViewHolder(ViewGroup parent, int viewType)
i have a RecycleView with an adapter that show a list of servers
and the user must select one server.
when i call notifyItemChanged(previousPosition) inside the onClick() method
to make the old server unselected and the new server selected,
that's make the RecycleView list jump to up exactly in the middle of list.
and this problem happen just when i click on one of the last 2 or 3 servers inside the RecycleView list
here is the code of my RecyclerView.Adapter :
public class ServerAdapter extends RecyclerView.Adapter<ServerAdapter.ServerViewHolder> {
private List<Server> listServers = new ArrayList<>();
private int[] icons = new int[]{R.drawable.server1,R.drawable.server2,R.drawable.server3,R.drawable.server4,R.drawable.server5,R.drawable.server6,R.drawable.offline};
private int selected = 0;
private int previousSelected = 0;
public ServerAdapter(List<Server> listServers){
this.listServers = listServers;
}
#Override
public ServerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.server_relative_layout,parent,false);
return new ServerViewHolder(view);
}
#Override
public void onBindViewHolder(final ServerViewHolder holder, final int position) {
if(position == selected){
holder.getBackground().setSelected(true);
}else{
holder.getBackground().setSelected(false);
}
holder.getBackground().setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(position != selected){
previousSelected = selected;
selected = position;
holder.getBackground().setSelected(true);
notifyItemChanged(previousSelected);
}
}
});
holder.getImageServer().setImageResource(icons[position%6]);
holder.getTextNameServer().setText(listServers.get(position).getName());
holder.getTextConnected().setText(listServers.get(position).getUrl());
}
#Override
public int getItemCount() {
return listServers.size();
}
public class ServerViewHolder extends RecyclerView.ViewHolder{
private ImageView imageServer;
private TextView textNameServer;
private TextView textConnected;
private View background;
public ServerViewHolder(View itemView) {
super(itemView);
imageServer = (ImageView)itemView.findViewById(R.id.imageServer);
textNameServer = (TextView)itemView.findViewById(R.id.textNameServer);
textConnected = (TextView)itemView.findViewById(R.id.textConnected);
background = itemView;
}
public ImageView getImageServer() {
return imageServer;
}
public TextView getTextConnected() {
return textConnected;
}
public TextView getTextNameServer() {
return textNameServer;
}
public View getBackground() {
return background;
}
}
}
any solutions to solve this problem ? thanks.
The problem happened exactly when i specify the layout height and do not let it to wrap_content
<android.support.v7.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="400dp"
android:id="#+id/serverRecyclerView"
android:layout_margin="10dp"
/>
or when i put it below something for expample like that :
<android.support.v7.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/serverRecyclerView"
android:layout_margin="10dp"
android:layout_below="#+id/image"/>
my code exactly is :
<android.support.v7.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/serverRecyclerView"
android:layout_margin="10dp"
android:layout_alignTop="#+id/imageBall"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_toRightOf="#+id/camera"
android:layout_toEndOf="#+id/camera"/>
Looks like this is a bug: https://code.google.com/p/android/issues/detail?id=203574
The best workaround seems to be Bart's answer to set the RecyclerView's LinearLayoutManager's AutoMeasure property to false.
LinearLayoutManager llm = new LinearLayoutManager(context);
llm.setAutoMeasureEnabled(false);
recyclerView.setLayoutManager(llm);
The set FixedSize to true solution had way too many side-effects...
RecyclerView.setHasFixedSize(true)
I don't know why, but I used:
RecyclerView.setHasFixedSize(true)
This worked for me. I hope it can help.
android:descendantFocusability="blocksDescendants"
android:descendantFocusability="blocksDescendants"
this attr solve my bug
RecyclerView.ItemAnimator animator = myRecyclerListView.getItemAnimator();
if (animator instanceof SimpleItemAnimator) {
((SimpleItemAnimator)animator).setSupportsChangeAnimations(false);
}
My RecyclerView was inside ConstraintLayout, and I also had such problem and calling setAutoMeasureEnabled(false) of RecyclerView's LayoutManager did not fix the issue for me, furthermore this method is deprecated in 28.0.0 version. What I did is that, I wrapped my RecyclerView with RelativeLayout and now it works like a charm. As mentioned in bugtracker, this "issue" is intented behaviour in LinearLayout and is not going to be fixed. So if it is possible, just wrap your RecyclerView something like this:
<RelativeLayout
android:id="#+id/container_messages_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#drawable/chat_back_pattern"
app:layout_constraintBottom_toTopOf="#+id/bottom_view"
app:layout_constraintTop_toBottomOf="#+id/toolbar">
<android.support.v7.widget.RecyclerView
android:id="#+id/messages_list"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
for anyone who stumbles upon this issue, try using
yourRecyclerView.notifyItemChanged(int position, Object payload);
This one did the trick for me.
Using
setAutoMeasureEnabled(false);
also worked but in some edge cases recycler view was acting weird. Good luck!
RecyclerView can perform several optimizations if it can know in advance that RecyclerView's size is not affected by the adapter contents. RecyclerView can still change its size based on other factors (e.g. its parent's size) but this size calculation cannot depend on the size of its children or contents of its adapter (except the number of items in the adapter).
If your use of RecyclerView falls into this category, set this to true. It will allow RecyclerView to avoid invalidating the whole layout when its adapter contents change.
If we have a RecyclerView with match_parent as height/width, we should add setHasFixedSize(true) since the size of the RecyclerView itself does not change inserting or deleting items into it.
setHasFixedSize should be false if we have a RecyclerView with wrap_content as height/width because each element inserted by the adapter could change the size of the RecyclerView depending on the items inserted/deleted, so, the size of the RecyclerView will be different each time we add/delete items.
recyclerView.setHasFixedSize(true);
true if adapter changes cannot affect the size of the RecyclerView.
References
Android Developers Reference - RecyclerView
Understanding RecyclerView setHasFixedSize - Gastón Saillén
I came across the similar problem, just take care of the xml layout file.
Do not use the layout_below , layout_above or others similar properties in RecyclerView or RecyclerView's parent view.
You can use LinearLayout weight , layout_marginBottom or sth to achieve
layout_below or other.
The late answer better than nothing, if you're using NestedScrollView as the parent view of RecyclerView you should delete it.
I had a similar problem and I tryed all solutions listed above, but noone worked.
I was already padding the "Payloads" to "notifyItemChanged(position, payloads)" because I just needed to "upload" a checkbox value so I was passing the value inside "Payloads" without recalling the update of the entire viewholder.
This solution worked for all view holders in my recycler view except for the last one (and probably for all "recycled" ones, I mean those who recall the "onBindViewHolder" by "recycling" an existing view).
I think using "notifyItemChanged" will works if you have only the recyclerview and I also think that this problem of "auto-scrolling" is raised by nested scroll views & recycler views.
I was in the case exposed by "raed", so "ScroolView -> RecyclerView -> "n" x RecyclerView". I have a scroolview wich contains a recyclerview whose viewholders can contains a recycler views.
Delete the parent ScrollView is a really weird solution and I couldn't use it, so I setted the "onStopNestedScroll" inside the "ScrollView" and not inside the RecyclerView.
Personally I used it programmatically before the code part which calls the "notifyItemChanged" method by doing:
msvContainer.onStopNestedScroll(mRecyclerView);
Where "msvContainer" is my ScrollView which contains the RecyclerView, and "mRecyclerView" is my RecyclerView contained by the ScrollView.
This way worked 99% because the first time I call "notifyItemChanged" the view scroll up only for the ScrollView, so it hides a button inside my ScrollView which is below my RecyclerView but it doesn't scroll the RecyclerView items. After the first call "notifyItemChanged" works properly.
I found that calling:
msvContainer.stopNestedScroll();
works too. But i suggest to use the first method with the target view if you have multiple nested scroll views.
Anyway you should call "startNestedScroll" after you ran out of the critical part of re-updating your view holder because the targeted view, so in my case the RecyclerView, won't scroll until you call this method so it won't recycler his view holders too.
(In my case that I have multiple Recycler View inside a parent Recycler View inside a parent Scroll View if I was in need to call "notifyItemChanged" inside the most inner Recycler View i would use the "stopNestedScroll" method for every parent view and then re-activated the scroll after the scroll-critical part)
Hope this is helpful, have a nice coding!
Bye
In my case, all I did was to set the height of the recyclerview to "match_parent". Then in your MainActivity, do;
recyclerView.smoothScrollToPosition(yourAdapter.getItemCount()-1);
Thats all...
An activity in our Android application features a spinner, the selected value of which affects which other views are to be displayed in the activity (these views are inputs for sub-parameters of the spinner parameter, and so are spinner value specific).
The contents of the activity (below the top-most spinner) should change dynamically upon spinner selection and is visualized with this very quick mockup:
Initially, we had the sub-parameters in their own linear layouts in the activity xml, and upon spinner selection change, hid all the irrelevant sub-parameter layouts, but this seems a fairly rotten approach, and also severely undermines the extensibility of the activity (in terms of adding new top spinner box options and sub-parameters).
We've also considered generating the layout completely in code with declarations of the types of inputs needed (with some encapsulated layout generator based on these declarations) for each parameter, but this seemed a bit of an over-complication, and we'd really prefer to define the sub-parameter layouts in xml.
How should we approach this?
Would this be an appropriate scenario for using fragments? (would using fragments involve hiding and showing them just as awfully as using the sub-parameter linear layouts?
Thanks!
For anyone out there seeking a solution:
We ended up having an empty 'container' view (a linear layout) within our activity's xml, which will store the sub-menus...
<LinearLayout
android:id="#+id/algorithm_layout_container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</LinearLayout>
and seperate xml layout files for each of the sub-menus (eg; the empty negative sub-menu...)
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
tools:context="PACKAGE.ACTIVITY"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
installing an onItemSelectedListener (well actually, having our activity implement it) and upon the selected spinner item changing, adjust the displayed sub-menu by clearing the container, and adding the sub-menus corresponding layout (inflated to a view) to the container...
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
// get the current spinner value
String choice = parent.getSelectedItem().toString();
// get and clear our sub-menu container
LinearLayout container = (LinearLayout) findViewById(R.id.algorithm_layout_container);
container.removeAllViews();
// determine which sub-menu layout to set by the spinner option
// (we're exploiting the strings.xml string identifier for our own identification)
int layout;
if (choice.equals(getString(R.string.halftone_algorithm_choice))) {
layout = R.layout.algorithm_halftone;;
}
else if (choice.equals(getString(R.string.negative_algorithm_choice))) {
layout = R.layout.algorithm_negative;
}
else if (choice.equals(getString(R.string.gaussian_algorithm_choice))) {
layout = R.layout.algorithm_gaussian;
}
else if (choice.equals(getString(R.string.dithering_algorithm_choice))) {
layout = R.layout.algorithm_dithering;
} else {
// only reached via a dev bug: you've got an unexpected spinner value selected
// we handle this with an alert, then switch to another (default) spinner value
}
// inflate the determined layout to a view, and add it to our container
container.addView(LayoutInflater.from(this).inflate(layout, null, false));
}
I want to make an application that show lyric of sound with music,
I put lyrics in a custom made listview with layout below ( layout for row's ), and time of that lyric in text separated with comma,
then, I want to scroll with media.
This is my custom layout for rows:
<TextView
android:id="#+id/custom_text_arabic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:gravity="center"
android:lineSpacingExtra="15dp"
android:textSize="20sp" />
<TextView
android:id="#+id/custom_text_persian"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:lineSpacingExtra="10dp"
android:textColor="#999999"
android:textSize="12sp" />
</LinearLayout>
and I have an adapter for this custom row layout in list view,
I have a music file that plays with MediaPlayer and I get current position of sound and check that in an array of time to find the position of row in list view, then I scroll to that row in a listview, along side this things, I want that row background change to black!
So, I get that row with this code and change it!
// cAdapter is the name of my BaseAdapter and whereIsMe is current child of listview
// that i want to manipulate it
View mVi = cAdapter.getView(whereIsMe-1, null, lv);
TextView persian = (TextView) mVi.findViewById(R.id.custom_text_persian);
// this toast works great!
Toast.makeText(getApplicationContext(),persian.getText(), Toast.LENGTH_LONG).show();
// this part of code is not working!
persian.setBackgroundColor( Color.BLACK );
the problem is:
I can Toast Text in a TextView perfectly! But I can't change that TextView Background or any other manipulation! why and how can I fix that?
Calling getView doesn't return the actual child of the ListView. There are two options, you can call getChild for the ListViewand update the background color or call notifyDataSetChanged and set the background color in your adapter getView method.
The getView method of the adapter is not meant to be used as you are using it.
Read some tutorials on creating a custom adapter for a listView. Then you can do whatever you want.
Here is a good read on custom adapters;
http://www.vogella.com/tutorials/AndroidListView/article.html
For getting the view you want from outside the adapter use something like this:
View view;
int nFirstPos = lv_data.getFirstVisiblePosition();
int nWantedPos = invalidaEste - nFirstPos;
if ((nWantedPos >= 0) && (nWantedPos <= lv_data.getChildCount())
{
view = lv_data.getChildAt(nWantedPos);
if (view == null)
return;
// else we have the view we want
}
Sometimes listView.getChildAt(int index) returns NULL (Android)
This happens because getView is called internally to create a view. by what you are doing you are actually creating a new view which is not added to any screen/layout so any changes you do wont be reflected on the UI
I have set up a listview with one edit text widget. The listview is populated with a string file and I wanted to make changes on each item by clicking on the item. code builds and the listview shows as planned. When the item is clicked it gets focus but then looses focus after the keyboard pops up without any entries.
els_edit_tiles.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/listText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
</EditText>
This is the onclicklistener, I assume the changes will need to be here and did not post the adapter code.
private class ListClickhandler implements OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> Adapter, View view, int position, long arg3) {
TextView listText = (TextView) view.findViewById(R.id.listText);
String text = listText.getText().toString();
// Toast.makeText(context, text + " " + position, Toast.LENGTH_LONG).show();
}
}
I assume you mean the item loses its highlight, rather than focus. Focus goes wherever you tap and is there whether the highlight shows or not. So, I'll explain some options for the disappearing highlight.
For better or worse, they've designed it this way. You have a few choices. One is to subclass ListView and try to figure out how to add a sticky highlight. (I tried that and got it to work, but abandoned it for another reason - I wanted to put the list in a ScrollView, which doesn't work with a ListView).
Another choice: use a spinner (aka a drop down list). This doesn't exactly do what you're asking but it does show you which item was selected -- it is the one that shows when the spinner collapses.
Yet a third choice (I implemented this) - Use a LinearLayout and add each item as a child of the layout. You have to implement your own selection and highlighting, but that takes relatively little code (I can help if you need). This effectively gives you a list that shows all items - it doesn't scroll. You can include it in a ScrollView along with other elements to scroll the whole collection together.