3

我的应用程序有 2 个包含 webviews 的片段,当我在 2 个片段之间滑动时,我得到了不需要的滚动。如果我将滚动条留在片段的顶部,当我滑动并向后滑动时,滚动条会向下一点(总是到相同的位置)。另一方面,如果我在滑动之前向下滚动了一点片段,当我滑动并向后滑动时,y 滚动值保持不变。

我可以使用 setUserVisibleHint() 方法强制 y 滚动值转到片段的顶部,但这不是我希望片段执行的操作。

这是我的代码:

public class AgingChartFragment extends Fragment {

private static final String TAG = "AgingChartFragment";
private WebView chartView;
private AlertDialogManager alert = new AlertDialogManager();
private IAsyncTask asyncTasklistener;
private String jsAgingString = "";

private String chartPart1;
private String chartPart2;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    Log.v(TAG, "onCreate()");

    //listener to display a chart in my webview using javascript
    asyncTasklistener = new IAsyncTask() {

    @Override
    public void onTaskCompleted(JSONArray result) {
        Aging[] listAging = null;
        if (result != null) {
            Log.v(TAG, "onTaskCompleted, " + result.toString());
        listAging = new Aging[result.length()];
        try {
            listAging = parseListAging(result);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        jsAgingString = formatAgingDataString(listAging);
        chartView.loadUrl("javascript:" + jsAgingString);
        } else {
        alert.showAlertDialog(getActivity(), "Server Error",
            "Please refresh screen later", false);
        }
    }
};

this.setRetainInstance(true);

new GetAgingDataTask(asyncTasklistener).execute();

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v(TAG, "onCreateView()");

View mainView = (View) inflater.inflate(R.layout.aging_chart, container, false);

chartView = (WebView) mainView.findViewById(R.id.chartView);
chartView.getSettings().setJavaScriptEnabled(true);
chartView.setWebViewClient(new WebViewClient() {

    @Override
    public void onPageFinished(WebView view, String url) {
    if (!jsAgingString.isEmpty()) {
        Log.v(TAG, "onPageFinished");
        chartView.loadUrl("javascript:" + jsAgingString);
    }
    Log.v(TAG, "onpagefinished " + url);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
    Log.v(TAG, "shouldOverrideUrlLoading " + url);
    return true;
    }
});

chartView.loadUrl("file:///android_asset/www/agingChart.htm");

return mainView;
}

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);

    if (isVisibleToUser == true) {
        Log.v(TAG, "setUserVisibleHint :" + isVisibleToUser);
        if (scrollView != null) {
            Log.v(TAG, "scrollview not null");
            scrollView.post(new Runnable() {
                public void run() {
                    Log.v(TAG, "restoreInstanceState");
                    scrollView.scrollTo(0, 0);
                }
            });
        }
    } else if (isVisibleToUser == false) {
        Log.v(TAG, "setUserVisibleHint :" + isVisibleToUser);
    }

}

还有我的 xml 文件:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scroll_aging"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".AgingChartFragment" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

 <TextView
        android:id="@+id/site_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="16dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/equipment_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:gravity="center"/>

         <WebView
        android:id="@+id/chartView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

4

0 回答 0