Android - listview - scroll programmatically to specific layout - android

Here's my question :
I have a listview (with adapter) which contains multiple inflated layout for each row and I want to smooth scroll programmatically to a specific layout.
Exemple :
this is one listview row:
------------------
- -
- layout1 -
- -
- layout2 -
- layout3 -
- etc... -
------------------
I know that in a Scrollview we can use getY() of the layout view but when i did this for my specific layout in listview it return me 0. But I can't use scrollview because I need to get SwipeRefreshLayout (Or we can get it in Scrollview ?)
I hope I was clear enough, if this is not the case, please tell me.

If you are triggering scroll by action like onClick.
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View row = listView.getChildAt(0);
View layout = row.findViewById(R.id.layout_to_scroll_to);
listView.smoothScrollToPositionFromTop(rowPositionToScrollTo, layout.getTop());
}
});
If you are triggering scroll on page load. Add this in onCreate or onActivityCreated for Activity or Fragment, accordingly.
getListView().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
listView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
View row = listView.getChildAt(0);
View layout = row.findViewById(R.id.layout_to_scroll_to);
listView.smoothScrollToPositionFromTop(rowPositionToScrollTo, layout.getTop());
}
});
Change smoothScrollToPositionFromTop to setSelectionFromTop for no scroll animation.
Hope this helps !

Related

android scrollview scroll to top direction when new view added

In scrollview, if I add any view in middle, normally all the views below the added view scrolls downside. But I want to scroll the top views of added view to upside without disturbing the bottom views. Is it possible in scrollview , please help me ?
In the figure , If view 4 was added , then view 1 has to be scrolled upwards , without changing the positions of view 2 and view 3.
You can probably get the height of the view you are adding with and then scroll the scrollview manually that many pixels
scrollView.scrollBy(0, viewAdded.getHeight())
I've been wanting to try this question for quite some time, I finally got the chance today. The method is pretty simple (in fact, #dweebo already mentioned it earlier) - we move the ScrollView up as we add the view. For getting precise (and valid) dimensions when adding, we use a ViewTreeObserver. Here's the code you can get hints from:
// Getting reference to ScrollView
final ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView);
// Assuming a LinearLayout container within ScrollView
final LinearLayout parent = (LinearLayout) findViewById(R.id.parent);
// The child we are adding
final View view = new View(ScaleActivity.this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 100);
view.setLayoutParams(params);
// Finally, adding the child
parent.addView(view, 2); // at index 2
// This is what we need for the dimensions when adding
ViewTreeObserver viewTreeObserver = parent.getViewTreeObserver();
viewTreeObserver.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
#Override
public boolean onPreDraw() {
parent.getViewTreeObserver().removeOnPreDrawListener(this);
scrollView.scrollBy(0, view.getHeight());
// For smooth scrolling, run below line instead
// scrollView.smoothScrollBy(0, view.getHeight())
return false;
}
});

How to make a set of views invisible in android

I'm trying to make a set of views (that include several textviews and buttons - all in different parent layouts, but in the same activity) invisible if a particular condition is evaluated to false.
The conventional way to do that would be:
findViewById(R.id.myview).setVisibility(View.INVISIBLE);
My question is, will I have to do this for all the views one by one that I want to be invisible, And then toggle them back when I want them visible?
Or, is there a way to avoid the code-repetition?
If the Views are in different parents , you can't do it directly, but you can implement a method to change the visibility of a bunch of Views if you want to keep your code clean:
List<View> relatedViews = new ArrayList<>();
// ...
relatedViews.add(view1);
relatedViews.add(view2);
relatedViews.add(view3);
// ...
changeVisibility(relatedViews, View.INVISIBLE);
// ...
private void changeVisibility(List<View> views, int visibility) {
for (View view : views) {
view.setVisibility(visibility);
}
}
As a side note, you may want to change the visibility to View.GONE instead of View.INVISIBLE so it doesn't take any space in the layout.
you can use something like this. It's not the most elegant solution but works.
The idea is give to each view that you want to hide a same content description, because in the same layout you can not use same id for multiple view. With the same content description you can find all views in your layout and hide them.
That's an example considering the first layout as Linear. You can change obviously ;)
public class TestActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
LinearLayout rootLayout = (LinearLayout)findViewById(R.id.rootLayout);
int childcount = rootLayout.getChildCount();
for (int i=0; i < childcount; i++){
View v = rootLayout.getChildAt(i);
if(v.getContentDescription() != null && v.getContentDescription().equals("invisibleView")){
v.setVisibility(View.INVISIBLE);
//I suggest you to use GONE instead of INVISIBLE to remove the space of the view
}
}
}
}
in your xml give to the object that you want to hide this property
android:contentDescription="invisibleView"
Use varargs method for show or hide multiple views.
for example if you have views like view1, view2.....etc
then just call setVisibility(View.VISIBLE,view1,view2)
public static void setVisibility(int visibility, View... views){
for (View view : views){
view.setVisibility(visibility);
}
}

Android - Horizontal scroll with "Show more"

I have to create a view like this -
There will be three items shown with a show more option
On Click of show more the list will grow to 10 items which will be scrollable .
What could be the optimal way to implement this kind of view ?
Use HorizontalScrollView
On the first population of the scrollview detect the click on the 4th position and on the 4th item click reload the data and call adapter.notifyDataSetChanged()
fro this purpose you can Add 4 element at first time.and detect which Item is clicked by user if this is on 3rd position then you can add more element using this code snippets
HorizontalScrollView scrollView = (HorizontalScrollView) findViewById(R.id.scrollView1);
LinearLayout topLinearLayout = new LinearLayout(this);
// topLinearLayout.setLayoutParams(android.widget.LinearLayout.LayoutParams.FILL_PARENT,android.widget.LinearLayout.LayoutParams.FILL_PARENT);
topLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
for (int i = 0; i < 15; i++){
final ImageView imageView = new ImageView (this);
imageView.setTag(i);
imageView.setImageResource(R.drawable.ic_launcher);
topLinearLayout.addView(imageView);
imageView.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Log.e("Tag",""+imageView.getTag());
}
});
}
scrollView.addView(topLinearLayout);
hope so it will work for you.
enjoy your coding time:)
Use RecyclerView instead.
set the LinearLayoutManager's orientation HORIZONTAL, like this:
recyclerView.setLayoutManager(new LinearLayoutManager(context, HORIZONTAL, false);
Good luck.

Programmatically add items to scrollview top, but should not scroll to top

I have scrollview with child LinearLayout . I am adding data programmaticaly to it. When i add some data to top of linearlayout it automatically scrolls to top element. But i want something like , user reaches top -> scrolls upside to load previous data ->add data to linearlayout top but should not get focus, after addition complete , if user scrolls then and then only it should display .
How to achieve this?
Well I thought of a way and it works almost perfectly.
I have a LinearLayout (llCommunicationsLayout) inside a ScrollView (svCommunications) .
I inflate a new LinearLayout, I'm going to add views to the top of this new LinearLayout and then add this new layout to the LinearLayout inside the ScrollView.
This is the new layout:
final LinearLayout wrapperLayout = (LinearLayout) mInflater.inflate(R.layout.empty_layout, null);
I add my views to the 0'th position of this layout.
wrapperLayout.addView(view, 0);
After all the views are added into the wrapperLayout, I add the wrapperLayout into the llCommunicationsLayout (the one inside my ScrollView)
llCommunicationsLayout.addView(wrapperLayout, 0);
After this, I calculate their heights after the wrapperLayout is on screen (has a measurable height)
wrapperLayout.post(new Runnable() {
#Override
public void run() {
int wrapperHeight = wrapperLayout.getHeight();
int svHeight = svCommunications.getHeight();
int scrollHeight = Math.abs(svHeight - wrapperHeight);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int displayHeight = displaymetrics.heightPixels / 4;
svCommunications.scrollTo(0, (scrollHeight + displayHeight));
}
});
Here, I get both the newly added layout's and the ScrollView's heights.
I calculate their difference, add the 1/4 of the height of the screen of my device and scroll it, voila!
It's not perfect, but after the layouts are added, it no longer scrolls to the top of the screen. Experiment with the displayHeight for different results.
Hope this helps someone out.
You can grab the current view which is on top of your LinearLayout then add new content to your LinearLayout and then scroll back to view which was previously on top. The code would be something like:
public void addViewsOnTop(List<View> views) {
final View currentViewOnTop = (linearLayout.getChildCount() > 0) ? linearLayout.getChildAt(0) : null;
// Add Views. Note that views will appear in reverse order
for(View view : views) {
linearLayout.addView(view, 0);
}
// Scroll back to view which was on top
if(currentViewOnTop != null) {
new Handler().post(new Runnable() {
#Override
public void run() {
scrollView.scrollTo(0, currentViewOnTop.getBottom());
}
});
}
}
Solved....
Try this, worked for me ,
lv_chat.setAdapter(adapter);
lv_chat.setSelection(somePreviousPosition);

How to slide to imageView in ScrollView

I have a layout in which i have two button:
1)back
2)help
and ScollView in which i place two image Views:
1) firstImageVeiw
1) SecondImageVeiw
first image View will be displayed on front and second is placed beneath the 1st image.
Now i want that if i click on help button the the scrollView automatically slides to 2nd image view.
Any help or suggestion.
You can use scrollview's scrollTo() method
helpbutton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
yourscrollview.scrollTo(0, yourImageView.getTop());
}
});
At runtime you can get the location of every view object with
getleft()
getTop()
getRight()
getBottom()

Categories

Resources