I have added Fragments to a linearlayout where each child fragment view occupies complete screen. Now i have to implement a scrolling functionality so that i can navigate through them. I Tried to add a scrollView (horizontal) and it was showing all the children but due to some issues i cannot use scrollview. So, to scroll, i have used the following code that scrolls well but not all children are showed, only the first one is visible even after scrolling:
/**
* A function to scroll towards the left.
*/
public void scrollLeft(){
LinearLayout layout = findViewById(R.id.viewList);
int x = getResources().getDisplayMetrics().widthPixels;
layout.scrollBy(-x, 0); // scroll by the amount of the screen's width
}
/**
* A function to scroll towards the right.
*/
public void scrollRight(){
LinearLayout layout = findViewById(R.id.viewList);
int x = getResources().getDisplayMetrics().widthPixels;
layout.scrollBy(x, 0); // scroll by the amount of the screen's width
}
I even tried to scroll the ConstraintLayout (the base layout) but that too gave the same results. Here is my activity_test.xml:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/TestRoot"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TestActivity">
<LinearLayout
android:id="@+id/viewList"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"></LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Here is the code i used to add fragments to the LinearLayout and this is called only in the onCreate method:
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
for (int i = 0; i < results.length; i++) {
ResultFragment result = new ResultFragment();
// configuring the fragment
// ....
LinearLayout l = new LinearLayout(this);
l.setId(View.generateViewId());
transaction.add(l.getId(), result, "result-"+i);
((LinearLayout)findViewById(R.id.viewList)).addView(l,i);
findViewById(R.id.viewList).invalidate();
}
transaction.commit();
Got no response but no problem, I found the solution myself. So what i did was simple. I created a custom ScrollView which overrided the
onTouchEvent
function, implemented my own scrolling mechanism and placed my linearlayout inside this scrollView and kaboom! It worked. The new ScrollView looks like this: