FragmentStatePagerAdapter getitem问题

时间:2016-02-16 18:56:16

标签: android fragmentstatepageradapter

我做了一个照片库。图片存储在目录中。 但如果目录中有3张图片,则只显示2张图片。 getcount返回3。 我在listfiles中有3个元素。文件名是正确的。 但是我觉得getitem被称为2次,位置0& 1。

在args.putInt行(DemoObjectFragment.ARG_OBJECT,i + 1);,我尝试使用i,i + 1,i-1。 (与i-1坠毁:))但其余我只有2张照片

 public static class DemoCollectionPagerAdapter extends FragmentStatePagerAdapter{
    String path = Environment.getExternalStorageDirectory() + "PATH";
    public DemoCollectionPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        Fragment fragment = new DemoObjectFragment();
        Bundle args = new Bundle();
        args.putInt(DemoObjectFragment.ARG_OBJECT, i+1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getCount() {
        File f = new File(path);
        final File filePict[] = f.listFiles();
        int nb = filePict.length-1;
        return nb;
    }}
public static class DemoObjectFragment extends Fragment {

    public static final String ARG_OBJECT = "object";

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

        Bundle args = getArguments();
        String path = Environment.getExternalStorageDirectory() + "PATH";
        File f = new File(path);
        final File filePict[] = f.listFiles();
        if(filePict.length>0) {

                    ((ImageView) rootView.findViewById(R.id.pictgal)).setImageBitmap(BitmapFactory.decodeFile(filePict[args.getInt(ARG_OBJECT)].toString()));
            rootView.findViewById(R.id.butpict).setTag(filePict[args.getInt(ARG_OBJECT)].toString());
        }
        return rootView;
    }
}

0 个答案:

没有答案