0

我有一个带有扩展 FragmentStatePagerAdapter 的适配器的 viewpager,这必须显示一些片段,每个页面总是重复相同的布局(其中有一个 listView)......现在的问题是如何更新当前的 listView通过单击片段外部的按钮可见?我只能刷新那些在不可见页面中的页面,但不能刷新那些显示或相邻的页面...一种可能的解决方案是重置适配器,但它似乎不是正确的,因为它是慢...你能帮帮我吗?

  public class MainActivity extends FragmentActivity implements OnClickListener{

    @Override
    protected void onCreate(Bundle arg0) {
            // TODO Auto-generated method stub
            super.onCreate(arg0);
            setContentView(R.layout.layout1);
             pagerAdapter = new AdapterPager(getSupportFragmentManager());
            ctx=this;
          mViewPager = (ViewPager) findViewById(R.id.pager);
            mViewPager.setAdapter(pagerAdapter);
            pagerTab=(PagerTabStrip) findViewById(R.id.pager_tab_strip);
           mViewPager.setCurrentItem(paginaCentrale);
            modifica=(ImageView)findViewById(R.id.modifica);
         modifica.setOnClickListener(this);


    }

    @Override
    public void onClick(View arg0) {
            // TODO onClick
            switch (arg0.getId()){
            case R.id.modifica:

                 //here I would like to call the method caricaLista() of the fragment

            break;
            }
    }


    public static class AdapterPager extends FragmentStatePagerAdapter {
   public AdapterPager(FragmentManager fm) {
            super(fm);
        }

        @Override
            public int getItemPosition(Object object) {
                    // TODO Auto-generated method stub

                    return super.getItemPosition(object);
            }


            @Override
        public Fragment getItem(int i) {
            Fragment fragment;
            Bundle args = new Bundle();
          args.putInt("position", i);
            fragment=new Fragment1();
          fragment.setArguments(args);
     return fragment;
        }

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

        @Override
        public CharSequence getPageTitle(int i) {
            String title=String.valueOf(i);

            return title;
        }


    }

 public static class Fragment1 extends Fragment implements OnItemClickListener{

int fragmentPosition;
ListView lv;
      List<Lista> lista= new ArrayList<Lista>();

      public Fragment1(){    
          }
          public static Fragment1 newInstance(){
            return new Fragment1();
          }

  @Override
            public void onResume() {
                    // TODO Auto-generated method stub
                    super.onResume();
                    caricaLista("")}
            }
    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle
savedInstanceState)    {
            View rootView = inflater.inflate(R.layout.fragment1, container, false);

lv=(ListView) rootView.findViewById(R.id.listViewCompiti);

            Bundle args = getArguments(); 
            int position=  args.getInt("position");

            fragmentPosition=position;
            lv.setOnItemClickListener(this);
     homeListAdapter= new HomeListAdapterWithCache(ctx,R.layout.list_item_home,lista);
                    lv.setAdapter(homeListAdapter);



    return rootView;
}
4

1 回答 1

0

您必须将 Current Fragment 存储到 MainActivity 中,并在每次页面更改时更改。所以你调用当前片段的方法。

查看您的 MainActivity 应该是什么样子:

public class MainActivity extends FragmentActivity implements OnClickListener{

    &Fragment mCurrentFragment;*

    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.layout1);
        pagerAdapter = new AdapterPager(getSupportFragmentManager());
        ctx=this;
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(pagerAdapter);
        pagerTab=(PagerTabStrip) findViewById(R.id.pager_tab_strip);
        mViewPager.setCurrentItem(paginaCentrale);
        modifica=(ImageView)findViewById(R.id.modifica);
        modifica.setOnClickListener(this);

        *mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
            mCurrentFragment = pagerAdapter.getItem(position);
            }*

    });


@Override
    public void onClick(View arg0) {
            // TODO onClick
        switch (arg0.getId()){
        case R.id.modifica:
            *mCurrentFragment.caricaLista();*
        break;
        }
}

}
于 2013-07-18T17:43:43.750 回答