片段恢复默认值onswipe [英] Restore Fragment default values onswipe

本文介绍了片段恢复默认值onswipe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有许多线程在谈论这个,但我想一切,好好尝试一下似乎工作。我的问题是有点简单,我有5个片段页面的viewpager。

there are to many threads talking about this but i tried everything and doens't seem to work. My Problem is kinda simple i have a viewpager with 5 fragments pages.

每个这种片段具有相同的布局只有1编辑文本,而问题是,如果我写一篇关于editext onswipe到明年或previous页东西,我想重置的EditText。

Each of this fragment has the same layout only 1 edit text, and the issue is if i write something on the editext onswipe to next or the previous page i would like to reset that edittext.

例如:
PAGE1:editext = 2929 ---(刷卡)---第2页:EditText上= 0 ----(刷卡)--- PAGE1:EditText上= 0

For example: PAGE1: editext=2929 ---(swipe)--- PAGE2: edittext = 0 ----(swipe)--- PAGE1:edittext = 0

但它始终显示2929。

But it always shows the 2929.

我要做到这一点我的应用程序,即时试图在这个睾丸计划。

I to do this for my app, im trying to this on teste program.

这是我的Adpater

this is my Adpater

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

}

和我喜欢做的同样的按钮监听,重置的EditText但不是单击它,那将是滑动手势是什么。

And what i like to do its the same of the button listener, reset the edittext but instead of clicking it, it would be on swipe gesture.

感谢您,
贡萨洛·莫拉

Thank you, Gonçalo Moura

编辑:
从fragment_one1 XML

xml from fragment_one1

<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删除片段,

I tried remove the fragment from FragmentManger,

    @Override
public int getItemPosition(Object object) {
    return POSITION_NONE;
}

此外,tv.setOffscreenPageLimit(限制);,试图FragmentStatePagerAdapter和FragmentPagerAdapter,我有状态,因为它破坏了片段,使用事件onDetach()的onDestroy()等等等等,如果你看到的片段的code你可以看到我的方法onDetach(),notifydatasetchanged,创下新的适配器。

also, tv.setOffscreenPageLimit(limit);, tried FragmentStatePagerAdapter and FragmentPagerAdapter, i have state because it destroys the fragment, using events onDetach(), onDestroy() etc etc, if you saw the code of the fragment you can see my method onDetach(), notifydatasetchanged, setting new adapter.

推荐答案

好吧,我理解了它。

还有我们都知道viewpager创建3个网页,这个网页将在不可见的,可见设置。我用Google搜索了一圈,发现这样的:

Well as we "all" know viewpager creates 3 pages, and this pages will be set at NOT VISIBLE and VISIBLE. I googled around and found this:

    @Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser) {
        Log.w("Visible1", "CurrentPage " + mCurrentPage);

    } else {
        Log.w("NOTVISIBLE1", "CurrentPage " + mCurrentPage);
        if (edt != null) {
            edt.setText("");
        }
    }
}

好吧,当我刷卡刷和回我的值将先复位,如果是因为认为也许它尚未创建,此方法是onCreate()方法之前调用。但它的作品,我不知道,如果你们同意,或有这更好的解决方案。我希望这会帮助别人。

Well when i swipe and swipe back my values will be reseted, the "if" is because the view maybe its not created yet, this method is called before the onCreate() method. But it works, i don't know if you guys agree or have a better solution for this. I hope it will help someone.

干杯

这篇关于片段恢复默认值onswipe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆