Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 滑动碎片位置=0?_Java_Android_Android Fragments_Android Fragmentactivity - Fatal编程技术网

Java 滑动碎片位置=0?

Java 滑动碎片位置=0?,java,android,android-fragments,android-fragmentactivity,Java,Android,Android Fragments,Android Fragmentactivity,我正在创建一个包含产品列表的应用程序。我为每个片段显示一个产品(到目前为止总共是5个,我在片段上显示了一张硬代码照片),但现在我需要在创建/显示片段时保存一个变量 当我启动应用程序片段1(代码中的position0)时,我向左滑动,显示片段2,然后是片段3,依此类推。每个片段代码中的变量从ArrayList中提取一个字符串。如果在片段1上,那么它将获取数组中的第一个字符串。如果在片段2上,那么它将从数组中获取第二个字符串,依此类推。这意味着显示的产品/片段与正确的字符串匹配 问题是,当我到达片段

我正在创建一个包含产品列表的应用程序。我为每个片段显示一个产品(到目前为止总共是5个,我在片段上显示了一张硬代码照片),但现在我需要在创建/显示片段时保存一个变量

当我启动应用程序片段1(代码中的position0)时,我向左滑动,显示片段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;
}
}
//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
片段代码:

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;
}
}
//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
Android管理器日志:

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;
}
}
//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
如何返回片段而不重新启动位置,从而丢失表示的产品字符串

什么都行


--------***回答***---------->-->->--

我没有浏览这些方法,也没有没完没了地浏览表单来查找bug修复或解释,而是使用以下方法:

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);
    }
}
这是我在另一篇堆栈溢出文章中找到的方法。它基本上检查片段是否对用户可见,这就是我设置变量的地方。现在我可以去如何每五月时间向前和五月时间向后和正确的变量得到更新

您还将此方法放在每个片段代码中