0

有許多線程談論這個,但我嘗試了一切,doens似乎沒有工作。我的問題有點簡單,我有一個視圖與5頁的頁面。恢復片段默認值onswipe

這個片段中的每一個都只有一個編輯文本,並且問題是如果我在editext onswipe上寫入下一個或上一頁的內容,我想重置該edittext。

例如: PAGE1:editext = 2929 ---(刷卡)--- PAGE2:的EditText = 0 ----(刷卡)--- PAGE1:的EditText = 0

但是它總是顯示2929.

我這樣做是爲了我的應用程序,即時嘗試這個在teste程序上。

這是我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但不是點擊它,它會是什麼一直在滑動手勢。

謝謝 貢薩洛·莫拉

編輯: XML從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片段,

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

另外,tv.setOffscreenPageLimit(限制);試過FragmentStatePagerAdapter和FragmentPagerAdapter,我有狀態,因爲它破壞了片段,使用事件onDetach(),onDestroy()等等,如果你看到了片段的代碼,你可以看到我的方法onDetach(),notifydatasetchanged,設置新的適配器。

+0

「......說起這個,但我都試過了......」你能不能給我們例子你嘗試過什麼... – Selvin

+0

你可以將XML代碼? – MikeKeepsOnShine

+0

@Selvin,感謝您的評論,我提到了其中的一些。 – Ratz

回答

0

嗯,我想通了。

那麼,因爲我們「所有」知道viewpager創建3頁,並且這個頁面將被設置爲不可見和可見。我用Google搜索了一圈,發現這樣的:

@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()之前方法。但它的作品,我不知道,如果你們同意還是對此有一個更好的解決方案。我希望它能幫助別人。

乾杯