Note that there are some explanatory texts on larger screens.

plurals
  1. POHorizontalScrollView scrolls to 0 on fragment replace (Jelly Bean)
    text
    copied!<p>I have a <code>HorizontalScrollView</code> that contains two fragments, fragment A that has list and fragment B that shows the details of the selected item in fragment A. Both fragments have a specific width , so the total width is bigger than the screen and the horizontal scrolling is enabled. One of the possible detail-views contains a form with an <code>EditText</code>. </p> <p>Before JellyBean, replacing the details fragment would never affect the position of the horizontal scrolling. But in 4.1.1, if the user selects the item with the form-type details ,clicks on the EditText and then selects another item (press back to hide keyboard and scroll a bit to the right so that both fragments are visible ), the <code>HorizontalScrollView</code> scrolls back to 0. </p> <p>Here is some code that demonstrates the problem based on the <code>FragmentLayout</code> of the ApiDemos:</p> <pre><code>package com.example.testproject; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentTransaction; import android.support.v4.app.ListFragment; import android.util.TypedValue; import android.view.Display; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; public class FragmentLayout extends FragmentActivity { public static final String[] TITLES = { "Row 1", "Row 2", "Row 3", "Row 4", "Row with EditText as detail" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_layout); Fragment sideList = getSupportFragmentManager().findFragmentById( R.id.titles); if (sideList == null) { sideList = new TitlesFragment(); FragmentTransaction ft = getSupportFragmentManager() .beginTransaction(); ft.replace(R.id.titles, sideList, "side_list").commit(); } } public static class TitlesFragment extends ListFragment { int mCurCheckPosition = 0; @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); setListAdapter(new ArrayAdapter&lt;String&gt;(getActivity(), android.R.layout.simple_list_item_activated_1, TITLES)); if (savedInstanceState != null) { mCurCheckPosition = savedInstanceState.getInt("curChoice", 0); } getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); showDetails(mCurCheckPosition); } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putInt("curChoice", mCurCheckPosition); } @Override public void onListItemClick(ListView l, View v, int position, long id) { showDetails(position); } void showDetails(int index) { mCurCheckPosition = index; getListView().setItemChecked(index, true); Fragment details = getFragmentManager().findFragmentById( R.id.details); if (index &lt; 4) { details = DetailsFragment.newInstance(index); } else { details = FormFragment.newInstance(index); } FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.replace(R.id.details, details); ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); ft.commit(); } } public static class DetailsFragment extends Fragment { public static DetailsFragment newInstance(int index) { DetailsFragment f = new DetailsFragment(); Bundle args = new Bundle(); args.putInt("index", index); f.setArguments(args); return f; } public int getShownIndex() { return getArguments().getInt("index", 0); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (container == null) { return null; } Display display = getActivity().getWindowManager() .getDefaultDisplay(); LinearLayout l = new LinearLayout(getActivity()); TextView text = new TextView(getActivity()); l.addView(text); text.getLayoutParams().width = display.getWidth(); int padding = (int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, 4, getActivity() .getResources().getDisplayMetrics()); text.setPadding(padding, padding, padding, padding); // use the ApiDemos Shakespeare.java file for the DIALOGUe text.setText(Shakespeare.DIALOGUE[getShownIndex()]); return l; } } public static class FormFragment extends Fragment { public static FormFragment newInstance(int index) { FormFragment f = new FormFragment(); return f; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (container == null) { return null; } Display display = getActivity().getWindowManager() .getDefaultDisplay(); LinearLayout l = new LinearLayout(getActivity()); EditText text = new EditText(getActivity()); l.addView(text); text.getLayoutParams().width = display.getWidth(); return l; } } } </code></pre> <p>and the only interesting xml fragment_layout.xml:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" &gt; &lt;LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:baselineAligned="false" android:orientation="horizontal" &gt; &lt;FrameLayout android:id="@+id/titles" android:layout_width="300dip" android:layout_height="fill_parent" /&gt; &lt;FrameLayout android:id="@+id/details" android:layout_width="wrap_content" android:layout_height="fill_parent" android:background="?android:attr/detailsElementBackground" /&gt; &lt;/LinearLayout&gt; &lt;/HorizontalScrollView&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload