0

我想要的是当我单击我的主片段中的列表视图中的一个项目时,打开文档,我可以阅读它并滚动。我已经设法向我展示了我的 android 的所有 pdf,并且我将它很好地加载到了一个数组中,但是当我单击时,我得到了一个错误。

我加载列表视图视图的片段类

public class HomeFragment extends Fragment {

    private File dir;
    private ListView lv_pdf;
    boolean bolean_permission;
    SearchView searchView;
    PDFView pdfView;
    int position = -1;

    private static ArrayList<File>
            fileList = new ArrayList<>();

    public HomeFragment() {
        // Required empty public constructor  CONSTRUCTOR VACIO DE LA CLASE HOME FRAGMENT
    }

    public static HomeFragment newInstance(/*parámetros*/) {
        // Setup parámetros           SETEO PARAMETROS
        return new HomeFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);

        if (getArguments() != null) {
            // Gets parámetros   OBTENGO PARAMETROS

            pdfView = (PDFView) getActivity().findViewById(R.id.pdfView);

            position = getActivity().getIntent().getIntExtra("position", -1);

            displayPDF();

        }

    }

显示以打开 pdf

private void displayPDF() {
    pdfView.fromFile(MainActivity.fileList.get(position))

            .enableSwipe(true)
            .enableAnnotationRendering(true)
            .scrollHandle(new DefaultScrollHandle(getActivity()))
            .load();

}

@Override
public View onCreateView(LayoutInflater inflater , ViewGroup container ,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_home, container, false);

    //ArrayList<String> data = new ArrayList<>();
    //data.add("Eder");
    //data.add("Viera");

    //ArrayAdapter<String> adapter = new ArrayAdapter<String>(
            //getContext(),
            // android.R.layout.simple_list_item_1,
            // data
            // );

    // ListView lvdata = (ListView)view.findViewById(listappalhome);
    // lvdata.setAdapter(adapter);

    lv_pdf = view.findViewById((R.id.listappalhome));

    dir = new File(Environment.getExternalStorageDirectory().toString());

    lv_pdf.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, final View view,
                                int position, long id) {

        }

    });

    //lv_pdf.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    // public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
    //Intent intent = new Intent(getActivity(), VerArchivosPDF.class);
            // add data to the intent...
    //startActivity(intent);
    //}
    //}  );

    permission_fn();

    return view;

}

public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
    // TODO Add your menu entries here
    super.onCreateOptionsMenu(menu, inflater);
}

private void permission_fn() {

    if ((ContextCompat.checkSelfPermission((getActivity()).getApplication().getApplicationContext(),

            Manifest.permission.READ_EXTERNAL_STORAGE) !=PackageManager.PERMISSION_GRANTED)) {

        if ((ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),
                Manifest.permission.READ_EXTERNAL_STORAGE))) {

        } else {

            int REQUEST_PERMISSION = 1;
            ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                    REQUEST_PERMISSION);

        }

    } else {

        bolean_permission = true;
        getfile(dir);
        adaptadorPDF obj_adapter = new adaptadorPDF(getActivity().getApplicationContext() , fileList);
        lv_pdf.setAdapter(obj_adapter);

    }
}

private void getfile(File dir) {

    File[] listFile = dir.listFiles();

    if (listFile!=null && listFile.length >0) {

        for (File file : listFile) {

            if (file.isDirectory()) {

                getfile(file);

            } else {

                boolean booleanpdf = false;

                if (file.getName().endsWith(".pdf")) {

                    for (int j = 0; j < fileList.size(); j++) {

                        if (fileList.get(j).getName().equals(file.getName())) {

                            booleanpdf = true;
                        }
                    }

                    if (booleanpdf) {

                        booleanpdf = false;
                    } else {

                        fileList.add(file);
                    }
                }
            }
        }

    }
}


}

打开pdf课

public class OpenPDF extends AppCompatActivity{

    PDFView pdfView;
    int position = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ver_archivos_pdf);

        pdfView = (PDFView) findViewById(R.id.pdfView);

        position = getIntent().getIntExtra("position", -1);

        displayPDF();

    }

    private void displayPDF() {

        //pdfView.fromFile(HomeFragment.fileList.get(position))

        //.enableSwipe(true)
        //.enableAnnotationRendering(true)
        //.scrollHandle(new DefaultScrollHandle(this))
        //.load();

    }
}

主要活动

public class MainActivity extends AppCompatActivity  {

    private AppBarConfiguration mAppBarConfiguration;

    adaptadorPDF obj_adapter;
    public static int REQUEST_PERMISSION = 1;
    boolean bolean_permission;
    File dir;
    ListView lv_pdf;
    DrawerLayout drawer;

    public static ArrayList<File>
            fileList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);

        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.

        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow, R.id.nav_scanqr, R.id.navfirmadigital,
                R.id.nav_tools, R.id.nav_share, R.id.nav_send)
                .setDrawerLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.contenedor_fragmentos);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);

        HomeFragment homeFragment = new HomeFragment();
        FragmentManager fragmentManager=getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.listappalhome, homeFragment).commit();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        MenuItem item = menu.findItem(R.id.nav_busqueda);
        SearchView searchView = (SearchView) item.getActionView();
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.contenedor_fragmentos);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }

    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        drawer.closeDrawer(GravityCompat.START);

        int id = item.getItemId();

        FragmentManager fragmentManager=getSupportFragmentManager();

        if (id== R.id.nav_home){

            fragmentManager.beginTransaction().replace(R.id.contenedor_fragmentos, new HomeFragment()).commit();

        } else if (id == R.id.nav_gallery) {
            fragmentManager.beginTransaction().replace(R.id.contenedor_fragmentos, new GalleryFragment()).commit();

        } else if (id == R.id.nav_send) {
            fragmentManager.beginTransaction().replace(R.id.contenedor_fragmentos, new SendFragment()).commit();
        } else if (id == R.id.nav_slideshow) {
            fragmentManager.beginTransaction().replace(R.id.contenedor_fragmentos, new SlideshowFragment()).commit();
        } else if (id == R.id.nav_share) {
            fragmentManager.beginTransaction().replace(R.id.contenedor_fragmentos, new ShareFragment()).commit();
        } else if (id == R.id.nav_tools) {
            fragmentManager.beginTransaction().replace(R.id.contenedor_fragmentos, new ToolsFragment()).commit();

        } else if (id == R.id.nav_busqueda) {
            fragmentManager.beginTransaction().replace(R.id.contenedor_fragmentos, new BusquedaFragment()).commit();

        }

        return true;
    }

    private void changeFragment(Fragment fragment, MenuItem item){
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.contenedor_fragmentos, fragment)
                .commit();
        item.setChecked(true);
        getSupportActionBar().setTitle(item.getTitle());
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        //noinspection SimplifiableIfStatement
        if (id == R.id.listappalhome) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

具有列表视图的布局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".HomeFragment"
    android:orientation="horizontal">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/textviewhome"/>


    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listappalhome"
        android:divider="#efefef"
        android:dividerHeight="5dp"
        android:layout_gravity="center_horizontal"/>


</FrameLayout>

4

0 回答 0