viewpager前两页相同

时间:2016-10-11 15:48:36

标签: android android-fragments android-viewpager

我有数组,我想传递给嵌套片段,根据当前位置显示正确的数据。但是显示的前两页是相同的,调试似乎表明跳过了页面0,并且数组的pos 1中的数据被加载到前两页中。

在浏览几页并向后滑动到开头后,它会正确显示,但初始设置没有。

这是我的适应器嵌入到外部片段中:

    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {

    public ScreenSlidePagerAdapter(FragmentManager fm) {
        super(fm);

    }

    @Override
    public ScreenSlidePageFragment getItem(int position) {
        Bundle bundle = new Bundle();

        int logo=logos[position];
        String win=wins.get(position);
        String draw=draws.get(position);
        String loss=losses.get(position);
        String team=teams.get(position);
        String data=output.toString();

        Log.e("check pos outer", "" + position+" "+teams.get(position));

        return ScreenSlidePageFragment.newInstance(logo,win,draw,loss,team,data);
    }

    @Override
    public int getCount() {
        return NUM_PAGES;
    }
}

这是我的嵌套片段:

public class ScreenSlidePageFragment extends Fragment {
private static JSONArray data;
private TableLayout tableLayout;
private LayoutInflater inflate;
private static String wins;
private static String draws;
private static String losses;
private static String teams;
private static int logo;

public static ScreenSlidePageFragment newInstance(int logos,String win,String draw,String loss,String team,String output) {
    ScreenSlidePageFragment fragmentFirst = new ScreenSlidePageFragment();
    Bundle bundle = new Bundle();
    bundle.putInt("logo", logos);
    bundle.putString("wins", win);
    bundle.putString("draws", draw);
    bundle.putString("losses", loss);
    bundle.putString("teams",team);
    bundle.putString("data", output);
    fragmentFirst.setArguments(bundle);
    return fragmentFirst;
}


public ScreenSlidePageFragment() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    logo = getArguments().getInt("logo");
    wins= getArguments().getString("wins");
    draws= getArguments().getString("draws");
    losses= getArguments().getString("losses");
    teams= getArguments().getString("teams");
    String stringData= getArguments().getString("data");
    try {
        data=new JSONArray(stringData);
    } catch (JSONException e) {
        e.printStackTrace();
    }

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_screen_slide_page, container, false);
    tableLayout=(TableLayout)rootView.findViewById(R.id.squads_tables);
    inflate=inflater;
    fillTable();

    return rootView;
}

private void fillTable() {
    if (teams.length() > 0) {

        View tr = inflate.inflate(R.layout.squadrow, null,false);
        ImageView teamLogo=(ImageView)tr.findViewById(R.id.imageView);
        Drawable logoDraw= ContextCompat.getDrawable(getActivity(), logo);
        teamLogo.setImageDrawable(logoDraw);
        TextView teamname=(TextView)tr.findViewById(R.id.name);
        teamname.setText(teams);
        TextView record=(TextView)tr.findViewById(R.id.textView3);
        record.setText("wins: " + wins + "  draws: " + draws + "  lost: " + losses);
        tableLayout.addView(tr);

        try {
            int currPos=0;
            boolean passed=false;
            playerloop:
            for(int j=0;j<data.length();j++,currPos++){
                JSONObject jsonobject = data.getJSONObject(j);
                String currTeamName=jsonobject.getString("name");
                String playerName=jsonobject.getString("Name");
                String position=jsonobject.getString("position");
                int age=jsonobject.optInt("Age", 0);
                int played=jsonobject.optInt("played", 0);
                int goals=jsonobject.optInt("goals", 0);
                if(!currTeamName.equals(teams) && passed){

                    break playerloop;
                }
                else if(currTeamName.equals(teams) && !playerName.equals("null")){
                    if(!passed){
                        passed=true;
                    }



                    View tr_player = inflate.inflate(R.layout.player_row, null,false);
                    TextView currPlayer=(TextView)tr_player.findViewById(R.id.name_txt);
                    currPlayer.setText(playerName);

                    TextView currPosiotion=(TextView)tr_player.findViewById(R.id.textView2);
                    currPosiotion.setText(position);

                    TextView currAge=(TextView)tr_player.findViewById(R.id.agetxt);
                    currAge.setText("Age: "+age);

                    TextView currMatches=(TextView)tr_player.findViewById(R.id.matchesTxt);
                    currMatches.setText("Matches: "+played);

                    TextView currGoals=(TextView)tr_player.findViewById(R.id.textView4);
                    currGoals.setText("Goals: "+goals);

                    tableLayout.addView(tr_player);
                }

            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }




}

}

我尝试了不同的方法,例如返回新实例和传递数据包,但似乎没有解决它。

1 个答案:

答案 0 :(得分:0)

private static JSONArray data;
private TableLayout tableLayout;
private LayoutInflater inflate;
private static String wins;
private static String draws;
private static String losses;
private static String teams;
private static int logo;

片段类的所有成员都是静态的。这可能不是你想要的,因为这意味着它们是为所有片段实例共享的。

在初始化时,ViewPager将创建当前页面和下一页面。由于成员是共享的,因此可能会导致两个页面都使用相同的数据。

所以:只需删除'static'关键字即可。我不明白为什么你需要这个。