getItem()时调用FragmentPagerAdapter多个时间时调、多个、时间、getItem

2023-09-07 08:50:23 作者:伤痕累累的疯子

我使用viewpager与fragment.In我的应用程序开发应用我有一个项目,我在List.After显示,当我点击我已经打电话SherlockFragmentActivity它与adapter.Now问题致电ViewPager是在的getItem给FragmentPagerAdapter多个位置在当我不停地翻转backword随后还giveing​​单time.Also错position.Here是我的code

I am developing application using viewpager with fragment.In my application i have a items which i showing in List.After that when i click i have calling SherlockFragmentActivity which calling ViewPager with adapter.Now the problem is getItem in FragmentPagerAdapter give multiple position at a single time.Also when i flip backword then also giveing wrong position.Here is my code

ViewPager code: -

ViewPager Code:-

private void initialisePaging() {
            //Adapter Context;
    this.mPagerAdapter = new MyPagerAdapter(
            super.getSupportFragmentManager(), getApplicationContext(),
            title, link, description);


    ViewPager pager = (ViewPager) super.findViewById(R.id.viewpager);
    pager.setAdapter(this.mPagerAdapter);
           //Take position from ListView & set postion .
    pager.setCurrentItem(post_position);
}

我的适配器类,它负责组片段: -

My Adapter Class which responsible for set fragment:-

问题: - 问题是调用的getItem 3次&安培;假设我点击4项的给我数3,4,5

Problem:- The problem is getItem Calling 3 time & suppose i click on 4 item its giving me 3,4,5 number.

public MyPagerAdapter(FragmentManager fm, Context cont,
            ArrayList<String> title, ArrayList<String> link,
            ArrayList<String> description) {

             super(fm);
    this.context = cont;
    this.key_desc = description;
    this.key_link = link;
    this.key_title = title;
    viewPagerApplication = (RssItem) cont;

}

@Override
public Fragment getItem(int position) {
        return Fragment0.newInstance(position, this.context, key_title,
            key_link, key_desc);
}

@Override
public int getCount() {
     return key_title.size();
}
My Fragment Class:-

Problem:-Here if i show postion in textview its correct position but if i want take position in logcat for further parsing its giving me again 4,3,5 number which is also diffrent from getItem();

public static Fragment0 newInstance(int num,Context cont, ArrayList<String> key_title, ArrayList<String> key_link, ArrayList<String> key_desc) {
    context=cont;



    Fragment0 f = new Fragment0();
    Bundle args = new Bundle();
    args.putInt("num", num);
    f.setArguments(args);

    return f;
}


 public void onCreate(Bundle savedInstanceState) {

   super.onCreate(savedInstanceState);






 };

   public int getShownIndex() {
       return getArguments().getInt("num", 0);
   }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
         View v = inflater.inflate(R.layout.newsdetailfragment_screen, container, false);
          tv = v.findViewById(R.id.headingtextview_id);
         ((TextView)tv).setText(String.valueOf(getShownIndex()));
          ((TextView)tv).setTextColor(Color.BLACK);
         ((TextView)tv).setTextSize(20);
           return v;
    }

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}

现在请让我知道我错了,&安培;我怎样才能得到只有一个进一步的处理确切位置。

Now kindly let me know where i am wrong & how can i get only one exact position for further process.

推荐答案

里面的getItem()的,位置参数是需要渲染的位置。这并不是说用户将看到当前焦点项的位置。到当前显示的视图的左侧和右侧的页需要在存储器呈现使得动画的那些屏幕将是平滑的$ P $页。要在当前位置使用获得该项目:

Inside of getItem(), the position parameter is the position that is in need of rendering. It is NOT the position of the currently focused item that the user would see. The pages to the left and right of the currently displayed view need to be pre rendered in memory so that the animations to those screens will be smooth. To get the item at current position use:

pager.getCurrentItem();

如果你需要可以实现onPageChangeListener页面的实际位置

If you need an actual position of the page you can implement onPageChangeListener

pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
}

在使用onPageSelected位置会给页面的准确的当前位置。

position in onPageSelected will give an accurate current position of the page.