在我现有的 Android 应用程序中,我使用的是 MuPDF,我在 this doc. 的帮助下移植了它 现在,当我想在我使用的 Activity 中打开 pdf 文件时: uri uri = Uri.parse(path);
Intent intent = new Intent(this, MuPDFActivity.class);
intent.setAction(Intent.ACTION_VIEW);
intent.setData(uri);
startActivity(intent);
这会触发一个新 Activity ,我的问题是:(1) 我怎样才能启动 Fragment 来查看 pdf? (2) MuPDF 是否支持我可以在我的 currant Android-Tab-View 下调用的 Fragment? (3) 有没有办法把这个activity转化为fragment?
目前我在做:
public class DummySectionFragment extends Fragment {
public DummySectionFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = null;
rootView = inflater.inflate(R.layout.activity_dummy_section_fragment, container, false);
Intent myIntent = new Intent(getActivity(), MuPDFActivity.class);
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(uri);
getActivity().startActivity(myIntent);
return rootView;
}
}
其中:在我当前的选项卡 View 布局上打开一个新 Activity ,它看起来不太好,因为它覆盖了整个选项卡布局,用户必须单击 BACK
按钮才能查看选项卡 View 。
最佳答案
也许您不应该在您的项目中使用 MuPDFActivity - 它只是 Mupdf 工作原理的一个示例。您只需要 MuPDFReaderView/MuPDFCore/MuPDFPageAdapter。 MuPDFReaderView 扩展自 View/ViewGroup,因此您只需将其添加到您的布局中即可。像这样尝试(完全未经测试!!):
1.) XML --> fragment 的基本布局 (mupdf_wrapper.xml):
<RelativeLayout
android:id="@+id/mupdf_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
2.) JAVA:
public class DummySectionFragment extends Fragment {
public DummySectionFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = null;
rootView = inflater.inflate(R.layout.mupdf_wrapper, container, false);
RelativeLayout mupdfWrapper (RelativeLayout)rootView.findViewById(R.id.mupdf_wrapper);
String path = "path/To/Your/PDF/File.pdf";
MuPDFCore core = new MuPDFCore(getActivity(), path);
MuPDFReaderView mDocView = new MuPDFReaderView(getActivity());
mDocView.setAdapter(new MuPDFPageAdapter(getActivity(), getActivity(), core));
mupdfWrapper .addView(mDocView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
return rootView;
}
关于android - Android版MuPDF : Option for fragment instead Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17803076/