为了账号安全,请及时绑定邮箱和手机立即绑定

viewpager与FragmentPagerAdapter重复加载问题?

viewpager与FragmentPagerAdapter重复加载问题?

慕数据2535060 2016-10-24 14:24:28
当从tab1滑动到tab3,然后返回tab1,发现tab1的数据多加载了一遍,然后每次这样都会多加载一遍打log打了一天不知道问题出在哪里public class MainActivity2 extends AppCompatActivity {     private TabLayout tabLayout;     private ViewPager viewPager;     String[]tabText={"tab1","tab2","tab3","tab4","tab5"};     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main_activity);         tabLayout = (TabLayout) findViewById(R.id.tablayout);         viewPager = (ViewPager) findViewById(R.id.Viewpager);         for (int i=0;i<tabText.length;i++) {             tabLayout.addTab(tabLayout.newTab().setText(tabText[i]));         }         MyAdpter2 myAdpter2 = new MyAdpter2(getSupportFragmentManager());         viewPager.setAdapter(myAdpter2);         //添加TabLayout变化监听         viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));         //添加点击监听         tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {             //标签选中之后执行的方法             @Override             public void onTabSelected(TabLayout.Tab tab) {                 viewPager.setCurrentItem(tab.getPosition());             }             //标签没选中             @Override             public void onTabUnselected(TabLayout.Tab tab) {             }             @Override             public void onTabReselected(TabLayout.Tab tab) {             }         });     }     class MyAdpter2 extends FragmentPagerAdapter{         public MyAdpter2(FragmentManager fm) {             super(fm);         }         @Override         public Fragment getItem(int position) {             switch (position)             {                 case 0:                     return new Fragment1();                 case 1:                     return new Fragment2();                 case 2:                     return new Fragment3();                 case 3:                     return new Fragment4();                 case 4:                     return new Fragment5();             }             return null;         }         @Override         public int getCount() {             return tabText.length;         }     } } //碎片1的代码 public class Fragment3 extends Fragment implements AdapterView.OnItemClickListener{     private List<Character> list = new ArrayList<Character>();     private int[] image1 = {R.drawable.icon_1tools,R.drawable.icon_2fire,R.drawable.icon_3survival,             R.drawable.icon_4food,R.drawable.icon_5science,R.drawable.icon_6fight,R.drawable.icon_7build,             R.drawable.icon_8refine,R.drawable.icon_9magic,R.drawable.icon_10dress,R.drawable.icon_11ancient_altar};     private String[] name = {"1", "2", "3", "4", "5", "6", "7", "8",             "9             ", "10", "11"};     @Nullable     @Override     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {         View view = inflater.inflate(R.layout.gvmain_layout,container,false);         addList();         GridView gridView = (GridView) view.findViewById(R.id.main_gridlayout);         MyAdapter adapter = new MyAdapter(getActivity(),R.layout.item,list);         gridView.setAdapter(adapter);         gridView.setOnItemClickListener(this);         return view;     }     private void addList() {         for (int i = 0; i < image1.length; i++) {             list.add(new Character(name[i], image1[i]));         }     }     @Override     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {     }     public class MyAdapter extends ArrayAdapter<Character> {         public int resourceId;         public MyAdapter(Context context, int resource, List<Character> objects) {             super(context, resource, objects);             resourceId =resource;         }         @Override         public View getView(int position, View convertView, ViewGroup parent) {             Character character = getItem(position);//获得实例             View view;             ViewHoleder viewHoleder;             if (convertView == null){                 view = LayoutInflater.from(getContext()).inflate(resourceId,null);                 viewHoleder = new ViewHoleder();                 viewHoleder.fruitImage = (ImageView) view.findViewById(R.id.image);                 viewHoleder.fruitName = (TextView) view.findViewById(R.id.text);                 view.setTag(viewHoleder);             }else {                 view = convertView;                 viewHoleder = (ViewHoleder) view.getTag();             }             viewHoleder.fruitImage.setImageResource(character.getImageId());             viewHoleder.fruitName.setText(character.getName());             return view;         }         class ViewHoleder{             ImageView fruitImage;             TextView fruitName;         }     } }
查看完整描述

1 回答

?
慕数据2535060

TA贡献1条经验 获得超1个赞

找到原因了,因为每次加载的时候addList()中的i没有存储,但是list的数据却存储下来了,i变成0,后又重新添加了数据一次

查看完整回答
1 反对 回复 2016-10-24
  • 1 回答
  • 0 关注
  • 3472 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信