我正在创建一个包含产品列表的应用。我为每个片段显示一个产品(到目前为止总共有5个,我在片段上显示硬代码照片)但是现在我需要在创建/显示片段时保存变量。
当我启动应用程序片段1(代码中的位置 0 )时,我向左滑动,显示片段2,然后是片段3,等等。每个片段代码中的变量从ArrayList中提取一个字符串。如果在片段1上,那么它将获取数组中的第一个字符串。如果在片段2上,那么它将从数组中获取第二个字符串,依此类推。这意味着显示的产品/片段与正确的字符串匹配。
问题是,当我到达片段3(或任何其他点)并滑动到 右 时,片段2会显示但由于某种原因我的代码认为我在片段1上,现在字符串不代表正在显示的正确产品/片段。
每个产品和字符串变量完美匹配非常重要。
FragmentStatePagerAdapter代码:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
public class SwipeAdapter extends FragmentStatePagerAdapter {
public SwipeAdapter(FragmentManager fm) {
super(fm);
}
public static String Product;
Fragment fragment = null;
@Override
public Fragment getItem(int position) {
if (position == 0) {
fragment = new BuyerHomePageFragment();//Calls the fragment.
}
if (position == 1) {
fragment = new BuyerHomePageFragment2();//Calls the fragment.
}
if (position == 2) {
fragment = new BuyerHomePageFragment3();//Calls the fragment.
}
if (position == 3) {
fragment = new BuyerHomePageFragment4();//Calls the fragment.
}
if (position == 4) {
fragment = new BuyerHomePageFragment5();//Calls the fragment;
}
return fragment;
}
@Override
public int getCount() {
int Count;
Count = 5;
return Count;//Sets the specific number of pages.
}
}
片段代码:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class BuyerHomePageFragment extends Fragment {
public static String ProductOne;
public static boolean ProductOneActive = false;
public BuyerHomePageFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_buyer_home_page, container, false);
//"Product" is a string that holds the first string the the array list "Products".
//In the different fragments the index for the ArrayList changes to 1, 2, etc.
//Every fragment has the same code as this one. Except the numbers increment.
SwipeAdapter.Product = BuyerHomePage.Products.get(0);
return view;
}
}
Android Manager日志:
//The start(fragment 0 or product 1)
04-07 23:08:00.028 3553-3553/com.wilsapp.wilsapp W/FragmentManager: moveToState: Fragment state for BuyerHomePageFragment2{a04fed4 #0 id=0x7f0d00be} not updated inline; expected state 3 found 2
//One slide to the left and the second fragment is shown. All is good
04-07 23:08:09.123 3553-3553/com.wilsapp.wilsapp W/FragmentManager: moveToState: Fragment state for BuyerHomePageFragment3{a062d70 #2 id=0x7f0d00be} not updated inline; expected state 3 found 2
//One more slide and the third fragment is shown and still all is good
//The variables match up with the product perfectly.
04-07 23:08:10.867 3553-3553/com.wilsapp.wilsapp W/FragmentManager: moveToState: Fragment state for BuyerHomePageFragment4{db48c21 #3 id=0x7f0d00be} not updated inline; expected state 3 found 2
//Now i slid to the RIGHT and the 2nd fragment is shown but
//now it restarts the position and the string that represents the first product is linked with
//the second product.
04-07 23:08:15.598 3553-3553/com.wilsapp.wilsapp W/FragmentManager: moveToState: Fragment state for BuyerHomePageFragment{1b69fa3 #1 id=0x7f0d00be} not updated inline; expected state 3 found 2
我怎样才能返回片段而不重新开始这个位置,这样我就失去了代表的产品字符串?
一切都会有所帮助!
- - - - - - - - - - 的 *** *** ANSWER - - - - - - - - - - - -
不是通过方法查看并无休止地查找错误修复或解释的表单,而是使用了这个:
private static boolean m_iAmVisible;
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
m_iAmVisible = isVisibleToUser;
if (m_iAmVisible) {
SwipeAdapter.Product = BuyerHomePage.Products.get(0);
}
}
这是我在其他Stack Overflow帖子上找到here的方法。它基本上检查片段是否对用户可见,以及我在哪里设置我的变量。现在我可以去每一个时间前进,并且可能时间回来,正确的变量得到更新!
此外,您将此方法放在每个片段代码中!