Restore Fragment default values onswipe

独自空忆成欢 提交于 2019-12-12 02:37:39

问题


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.

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.

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

But it always shows the 2929.

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

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

this my fragment

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

}

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

EDIT: 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>

view pager:

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

I tried remove the fragment from FragmentManger,

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

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.


回答1:


well i figured it out.

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

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.

Cheers



来源:https://stackoverflow.com/questions/20640078/restore-fragment-default-values-onswipe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!