Android viewpager前两页相同
我有一些数组,我想将它们传递给一个嵌套片段,该片段将根据当前位置显示正确的数据。但是,显示的前两页是相同的,调试结果似乎显示跳过了第0页,并将来自阵列位置1的数据加载到前两页中 在浏览了几页并滑回起始位置后,屏幕显示正确,但初始设置不正确 这是嵌入外部碎片的适配器:Android viewpager前两页相同,android,android-fragments,android-viewpager,Android,Android Fragments,Android Viewpager,我有一些数组,我想将它们传递给一个嵌套片段,该片段将根据当前位置显示正确的数据。但是,显示的前两页是相同的,调试结果似乎显示跳过了第0页,并将来自阵列位置1的数据加载到前两页中 在浏览了几页并滑回起始位置后,屏幕显示正确,但初始设置不正确 这是嵌入外部碎片的适配器: private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter { public ScreenSlidePagerAdapter(Fr
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();
}
}
}
这是我的嵌套片段:
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();
}
}
}
公共类ScreenSlidePageFragment扩展片段{
私有静态JSONArray数据;
私人桌面布局;
私人住宅价格上涨;
私有静态字符串获胜;
私有静态字符串绘制;
私有静态串损耗;
私人静态字符串组;
私人静态int标志;
公共静态屏幕SlidePageFragment newInstance(整数徽标、字符串赢、字符串抽、字符串输、字符串团队、字符串输出){
ScreenSlidePageFragmentFirst=新建ScreenSlidePageFragment();
Bundle=新Bundle();
bundle.putInt(“logo”,logo);
bundle.putString(“赢”,赢);
bundle.putString(“draws”,draw);
bundle.putString(“损失”,损失);
bundle.putString(“团队”,团队);
bundle.putString(“数据”,输出);
fragmentFirst.setArguments(bundle);
首先返回碎片;
}
公共屏幕SlidePageFragment(){
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
logo=getArguments().getInt(“logo”);
wins=getArguments().getString(“wins”);
draws=getArguments().getString(“draws”);
损失=getArguments().getString(“损失”);
teams=getArguments().getString(“团队”);
String stringData=getArguments().getString(“数据”);
试一试{
数据=新的JSONArray(stringData);
}捕获(JSONException e){
e、 printStackTrace();
}
}
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
视图组根视图=(视图组)充气器。充气(R.layout.fragment\u screen\u slide\u页面,容器,false);
tableLayout=(tableLayout)rootView.findviewbyd(R.id.squads\u tables);
充气=充气机;
fillTable();
返回rootView;
}
私有void fillTable(){
如果(teams.length()>0){
视图tr=充气。充气(R.layout.squadrow,null,false);
ImageView团队徽标=(ImageView)tr.findViewById(R.id.ImageView);
Drawable logoDraw=ContextCompat.getDrawable(getActivity(),logo);
teamLogo.setImageDrawable(logoDraw);
TextView团队名称=(TextView)tr.findviewbyd(R.id.name);
teamname.setText(团队);
TextView记录=(TextView)tr.findViewById(R.id.textView3);
record.setText(“赢:+赢+”抽:+抽+”输:+输);
tableLayout.addView(tr);
试一试{
int currPos=0;
布尔传递=假;
playerloop:
对于(int j=0;j
你的片段类的所有成员都是静态的。这可能不是你想要的,因为这意味着它们对于所有片段实例都是共享的
初始化时,ViewPager将创建当前页和下一页。由于成员是共享的,这可能导致两页使用相同的数据
所以:只需删除“static”关键字。我不明白你为什么需要这个
你的片段类的所有成员都是静态的。这可能不是你想要的,因为这意味着它们对于所有片段实例都是共享的
初始化时,ViewPager将创建当前页和下一页。由于成员是共享的,这可能导致两页使用相同的数据
所以:只需删除“static”关键字。我不明白你为什么需要这个