我在旁边活动中使用ViewPager
并添加Fragment-State-Pager-Adapter并在添加一些页面后尝试删除当前页面位置。但每次删除最后一页位置。请推荐我使用这样做的正确方法。谢谢。
这是我的活动和适配器代码。
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();
}
}
}
这是XML PART
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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>
这是片段类别,我在那里填写了检查框和编辑内容的布局。
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;
}
}
}
}
这是检查框和编辑文本的Fragment_Add_Item。
<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布局中获得任何输入类型时,会出现此问题,请帮我找出这个问题。这对我来说非常重要,我正在使用第一次查看分页器。在这个view-pager的代码项中删除但不精确。所以我正在做这种不同的方式。但没有得到正确的输出。提前致谢。