Android Viewpager不删除当前页面位置

Android Viewpager不删除当前页面位置,android,checkbox,android-edittext,android-viewpager,Android,Checkbox,Android Edittext,Android Viewpager,我正在side activity中使用ViewPager,添加片段状态寻呼机适配器,并尝试在添加一些页面后删除当前页面位置。但每次都删除最后一页位置。因此,请推荐我使用正确的方法来执行此操作。提前感谢 这是我的活动和适配器代码 public class Main-Activity extends Fragment-Activity implements AddItemFragment.OnFragmentInteractionListener { PagerAdapter mAdapt

我正在side activity中使用
ViewPager
,添加片段状态寻呼机适配器,并尝试在添加一些页面后删除当前页面位置。但每次都删除最后一页位置。因此,请推荐我使用正确的方法来执行此操作。提前感谢

这是我的活动和适配器代码

public class Main-Activity extends Fragment-Activity implements AddItemFragment.OnFragmentInteractionListener {

    PagerAdapter mAdapter;
    ViewPager mPager;
    Button button ,btn_add;
    int pos,TOTAL_PAGES=0;

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

//THIS IS THE MODEL CLASS WHERE I ADD THE PAGES.

        MoveItems.items = new ArrayList<>();
        MoveItems.items.clear();
        Intialize();


    }


    public void Intialize() {

        mPager = (ViewPager)findViewById(R.id.pager);

        mAdapter = new PagerAdapter(getSupportFragmentManager());
        mPager.setAdapter(mAdapter);

        mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {


                pos = position + 1;


            }

            @Override
            public void onPageScrollStateChanged(int state) {
                //Function.toast(MainActivity.this, "onPageScrollStateChanged");

            }
        });

        Addpage();


        button = (Button)findViewById(R.id.delete_current);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mAdapter.deletePage(mPager.getCurrentItem());
                Function.toast(MainActivity.this,"'Delete page");

            }
        });

        btn_add=(Button)findViewById(R.id.goto_first);
        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Addpage();

            }
        });


    }


    public void Addpage() {

        TOTAL_PAGES++;

        ItemDescription item = new ItemDescription();
        MoveItems.items.add(item);


        if (mAdapter != null)
            mAdapter.notifyDataSetChanged();

        mPager.setCurrentItem((TOTAL_PAGES - 1));


    }


    @Override
    public void onFragmentCreated(ItemDescription itemDescription, int position) {
        if (mAdapter != null)
            mAdapter.notifyDataSetChanged();
    }


    public class PagerAdapter extends FragmentStatePagerAdapter
    {


        public PagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {

            ItemDescription description = new ItemDescription();
            description.setItemNo(position);
            description = MoveItems.items.get(position);



            return AddItemFragment.newInstance(position, description);
        }

        @Override
        public int getCount() {

            return MoveItems.items.size();

        }

        public void deletePage(int position)
        {
            MoveItems.items.remove(position);;
            notifyDataSetChanged();
        }


    }




}
这是带有复选框和编辑文本的片段添加项

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="10dp"
            android:layout_marginTop="20dp"

            >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="5dp"
                android:orientation="horizontal">

                <CheckBox
                    android:id="@+id/furniture"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:paddingLeft="10dp"
                    android:singleLine="true"
                    android:text="Furniture"

                    android:textSize="15sp" />

                <CheckBox
                    android:id="@+id/mattress"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:paddingLeft="10dp"
                    android:singleLine="true"
                    android:text="Mattress"

                    android:textSize="15sp" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:paddingLeft="5dp"
                android:orientation="horizontal">

                <CheckBox
                    android:id="@+id/box"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:paddingLeft="10dp"
                    android:singleLine="true"
                    android:text="Box"
                    android:textSize="15sp" />

                <CheckBox
                    android:id="@+id/appliance"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:paddingLeft="10dp"
                    android:singleLine="true"
                    android:text="Appliance"
                    android:textSize="15sp" />

            </LinearLayout>

            <android.support.design.widget.TextInputLayout
                android:id="@+id/other_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp">

                <EditText
                    android:id="@+id/other"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Other"
                    android:singleLine="true"
                    android:textSize="15sp" />

            </android.support.design.widget.TextInputLayout>

            <android.support.design.widget.TextInputLayout
                android:id="@+id/notes_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <EditText
                    android:id="@+id/item_description"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Item Notes"
                    android:singleLine="true"
                    android:textSize="15sp" />

            </android.support.design.widget.TextInputLayout>



            <LinearLayout
                android:id="@+id/line1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView android:id="@+id/title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:padding="2dp"
                    android:text="Add a Photo"
                    android:textSize="15sp" />

                <TextView android:id="@+id/Des"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:padding="2dp"
                    android:textSize="12sp" />
            </LinearLayout>
        </LinearLayout>

</LinearLayout>


当我在Fragment_Add_Item layout中获得任何输入类型时,就会出现此问题。请帮助我找出此问题。这对我来说非常重要,我使用的是第一次查看页面。在这段代码中,查看页面的项被删除了,但并不精确。所以我用不同的方法来实现这一点。但是没有得到正确的输出。提前谢谢

你到底想要什么?事实上,先生,我有一个带有复选框和编辑文本的片段添加项目布局。首先,我增加了四页。我选中第一页中的第一个复选框,第二页选择第二个复选框,第三页选择第三个复选框,第四页选择四个复选框。我想删除第二页。请帮助我,如果有人知道任何IDIAW你到底想要什么?事实上,先生,我有片段添加项目布局,有复选框和编辑文本。首先,我增加了四页。我选中第一页中的第一个复选框,第二页选择第二个复选框,第三页选择第三个复选框,第四页选择四个复选框。我想删除第二页。如果有人知道任何idia,请帮助我
public class AddItemFragment extends Fragment {

    // Store instance variables
    private int page;

    ItemDescription description;
    CheckBox furniture, mattress, box, appliance;
    EditText other, item_description;


    private OnFragmentInteractionListener listener;
    LinearLayout line1;
    CheckBox checkboxes[];
    View v;

    // newInstance constructor for creating fragment with arguments
    public static AddItemFragment newInstance(int page, ItemDescription item) {

        AddItemFragment fragmentFirst = new AddItemFragment();
        Bundle args = new Bundle();
        args.putInt("pagecount", page);

        args.putSerializable("item", item);

        fragmentFirst.setArguments(args);
        return fragmentFirst;
    }


    @Override
    public void onAttach(Activity activity) {

        try {
            listener = (OnFragmentInteractionListener) getActivity();
        }
        catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnFragmentInteractionListener");
        }
        super.onAttach(activity);
    }



    /**
     * Interface for communicating data
     */
    public interface OnFragmentInteractionListener {
        public void onFragmentCreated(ItemDescription itemDescription, int position);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        v = inflater.inflate(R.layout.fragment_add_item, container, false);

        page = getArguments().getInt("pagecount", 0);
        description = (ItemDescription) getArguments().getSerializable("item");

        One = (CheckBox) v.findViewById(R.id.furniture);
        Two = (CheckBox) v.findViewById(R.id.mattress);
        Three = (CheckBox) v.findViewById(R.id.box);
        Four = (CheckBox) v.findViewById(R.id.appliance);



        other = (EditText) v.findViewById(R.id.other);
        item_description = (EditText) v.findViewById(R.id.item_description);

        checkboxes = new CheckBox[]{One, Two, Three, Four};

        return v;

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        BitmapsForImage bitmapsForImage = null;
        imagePath = "";
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == Constants.ImagePick) {
                Uri imageUri = getPickImageResultUri(data);
                imagePath = getPath(getActivity(), imageUri);
                bitmapsForImage = Function.resizeBitmap(imagePath, getActivity());
            } else if (Constants.CAMERA_REQUEST_CODE == requestCode) {
                bitmapsForImage = Function.resizeBitmap(imagePath, getActivity());
            } else if (Constants.GALLERY_IMAGE == requestCode && null != data) {
                if (data != null) {
                    Uri contentURI = data.getData();
                    String imagePath = Function.getRealPathFromURI(getActivity(), contentURI);
                    bitmapsForImage = Function.resizeBitmap(imagePath, getActivity());

                }
            }
            if (bitmapsForImage != null && imagePath != null && imagePath.length() > 0) {

                MoveItems.itemsBitmapList.set(page, bitmapsForImage.getBitmapToShow());
                line1.setVisibility(View.GONE);
                description.setItemFilePath(imagePath);
                description.setItemFile(new File(imagePath));
                MoveItems.items.get(page).setItemFile(new File(imagePath));
                MoveItems.items.get(page).setItemFilePath(imagePath);

                try {
                    listener.onFragmentCreated(description, page);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                Function.toast(getActivity(), "Some Error Occured");
                return;
            }
        }
    }
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="10dp"
            android:layout_marginTop="20dp"

            >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="5dp"
                android:orientation="horizontal">

                <CheckBox
                    android:id="@+id/furniture"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:paddingLeft="10dp"
                    android:singleLine="true"
                    android:text="Furniture"

                    android:textSize="15sp" />

                <CheckBox
                    android:id="@+id/mattress"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:paddingLeft="10dp"
                    android:singleLine="true"
                    android:text="Mattress"

                    android:textSize="15sp" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:paddingLeft="5dp"
                android:orientation="horizontal">

                <CheckBox
                    android:id="@+id/box"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:paddingLeft="10dp"
                    android:singleLine="true"
                    android:text="Box"
                    android:textSize="15sp" />

                <CheckBox
                    android:id="@+id/appliance"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:paddingLeft="10dp"
                    android:singleLine="true"
                    android:text="Appliance"
                    android:textSize="15sp" />

            </LinearLayout>

            <android.support.design.widget.TextInputLayout
                android:id="@+id/other_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp">

                <EditText
                    android:id="@+id/other"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Other"
                    android:singleLine="true"
                    android:textSize="15sp" />

            </android.support.design.widget.TextInputLayout>

            <android.support.design.widget.TextInputLayout
                android:id="@+id/notes_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <EditText
                    android:id="@+id/item_description"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Item Notes"
                    android:singleLine="true"
                    android:textSize="15sp" />

            </android.support.design.widget.TextInputLayout>



            <LinearLayout
                android:id="@+id/line1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView android:id="@+id/title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:padding="2dp"
                    android:text="Add a Photo"
                    android:textSize="15sp" />

                <TextView android:id="@+id/Des"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:padding="2dp"
                    android:textSize="12sp" />
            </LinearLayout>
        </LinearLayout>

</LinearLayout>