我有一个HorizontalScrollView
包含两个片段,片段 A 有列表,片段 B 显示片段 A 中所选项目的详细信息。两个片段都有一个特定的宽度,所以总宽度大于屏幕并且启用了水平滚动. 其中一个可能的详细视图包含一个带有EditText
.
在 JellyBean 之前,替换细节片段永远不会影响水平滚动的位置。但是在 4.1.1 中,如果用户选择带有表单类型详细信息的项目,单击 EditText,然后选择另一个项目(按返回隐藏键盘并向右滚动一点,以便两个片段都可见),HorizontalScrollView
回滚到 0。
以下是一些基于FragmentLayout
ApiDemos 演示问题的代码:
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<String>(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 < 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;
}
}
}
和唯一有趣的 xml fragment_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal" >
<FrameLayout
android:id="@+id/titles"
android:layout_width="300dip"
android:layout_height="fill_parent" />
<FrameLayout
android:id="@+id/details"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="?android:attr/detailsElementBackground" />
</LinearLayout>
</HorizontalScrollView>