Android, make scrollable view overflow to the left instead of right? - java

I was recommended using a parent view to get horizontal scrolling right in my TextView:
<HorizontalScrollView android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:scrollHorizontally="true"
android:gravity="center|right"
android:text="123456789"/>
</HorizontalScrollView>
Horizontal scrolling works fine but it makes the content overflow to the right when it gets longer than it's parent's width:
-------
|123456|7
-------
However I'm working on a textview that holds numbers and since numbers are commonly aligned to the right I need the textview to overflow to the opposite side. (you should have to scroll left to see the beginning of the string).
------
1|4567|
------
I have tried multiple combinations of gravity="right" and widths but I cannot manage to do it. How can I align the text to the right and make it overflow to the left?
EDIT:
I tried doing this when the user types:
calc.setText( newNumber );
HorizontalScrollView hsv = (HorizontalScrollView) findViewById(R.id.hsv);
hsv.scrollTo(hsv.getRight(), hsv.getTop());
This scrolls to the right every time the user types a number, however the latest number is always left out of the screen (it's like it scrolled and THEN added the number).

Inspired by this
You can make your own class derived from HorizontalScrollView
public class RightAlignedHorizontalScrollView extends HorizontalScrollView {
public RightAlignedHorizontalScrollView(Context context) {
super(context);
}
public RightAlignedHorizontalScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public RightAlignedHorizontalScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
scrollTo(getChildAt(0).getMeasuredWidth(), 0);
}
}
I posted a complete simple project there

I have tested the code . So there are two ways to get around this problem
First one with ScrollBars
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
HorizontalScrollView hsv = (HorizontalScrollView) findViewById(R.id.hsv1);
hsv.scrollTo(hsv.getRight(), hsv.getTop());
}
},100L);
Second one is without scrollBars
<TextView
android:ellipsize="start" //add this line
...
/>

<HorizontalScrollView
android:id="#+id/hsv1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:scrollHorizontally="true"
android:gravity="center|right"
android:text="123456789"/>
</HorizontalScrollView>
Added id to the HorizontalScrollView
HorizontalScrollView hsv = (HorizontalScrollView) findViewById(R.id.hsv1);
hsv.scrollTo(hsv.getRight(), hsv.getTop());
This is untested as I made it on the fly. Tell me how it goes.

I think you must use android:fillViewport="true" property for scroll-view
Check This & This
Go through a Tutorial
<ScrollView
android:fillViewport="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/scrollView"/>
Doubt: Where is your container layout for scroll-view??

Did you try assigning the ellipsize parameter to your textview?
android:singleLine="true"
android:ellipsize="start"
I hope this will solve your issue.
For setting the gravity of the text when not overflowing, try setting
android:gravity="center_vertical|right"

try this , this would work properly and will not give you any delays when you jump between threads
hor = (HorizontalScrollView) findViewById(R.id.horizontalScrollView1);
hor.postDelayed(new Runnable() {
public void run() {
hor.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
}
}, 1L);

I'm quite late at this, but here's a simple and easy approach by using rotation of the views.
horizontal_layout_id.setRotationY(180f)
The above code inverts the whole horizontal layout horizontally, this make the text start on the right instead of left. HOWEVER, this also inverts the text in the textview inside, which is obviously not good.
textview_inside_horizontal_layout_id.setRotationY(180f)
So, we invert the textview itself once more, this will mirror the mirrored text again! So, you'll end up having something like this-
This will make the text overflow to the right instead of left as well!

Related

Android Studio: Calling multiple Canvas in ScrollView to display 1 drawing (canvas) after another

I'm writing a code for a school project where one has to load a data file (CSV file, text file, etc) and from the obtained data, the app will pass the data to a custom draw View and the onDraw method will draw/plot a graph based on the data.
My goal is for the app to display 2 graphs, one after the other (stacked). The first set of data is loaded and the 1st graph is drawn. The loaded data is then used for a different calculation in a different method. The custom draw View is then called again with the new data to draw the 2nd graph.
When I run the app, both charts are drawn but because the x and y-axis' of the graph are coded to be drawn at certain fixed pixels, the 2nd graph is drawn over the first one and therefore only the 2nd graph is visible.
Is there any way I can draw the 2 graphs so that it does not overlap and instead appears to be stacked in ScrollView?
My code is shown below but I've gotten rid of calculations that I think aren't very important. Any help and pointers would be very much appreciated!
MainActivity.java:
#Override
protected void onActivityResult(......) {
super.onActivityResult(......);
switch (1) {
case 1:
Graph graph = this.findViewById(R.id.graph1);
graph.setData(data); // the loaded data is passed to Graph View
Graph drawGraph2 = this.findViewById(R.id.graph2);
graph2.setData(this.newCalculate(data));
break;
}
}
Graph.java
public class Graph extends View {
private Paint mPaint = new Paint();
private final int zero = 700; // mark the 0 line of graph at 700 pixels
public void setData(data){
......
}
public Graph(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
}
#Override
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(widthSize, heightSize);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
plotUnit(canvas); // plot points on graph
axisLabel(canvas); // label axis
axisLine(canvas); // draw axis
xyAxisMarker(canvas); // mark axis
}
private void plotUnit(Canvas canvas) {
......
// Due to data having negative values, the graph is inverted and the 0 starts
// of the graph is defined at 700 pixels (private final int zero)
}
private void axisLabel(Canvas canvas) {
......
}
private void axisLine(Canvas canvas, int inset) {
......
}
private void xyAxisMarker(Canvas canvas) {
......
}
Update
activity_main.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">
<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">
<Button
android:id="#+id/loadbutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Open Data File" />
<firstapp.drawtwograph.Graph
android:id="#+id/graph1"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<firstapp.drawtwograph.Graph
android:id="#+id/graph2"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
</ScrollView>
</LinearLayout>
You cannot have two views' heights match parent height inside of a LinearLayout with vertical orientation. It is not possible because heights of these views must be equal to the parent height but at the same time, they must be ordered one after the other resulting in double of parent's height.
If you imagine parent's height as 10dp then each of the Graph views must be 10dp as well which means parent's height must be 20dp, not 10dp. That is going to cycle forever so the Android does a simple thing: views that are going below the first child view with android:layout_height="match_parent" will have height 0dp or if their height is fixed they will be drawn outside of the layout and will not be visible.
Example
Screenshot from Design tab of layout editor in Android Studio IDE.
Here you can see:
red view as a parent linear layout;
purple view as a first child with height matching it's parent height;
outlined view that is drawn outside of the layout because it is pushed out by the first child with android:layout_height="match_parent";
there is one more view that is crushed to 0 height and thus not visible. You can see it down in the XML code.
XML code of this sample:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#android:color/holo_red_light">
<LinearLayout
android:background="#color/colorPrimary"
android:layout_width="60dp"
android:layout_height="match_parent" <!-- this view's height is a problem -->
android:orientation="vertical"/>
<LinearLayout
android:background="#android:color/holo_green_dark"
android:layout_width="120dp"
android:layout_height="match_parent" <!-- height is not fixed, then it will be 0 -->
android:orientation="vertical"/>
<LinearLayout
android:background="#android:color/holo_green_light"
android:layout_width="120dp"
android:layout_height="40dp" <!-- height is fixed, it is outlined outside of a layout -->
android:orientation="vertical"/>
</LinearLayout>
How to fix the issue?
Set fixed height. As a test try to define a fixed height, e.g. 100dp;
Redesign your layout. Use RelativeLayout or ConstraintLayout to position views relative to each other so that they are always visible no matter what the screen size, ratio, density is.
Example of how to fix
I personally prefer ConstraintLayout as it is very powerful in terms of positioning and adaptation.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/loadbutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Open Data File"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<firstapp.drawtwograph.Graph
android:id="#+id/graph1"
android:layout_width="wrap_content"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/graph2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/loadbutton" />
<firstapp.drawtwograph.Graph
android:id="#+id/graph2"
android:layout_width="wrap_content"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/graph1" />
</androidx.constraintlayout.widget.ConstraintLayout>
The result is (I used two buttons instead of Graph views):
Hints:
If you want to use ScrollView then setting fixed height or defining height at runtime will be required.
Get rid of private final int zero = 700; // mark the 0 line of graph at 700 pixels. Do not use pixel values directly as it will lead to error-prone UI. It will be the case of "work on my phone, does not work the other". Use view's height as the 0 line.

findFirstVisibleItemPosition() returns 0 inside nestedScrollView

I have a recyclerView inside nestedScrollView. I know it's a bad practice, but there are some cases, that I can handle only in this way.
<androidx.core.widget.NestedScrollView
android:id="#+id/nestedScroll"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_data"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:itemCount="1"
tools:listitem="#layout/item_favorite_tournament"
tools:visibility="visible" />
</androidx.core.widget.NestedScrollView>
And here's my code. I'll show you the scroll part, where I need to get the firstVisibleItemPosition. But it returns 0.
private void initList(RecyclerView recycler, View labelNoResults) {
recycler.setAdapter(mAdapter);
recycler.setHasFixedSize(true);
mListLayoutManager = Objects.requireNonNull((LinearLayoutManager) recycler.getLayoutManager());
recycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrollStateChanged(#NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
final int p = mListLayoutManager.findFirstVisibleItemPosition();
Log.d("Tag_1", "Position: " + p);
}
});
}
I think the problem is because of nestedScrollView. So how can I get the visibleItemPosition form recyclerView? Thank you.
The easiest way to fix this would be to replace NestedScrollView by simply ScrollView. This of course will change the behavior of your layout, but it will show you the right position.
In case of NestedScrollView, findFirstVisibleItemPosition() always returns 0 and findLastVisibleItemPosition() always returns the last item in the RecyclerView despite whether they are visible to the user or not. This happens because RecyclerView placed inside NestedScrollView behaves like a ListView without doing any recycling. Meaning all items are visible at the same time because all of them are bind at the same time.

Select a value from a NumberPicker

I'm trying to make a custom NumberPicker to display in a DialogFragment. So far I've succeeded in getting the picker to display in a dialog fragment and getting it to display the custom strings I want it to. I've also disabled the descendantFocusability so the text is not editable. Here is an overview of the questions I have about NumberPicker behaviour, I'll go more in depth after:
How does one 'commit' their selection?
How to return the selected value?
How does one 'commit' their selection?
When the dialog appears, I don't see a clear way to 'select' an option (see image below). Looking at native Android selection dialogs, I often see radiobuttons. Is that the way to go? And am I using the wrong UI component to build this?
How to return the selected value?
This question is tightly knit with the last one, as not knowing how to commit a selection obviously doesn't help here. Right now I use NumberPicker.OnValueChangeListener to see if the value changed, however it never fires. Here's how I structured the code:
class PlatePickerFragment: DialogFragment() {
lateinit var listener: NumberPicker.OnValueChangeListener
//I set up the fragment with onCreateDialog here.
}
And this is the code I use when I create an instance:
val platePicker = PlatePickerFragment()
platePicker.listener = NumberPicker.OnValueChangeListener { numberPicker, i1, i2 ->
//set what to do on value change here.
}
However, this block never gets called.
TL;DR: Am I using the right UI component? If I am, how would I implement this in a way that it works? Why does the NumberPicker not have a cancel/ok section by default (see image of DatePicker below)? Thanks in advance!
Answer to first part :
This is the ideal way of implementing NumberPicker. One thing you can do
is add an OK button to side to catch selection.See screenshot
Code for same :
picker.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:padding="16dp">
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:padding="5dp"
android:text="OK"
android:textSize="15dp" />
<NumberPicker
android:id="#+id/numberPicker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tv"
android:layout_marginTop="10dp" />
</RelativeLayout>
</LinearLayout>
In your activity :
final NumberPicker aNumberPicker = (NumberPicker) dialog.findViewById(R.id.numberPicker);
aNumberPicker.setMaxValue(12);
aNumberPicker.setMinValue(1);
aNumberPicker.setValue(1);
aNumberPicker.setFocusable(true);
aNumberPicker.setFocusableInTouchMode(true);
aNumberPicker.setOnScrollListener(new NumberPicker.OnScrollListener() {
#Override
public void onScrollStateChange(NumberPicker view, int scrollState) {
value = view.getValue();
}
});
aNumberPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
value = newVal;
}
});
TextView ok = (TextView) parent.findViewById(R.id.tv);
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// value variable can be used here
}
});
Declare value as global variable.
Answer to the second part of your question :
int hour;
numberPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
#Override
public void onValueChange(final NumberPicker numberPicker, final int i, final int i1) {
hour = Integer.valueOf(numberPicker.getDisplayedValues()[numberPicker.getValue()]);
}
});
On clicking of OK button you will have answer in hour variable.

How to refresh android view height when sibling position changed?

I have an Android Layout like this :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_layout"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_weighing"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/cv_price_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- another view elements here -->
</android.support.v7.widget.CardView>
</LinearLayout>
Basically I want the first element fill the remaining space between parent and the second element. It works fine until I change the Y position of the second element at runtime. I tried to call .invalidate() and .requestLayout() on both parent and the first element but it seems like doesn't work as expected.
I also tried to change the parent layout using <RelativeLayout> and set android:layout_above="#+id/cv_price_holder" on the first element but didn't work as well.
EDIT : Here is the method that invoked to toggle the layout :
public void togglePriceHolder(){
float newY = mPriceHolderToggleState ? -mPriceHolderToggleLimitY : mPriceHolderToggleLimitY;
int newIcon = mPriceHolderToggleState ? R.drawable.ic_keyboard_arrow_down_white_36dp : R.drawable.ic_keyboard_arrow_up_white_36dp;
mBtnTogglePriceHolder.setImageResource(newIcon);
mCvPriceHolder.animate().translationYBy(newY).setListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {}
#Override
public void onAnimationEnd(Animator animation) {
// refresh the layout
rvWeighing.forceLayout();
mMainLayout.requestLayout();
}
#Override
public void onAnimationCancel(Animator animation) {}
#Override
public void onAnimationRepeat(Animator animation) {}
});
mPriceHolderToggleState = !mPriceHolderToggleState;
}
The question is how can I refresh the layout so the first element can adjust its height when Y position of the second element change ?
Thanks in advance
Try calling forceLayout() on the views whose dimensions might change and then calling requestLayout().
When requestLayout() is called on a view, all the parents of that view are redrawn, but the siblings are not, calling forceLayout() on a particular view, marks that view for the next layout pass(along with the default traversal).
In your particular case, try calling forceLayout() on the siblings you want to redraw, and then call requestLayout() from one of the views.

RecyclerView onCreateViewHolder called excessively when scrolling fast with DPAD

I'm developing on Amazon Fire TV.
Because it's a TV app(No touch), I need focusables inside row's layout to be able to navigate around.
I have a really simple Recyclerview with image, text, and a focusable. When I press up or down, it all scrolls and stuff correctly, but I noticed that when I navigate faster than scroll can keep up, it creates new viewholders (Off screen) and lags up the UI.
I have created an activity with Creation numbers on it. When I scroll slowly, the highest creation # is 10. But when I scroll fast, I get cards with creation number 60 in a second. This causes an enormous lag and the application drops a lot of frames. Is my approach totally wrong?
Use the code below to test this out.
/**
* Created by sylversphere on 15-04-15.
*/
public class LandfillActivity extends Activity{
private Context context;
private static int ticketNumber;
private static int getTicket(){
ticketNumber ++;
return ticketNumber;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
setContentView(R.layout.landfill_activity);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
GridLayoutManager glm = new GridLayoutManager(context, 2);
recyclerView.setLayoutManager(glm);
SickAdapter sickAdapter = new SickAdapter();
recyclerView.setAdapter(sickAdapter);
}
public class SickViewHolder extends RecyclerView.ViewHolder{
TextView ticketDisplayer;
public ImageView imageView;
public SickViewHolder(View itemView) {
super(itemView);
ticketDisplayer = (TextView) itemView.findViewById(R.id.ticketDisplayer);
imageView = (ImageView) itemView.findViewById(R.id.imageView);
itemView.findViewById(R.id.focus_glass).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
context.startActivity(new Intent(context, LouisVuittonActivity.class));
}
});
}
public void setTicket(int value){
ticketDisplayer.setText(""+value);
}
}
public class SickAdapter extends RecyclerView.Adapter<SickViewHolder>{
#Override
public SickViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
SickViewHolder svh = new SickViewHolder(getLayoutInflater().inflate(R.layout.one_row_element, null));
svh.setTicket(getTicket());
return svh;
}
#Override
public void onBindViewHolder(SickViewHolder holder, int position) {
String[] image_url_array = getResources().getStringArray(R.array.test_image_urls);
Picasso.with(context).load(image_url_array[position % image_url_array.length] ).fit().centerCrop().into(holder.imageView);
}
#Override
public int getItemCount() {
return 100000;
}
}
}
one_row_element.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#mipmap/sick_view_row_bg" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:layout_marginLeft="15dp"
android:orientation="horizontal">
<TextView
android:id="#+id/virusTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Creation #"
android:textColor="#fff"
android:textSize="40sp" />
<TextView
android:id="#+id/ticketDisplayer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:textColor="#fff"
android:textSize="40sp" />
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/focus_glass"
android:background="#drawable/subtle_focus_glass"
android:focusable="true"
android:focusableInTouchMode="true"/>
</FrameLayout>
</FrameLayout>
test_image_urls.xml (Urls not owned by me)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="test_image_urls"
formatted="false">
<item>http://farm4.static.flickr.com/3175/2737866473_7958dc8760.jpg</item>
<item>http://farm4.static.flickr.com/3276/2875184020_9944005d0d.jpg</item>
<item>http://farm3.static.flickr.com/2531/4094333885_e8462a8338.jpg</item>
<item>http://farm4.static.flickr.com/3289/2809605169_8efe2b8f27.jpg</item>
<item>http://2.bp.blogspot.com/_SrRTF97Kbfo/SUqT9y-qTVI/AAAAAAAABmg/saRXhruwS6M/s400/bARADEI.jpg</item>
<item>http://fortunaweb.com.ar/wp-content/uploads/2009/10/Caroline-Atkinson-FMI.jpg</item>
<item>http://farm4.static.flickr.com/3488/4051378654_238ca94313.jpg</item>
<item>http://farm4.static.flickr.com/3368/3198142470_6eb0be5f32.jpg</item>
<item>http://www.powercai.net/Photo/UploadPhotos/200503/20050307172201492.jpg</item>
<item>http://www.web07.cn/uploads/Photo/c101122/12Z3Y54RZ-22027.jpg</item>
<item>http://www.mitravel.com.tw/html/asia/2011/Palau-4/index_clip_image002_0000.jpg</item>
<item>http://news.xinhuanet.com/mil/2007-05/19/xinsrc_36205041914150623191153.jpg</item>
<item>http://ib.berkeley.edu/labs/koehl/images/hannah.jpg</item>
<item>http://down.tutu001.com/d/file/20110307/ef7937c2b70bfc2da539eea9df_560.jpg</item>
<item>http://farm3.static.flickr.com/2278/2300491905_5272f77e56.jpg</item>
<item>http://www.pic35.com/uploads/allimg/100526/1-100526224U1.jpg</item>
<item>http://img.99118.com/Big2/1024768/20101211/1700013.jpg</item>
<item>http://farm1.static.flickr.com/45/139488995_bd06578562.jpg</item>
</string-array>
</resources>
subtle_focus
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="#color/glass_focus"/>
<item android:drawable="#color/glass_normal"/>
</selector>
glass_normal is #9000
glass_focus is #0000
Try increasing the maximum number of recycled views in the pool:
recyclerView.getRecycledViewPool().setMaxRecycledViews(50);
50 is an arbitrary number, you can try higher or lower and see what happens.
RecyclerView tries to avoid re-using views that have transient state, so if views are invalidated quickly or are animating, they may not be re-used right away.
Similarly if you have many smaller views, you may end up with more on screen than the default pool size can handle (more common with grid like layouts).
Picasso is holding you up but the suggested solution to build your own mechanism is not the way to go.
Picasso has fallen behind in the last year or so and today there are far better alternatives in the form of Google Glide and Facebook Fresco which specifically released updates to work better with RecyclerView and have proved faster and more efficient in loading, caching and storing in many tests which are available online such as:
http://inthecheesefactory.com/blog/get-to-know-glide-recommended-by-google/en
https://code.facebook.com/posts/366199913563917/introducing-fresco-a-new-image-library-for-android/
I hope that helped.
Good luck.
As the commenters pointed out pending responses from Picasso might be holding you up. If that is the case, you can solve it by extending ImageView and overriding the following method. I think it is worth trying.
#Override
protected void onDetachedFromWindow() {
Picasso.with(context).cancelRequest(this);
super.onDetachedFromWindow();
}
Update:
Turns out this is not the correct way, anyone wanting to cancel requests should do so in the onViewRecycled() callback as pointed out in the comments below.
By digging deeper in Sam Judd's answer I forced recycler to recycle the views by implementing the followings in its adapter.
#Override
public boolean onFailedToRecycleView(#NonNull VH holder)
return true;
}
As you can see here:
Called by the RecyclerView if a ViewHolder created by this Adapter cannot be recycled due to its transient state. Upon receiving this callback, Adapter can clear the animation(s) that effect the View's transient state and return true so that the View can be recycled. Keep in mind that the View in question is already removed from the RecyclerView.
In some cases, it is acceptable to recycle a View although it has transient state. Most of the time, this is a case where the transient state will be cleared in onBindViewHolder(ViewHolder, int) call when View is rebound to a new position. For this reason, RecyclerView leaves the decision to the Adapter and uses the return value of this method to decide whether the View should be recycled or not.
Note that when all animations are created by RecyclerView.ItemAnimator, you should never receive this callback because RecyclerView keeps those Views as children until their animations are complete. This callback is useful when children of the item views create animations which may not be easy to implement using an RecyclerView.ItemAnimator.
You should never fix this issue by calling holder.itemView.setHasTransientState(false); unless you've previously called holder.itemView.setHasTransientState(true);. Each View.setHasTransientState(true) call must be matched by a View.setHasTransientState(false) call, otherwise, the state of the View may become inconsistent. You should always prefer to end or cancel animations that are triggering the transient state instead of handling it manually.
For anyone else looking for a quick hack,
Do this. This will delay selection until it's been inflated and selected.
I don't know how but it works.
Just return null on onFocusSearchFailed.
/**
* Created by sylversphere on 15-04-22.
*/
public class SomeGridLayoutManager extends GridLayoutManager{
private final Context context;
public SomeGridLayoutManager(Context context, int spanCount) {
super(context, spanCount);
this.context = context;
}
public SomeGridLayoutManager(Context context, int spanCount, int orientation, boolean reverseLayout) {
super(context, spanCount, orientation, reverseLayout);
this.context = context;
}
#Override
public View onFocusSearchFailed(View focused, int focusDirection, RecyclerView.Recycler recycler, RecyclerView.State state) {
return null;
}
}

Categories

Resources