在onCreate中添加/删除方法但在onPageSelected中没有

时间:2016-12-09 05:40:22

标签: android

我有两个viewpager我正在从第二个寻呼机中删除一个viewpager的当前页面。滑动到下一个位置时再次添加删除的页面。第二个viewpager的过程相同。问题是添加和删除方法工作正常,如果我在onCreate中使用它们但不在onPageSelected中工作。和其他问题是,如果我再次在同一位置添加任何删除的页面,它会显示我在viewpager的当前位置添加页面。我希望页面只是添加在该特定位置,但不设置当前位置。 以下是我的代码。任何帮助将不胜感激。

    public class MainActivity extends FragmentActivity {
    CustomViewPager pager1,pager2;
    List<Fragment> fragments1,fragments2;
    public PagerAdapter adapter1;
    public PagerAdapter1 adapter2;
     int lostPositionRight;
     int lostPositionLeft;

   ArrayList<String> image = new ArrayList<String>() {{
         add("0");
         add("1");
         add("2");
         add("3");
         add("4");
     }};

     ArrayList<String> imageNew = new ArrayList<String>() {{
         add("0");
         add("1");
         add("2");
         add("3");
         add("4");
     }};


     ArrayList<Integer> images = new ArrayList<Integer>() {{
        add(R.drawable.first);
        add(R.drawable.second);
        add(R.drawable.three);
        add(R.drawable.four);
        add(R.drawable.xiaomi);
    }};

     ArrayList<Integer> imagesNew = new ArrayList<Integer>() {{
         add(R.drawable.first);
         add(R.drawable.second);
         add(R.drawable.three);
         add(R.drawable.four);
         add(R.drawable.xiaomi);
     }};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.text);

        pager1 = (CustomViewPager) findViewById(R.id.pager1);
        fragments1 = getFragments1();
        adapter1 = new PagerAdapter(getSupportFragmentManager(), fragments1);
        pager1.setAdapter(adapter1);
        removeView1(1);
        lostPositionLeft = 1;
        adapter1.notifyDataSetChanged();

        pager2 = (CustomViewPager) findViewById(R.id.pager2);
        fragments2 = getFragments2();
        adapter2 = new PagerAdapter1(getSupportFragmentManager(), fragments2);
        pager2.setAdapter(adapter2);
        pager2.setCurrentItem(1);
        removeView2(0);
        lostPositionRight = 0;

        pager1.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                MyImageSlider fragmntDta = (MyImageSlider)findFragmentByPosition(pager1.getCurrentItem());
                String value = fragmntDta.getTextOnTextView();
                int p = Integer.parseInt(value);
                Toast toast = Toast.makeText(getApplicationContext(),"Text is " +p, Toast.LENGTH_SHORT);
                toast.show();
                addView2(lostPositionRight,lostPositionRight);
                removeView2(p);

               adapter2.notifyDataSetChanged();
               lostPositionRight = p;
            }
           @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        pager2.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }
            @Override
            public void onPageSelected(int position) {
               MyImageSlider1 fragmntDta = (MyImageSlider1)findFragmentByPosition2(pager2.getCurrentItem());
                String value1 = fragmntDta.getTextView();
                Toast toast = Toast.makeText(getApplicationContext(),"Text is " +value1, Toast.LENGTH_SHORT);
                toast.show();
               int p = Integer.parseInt(value1);
                addView1(lostPositionLeft,lostPositionLeft);
                removeView1(p);
                lostPositionLeft = p;

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

     public void addView1(int index,int position)
     {
         adapter1.addView1 (index,position);
         adapter1.notifyDataSetChanged();
     }

     public void addView2(int index,int position)
     {
         adapter2.addView2 (index,position);
        adapter2.notifyDataSetChanged();
     }

     public void removeView1 (int position)
     {
         adapter1.removeView1(pager1, position);
         adapter1.notifyDataSetChanged();
     }

     public void removeView2 (int position)
     {
         adapter2.removeView2(pager2, position);
         adapter2.notifyDataSetChanged();
     }



     public class PagerAdapter extends FragmentStatePagerAdapter {
        private List<Fragment> fragments;
        public PagerAdapter(FragmentManager fm, List<Fragment> fragments) {
            super(fm);
            this.fragments = fragments;
        }

         @Override
        public Fragment getItem(int position) {
            return this.fragments.get(position);
        }
        @Override
        public int getCount() {
            return this.fragments.size();
        }

      @Override
      public int getItemPosition (Object object)
      {

          return POSITION_NONE;
      }


         public int removeView1 (ViewPager pager, int position)
         {
             pager.setAdapter (null);
             fragments.remove (position);
             pager.setAdapter (this);
             notifyDataSetChanged();
             return position;
         }

             public void addView1 (int index,int position)
             {
                 fragments.add(index,MyImageSlider.newInstance(images.get(position),image.get(position)));
                 notifyDataSetChanged();
             }

     }

    private List<Fragment> getFragments1() {
        List<Fragment> fList = new ArrayList<>();
        for (int i = 0; i < images.size(); i++) {
            fList.add(MyImageSlider.newInstance(images.get(i),image.get(i)));
        }
        return fList;
    }

     public static class MyImageSlider extends Fragment {
         int imageid;
         String newid;
         TextView textView;

         public static MyImageSlider newInstance(int id,String s)
         {
             MyImageSlider slider=new MyImageSlider();
             Bundle b=new Bundle();
             b.putInt("imageid", id);
             b.putString("newid", s);
             slider.setArguments(b);
             return slider;
         }
         public String getTextOnTextView(){
             return (String) textView.getText();
         }


         @Override
         public void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             imageid=getArguments().getInt("imageid");
             newid = getArguments().getString("newid");
         }

         @Override
         public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
             View view = inflater.inflate(R.layout.leftpager,container, false);
             ImageView iv=(ImageView)view.findViewById(R.id.image1);
             textView = (TextView)view.findViewById(R.id.text);
             iv.setImageResource(imageid);
             textView.setText(newid);
             return view;
         }
     }

     public Fragment findFragmentByPosition(int position) {
         PagerAdapter fragmentPagerAdapter = adapter1;
         return getSupportFragmentManager().findFragmentByTag("android:switcher:" + pager1.getId() + ":" +
                         fragmentPagerAdapter.getItemId(position));
     }


     public class PagerAdapter1 extends FragmentStatePagerAdapter {
        private List<Fragment> fragment;

        public PagerAdapter1(FragmentManager fm, List<Fragment> fragments) {
            super(fm);
            this.fragment = fragments;
        }

        @Override
        public Fragment getItem(int position) {
            return this.fragment.get(position);
        }

       @Override
        public int getCount() {
            return this.fragment.size();
        }

      @Override
      public int getItemPosition (Object object)
      {
          return POSITION_NONE;
      }

        public int removeView2 (ViewPager pager, int position)
        {
            pager.setAdapter (null);
            fragment.remove (position);
            pager.setAdapter (this);
            notifyDataSetChanged();
            return position;
        }

         public void addView2 (int index,int position)
         {
             fragment.add(index,MyImageSlider1.newInstance(imagesNew.get(position),imageNew.get(position)));
             notifyDataSetChanged();

         }
    }

    private List<Fragment> getFragments2() {
        List<Fragment> fList = new ArrayList<Fragment>();
        for (int i = 0; i < imagesNew.size(); i++) {
            fList.add(MyImageSlider1.newInstance(imagesNew.get(i),imageNew.get(i)));

        }
        return fList;
    }

     public static class MyImageSlider1 extends Fragment {
        int imageid;
         String newid;
       public TextView textView1;

        public static MyImageSlider1 newInstance(int id,String s)
        {
            MyImageSlider1 slider=new MyImageSlider1();
            Bundle b=new Bundle();
            b.putInt("imageid", id);
            b.putString("newid", s);
            slider.setArguments(b);
            return slider;
        }

         public String getTextView(){
             return (String) textView1.getText();
         }
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            imageid=getArguments().getInt("imageid");
            newid = getArguments().getString("newid");

        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.rightpager, container, false);
            ImageView iv=(ImageView)view.findViewById(R.id.image2);
            iv.setImageResource(imageid);
            textView1 = (TextView)view.findViewById(R.id.text2);
            textView1.setText(newid);

            return view;
        }

    }
     public Fragment findFragmentByPosition2(int position) {
         PagerAdapter1 fragmentPagerAdapter2 = adapter2;
         return getSupportFragmentManager().findFragmentByTag
                 ("android:switcher:" + pager2.getId() + ":" + fragmentPagerAdapter2.getItemId(position));
     }
}

1 个答案:

答案 0 :(得分:1)

首先使用FragmentStatePagerAdapter代替FragmentPagerAdapter

但是,出于你想要的目的,你走错了路。

尝试这样做:

  1. 你说两个视图寻呼机都显示相同的内容。所以没有必要有两个相同的列表。每个人就足够了。

    ArrayList<String> imageNew = new ArrayList<String>() {{
     add("0");
     add("1");
     add("2");
     add("3");
     add("4");
     }};
    
    
    ArrayList<Integer> images = new ArrayList<Integer>() {{
    add(R.drawable.first);
    add(R.drawable.second);
    add(R.drawable.three);
    add(R.drawable.four);
    add(R.drawable.xiaomi);
    }};
    
  2. 为您的视图寻呼机定义onPageChangedListener。因此,每次页面更改时,您都有index。只需从列表中删除一个克隆并删除所需的索引并创建一个新的适配器并将其设置为另一个视图寻呼机。