您好我有2个XML布局和2个java类。我想要他们合并。
在main.activity.java中的我用这个方法调用它们:
switch (position % 4) {
case 0:
return WebViewGoogle.newInstance();
case 1:
return ScrollFragment.newInstance();
}
ScrollFragment.java:
public class ScrollFragment extends Fragment {
private ObservableScrollView mScrollView;
public static ScrollFragment newInstance() {
return new ScrollFragment();
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_scroll, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mScrollView = (ObservableScrollView) view.findViewById(R.id.scrollView);
MaterialViewPagerHelper.registerScrollView(getActivity(), mScrollView, null);
}
}
这也是WebViewGoogle.java
public class WebViewGoogle extends Fragment {
private ObservableWebView mWebView;
public static WebViewGoogle newInstance() {
return new WebViewGoogle();
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.webview_layout, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mWebView = (ObservableWebView) view.findViewById(R.id.webViewPars);
MaterialViewPagerHelper.preLoadInjectHeader(mWebView);
mWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
MaterialViewPagerHelper.injectHeader(mWebView, true);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
mWebView.loadUrl("http://www.google.com");
MaterialViewPagerHelper.registerWebView(getActivity(), mWebView, null);
}
}
现在我有2个包含此内容的xml文件:
fragment_scroll.xml:
<com.github.ksoichiro.android.observablescrollview.ObservableScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="@layout/material_view_pager_placeholder"/>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/cardMarginVertical"
android:layout_marginLeft="@dimen/cardMarginHorizontal"
android:layout_marginRight="@dimen/cardMarginHorizontal"
android:layout_marginTop="@dimen/cardMarginVertical"
android:background="@android:color/white"
app:cardElevation="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="1000dp"
android:orientation="vertical">
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</com.github.ksoichiro.android.observablescrollview.ObservableScrollView>
另一个xml是webview_layout.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="5dp"
android:paddingRight="5dp"
>
<com.github.ksoichiro.android.observablescrollview.ObservableWebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webViewPars"
android:background="@android:color/transparent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
这两个布局及其java类现在正常工作。
但我想合并它们。为什么?因为我想把webview放在CardView
所以我以这种方式合并2 xml布局:(newlayout.xml)
<com.github.ksoichiro.android.observablescrollview.ObservableScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/material_view_pager_placeholder"/>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="@dimen/cardMarginVertical"
android:layout_marginLeft="@dimen/cardMarginHorizontal"
android:layout_marginRight="@dimen/cardMarginHorizontal"
android:layout_marginTop="@dimen/cardMarginVertical"
android:background="@android:color/white"
app:cardElevation="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="1000dp"
android:orientation="vertical">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="5dp"
android:paddingRight="5dp"
>
<com.github.ksoichiro.android.observablescrollview.ObservableWebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webViewPars"
android:background="@android:color/transparent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</com.github.ksoichiro.android.observablescrollview.ObservableScrollView>
我认为这是真的,但我怎么能为他们的java类做呢?
第1步:
我创建了一个新的java claaa:
newclass.java
第2步
在main_activity
我称之为:
case 3: return newclass.newInstance();
现在我在newlayout中有2个ID:scrollView和webViewPars ...现在如何合并2个java类??