滚动位置故障排除

时间:2014-03-17 19:12:58

标签: android android-fragments scrollview

这是一个片段,我在 ScrollView 中有一个 RelativeLayout ,它在我的xml文件中定义。当我切换片段并弹出后台时,我正试图让我的程序恢复我以前的滚动位置。

/*Global Variables*/
int scrolly;
ScrollView myview;
/*Global Variables*/

@Override
public void onPause(){
    scrolly = myview.getScrollY();
    String write = Integer.toString(scrolly);
    writetofile(write, "myscrolly.txt");
    Log.w("TAG", "onPause");
    Log.w("TAG", "Scroll is " + scrolly);
    super.onPause();
}
@Override
public void onResume(){
    String readery = readfromfile("myscrolly.txt");
    scrolly=Integer.parseInt(readery);
    myview.scrollTo(0, scrolly);
    Log.w("TAG", "onResume");
    Log.w("TAG", "Scroll is " + scrolly);
    super.onResume();
}

我的scrollview被定义并链接到onCreate方法中的xmlfile。 writetofile(消息,文件); 完美地工作,就像 readfromfile(文件); 一样 这些值有效(我在logcat中检查过)。

这是我的问题: 当我要求它执行此操作时,为什么程序不会滚动到我的设定位置?

如果有更复杂的东西,我不明白,那么 这是我的片段

package com.hultan.formler;

/*Imports*/

public class LibraryFragment extends Fragment {

/*Variables*/
int scroll;
ScrollView myview;

public void setscroll(){
    int a = 0;
    while (a < 3){
        myview.scrollTo(0, scroll);
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {}
        a= a+1;
        Log.w("TAG", "Test nr." +a);
    }
}
public void fragmentswitch(Fragment newFragment){
    Log.w("TAG", "FragmentSwitch");
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.frame_container, newFragment).addToBackStack(null).commit();
}
public void writetofile(String content, String FILENAME){
    FileOutputStream outputStream;
    try {outputStream = getActivity().openFileOutput(FILENAME, Context.MODE_PRIVATE);
      outputStream.write(content.getBytes());
      outputStream.close();
    } catch (Exception e) {
      e.printStackTrace();}}

private String readfromfile(String FILENAME){
    String ret ="";
    try {InputStream inputStream = getActivity().openFileInput(FILENAME);
        if ( inputStream != null ) {
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String receiveString = "";
            StringBuilder stringBuilder = new StringBuilder();
            while ( (receiveString = bufferedReader.readLine()) != null ) {
                stringBuilder.append(receiveString);}
            inputStream.close();
            ret = stringBuilder.toString();
        }}

    catch (FileNotFoundException e) {
        ret="0";
    } catch (IOException e) {
        ret="0";}
    return ret;}

@Override
public void onPause(){
    scroll = myview.getScrollY();
    String write = Integer.toString(scroll);
    writetofile(write, "myscroll.txt");
    Log.w("TAG", "onPause, scroll="+scroll);
    super.onPause();
}
@Override
public void onResume(){
    String reader = readfromfile("myscroll.txt");
    scroll=Integer.parseInt(reader);
    myview.scrollTo(0, scroll);
    checkvisibilityf1();
    checkvisibilityf2();
    Log.w("TAG", "onResume, scroll="+scroll);
    setscroll();/*Just in case the view is slow to load*/
    super.onResume();
}




@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState)  {
Log.w("TAG", "onCreateView");
    final View rootView = inflater.inflate(R.layout.fragment_library, container, false);
    setvariables();
    myview= (ScrollView) rootView.findViewById(R.id.scroller);
    setbuttons(rootView);

    return rootView;
}   

}

0 个答案:

没有答案