MuPDF for Android: Option for fragment instead Act

2019-02-17 04:25发布

问题:

In my existing android app, Im using MuPDF, which i ported with help of this doc. Now when i want to open pdf files inside activity i use : Uri uri = Uri.parse(path);

            Intent intent = new Intent(this, MuPDFActivity.class);
            intent.setAction(Intent.ACTION_VIEW);
            intent.setData(uri);
            startActivity(intent);

which fires a new activity, My problem is: (1) how can I start Fragment to view pdf? (2) Does MuPDF supports Fragment that I can call under my currant Android-Tab-View? (3) Is there a Way Converting this activity into fragment?

Currently i'm doing:

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;
        }
}

Which: opens a new activity on my current Tab View layout, which does not look great as it covers entire tab layout and user have to click BACK button to view tab view.

回答1:

Maybe you shouldn't use the MuPDFActivity in your project - it's just an example how Mupdf works. All what you need is the MuPDFReaderView/MuPDFCore/MuPDFPageAdapter. MuPDFReaderView extends from View/ViewGroup, so you can just add it to your layout. Try it like this (totally untested!!):

1.) XML --> The base layout for the 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;
    }

}



回答2:

Converting activity to fragment :

public class a extend activity{

     public void oncreate(Bundle Saveinstance)
     {
     super.oncreate(saveinstance);
     setcontentview(r.layout.xyz);
     }    
    }

Converting ... :

  public class a extend fragment{


 public void onstart()
 {
 super.onstart();

 }   

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub


    return inflater.inflate(com.example.login.R.layout.fragment, container, false);

} 
}

If you have a function or you wanna make a Toast , and you have xyz.this replace him with getActivity()

Toast.makeText(xyz.this, e.getMessage(),Toast.LENGTH_LONG).show(); 
Toast.makeText(getActivity(), e.getMessage(),Toast.LENGTH_LONG).show();