vendredi 15 avril 2016

Viewpager not delete current page position

I am using view-pager in side activity and add Fragment-State-Pager-Adapter and trying to remove current page position after add the some pages.but every time removed last page position.so please recommend me for use right approach for doing this.Thanks in advance.



This is my activity and adapter code.   


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


    }




}

This The XML PART

<LinearLayout xmlns:android="http://ift.tt/nIICcg"
    android:orientation="vertical" android:padding="4dip"
    android:gravity="center_horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1">
    </android.support.v4.view.ViewPager>

    <LinearLayout android:orientation="horizontal"
        android:gravity="center"
        android:measureWithLargestChild="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0">

        <!--go to the first page in the view pager-->
        <Button android:id="@+id/goto_first"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="First">
        </Button>

        <!--Delete the current page-->
        <Button android:id="@+id/delete_current"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Delete">
        </Button>

        <!--go to the last page in the view pager-->
        <Button android:id="@+id/goto_last"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Last">
        </Button>
    </LinearLayout>
</LinearLayout>

THIS THE FRAGMENT CLASS WHERE I INFLATE THE LAYOUT WHICH HAVE CHECK-BOXES AND EDITEXT.

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

THIS IS THE Fragment_Add_Item WHICH HAVE CHECK BOX AND EDIT TEXT.

<LinearLayout xmlns:android="http://ift.tt/nIICcg"
    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>

This problem occurs when I get any input type inside Fragment_Add_Item layout please help me for find out this problem.This very-very important from me and i am using first time view-pager. In this code item of view-pager deleted but not exact.so i was doing this differnt ways. but not getting right output. thanks in advance.




Aucun commentaire:

Enregistrer un commentaire