有很多线程在谈论这个,但我尝试了一切,但似乎没有用。我的问题有点简单,我有一个带有 5 个片段页面的 viewpager。
这个片段中的每一个都具有相同的布局,只有 1 个编辑文本,问题是如果我在 editext onswipe 上写一些内容到下一页或上一页,我想重置该 edittext。
例如:PAGE1:editext=2929 ---(swipe)--- PAGE2:edittext = 0 ----(swipe)--- PAGE1:edittext = 0
但它总是显示 2929。
我为我的应用程序执行此操作,我正在尝试在 teste 程序上执行此操作。
这是我的适配器
public class MyFragmentPagerAdapter extends FragmentStatePagerAdapter {
int PAGE_COUNT;
int total;
Fragment frag;
FragmentManager fmm;
/** Constructor of the class */
public MyFragmentPagerAdapter(FragmentManager fm, int x, int total) {
super(fm);
fmm = fm;
total = 0;
PAGE_COUNT = x;
}
/** This method will be invoked when a page is requested to create */
@Override
public Fragment getItem(int arg0) {
final FragmentTab11 myFragment = new FragmentTab11();
Bundle data = new Bundle();
data.putInt("current_page", arg0 + 1);
myFragment.setArguments(data);
return myFragment;
}
/** Returns the number of pages */
@Override
public int getCount() {
return PAGE_COUNT;
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
这是我的片段
public class FragmentTab11 extends SherlockFragment {
int mCurrentPage;
TextView tv;
EditText edt;
Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/** Getting the arguments to the Bundle object */
Bundle data = getArguments();
/** Getting integer data of the key current_page from the bundle */
mCurrentPage = (data != null) ? data.getInt("current_page", 0) : 1;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_one1, container, false);
tv = (TextView) v.findViewById(R.id.textView1);
tv.setText("You are viewing the page #" + mCurrentPage
+ " of Frag1\n\n" + "Swipe Horizontally left / right");
btn = (Button) v.findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
update();
}
});
edt = (EditText) v.findViewById(R.id.editText1);
return v;
}
public void update() {
Log.w("text", mCurrentPage+": " + edt.getText().toString());
edt.setText("");
}
@Override
public void onDetach() {
edt.post(new Runnable() {
@Override
public void run() {
edt.setText("");
Log.w("DETACH", "CurrentPage " + mCurrentPage);
}
});
super.onDetach();
}
我喜欢对按钮监听器做同样的事情,重置编辑文本,但不是点击它,而是滑动手势。
谢谢你,贡萨洛·莫拉
编辑:来自 fragment_one1 的 xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="1" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:ems="10" >
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" /></LinearLayout>
查看寻呼机:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" /></LinearLayout>
我尝试从 FragmentManger 中删除片段,
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
另外,tv.setOffscreenPageLimit(limit);,尝试了 FragmentStatePagerAdapter 和 FragmentPagerAdapter,我有状态,因为它破坏了片段,使用事件 onDetach()、onDestroy() 等,如果你看到片段的代码,你可以看到我的方法 onDetach (), notifydatasetchanged, 设置新的适配器。