更改页面时如何刷新视图?

时间:2013-12-24 18:14:17

标签: android android-listview android-fragments android-viewpager android-custom-view

我有一个片段活动有4个页面并从4个链接加载来自互联网 当程序启动自定义列表显示列表但当我更改页面时,视图显示白页

我的片段:

public class VpiAbsTestActivitynouser extends SherlockFragmentActivity {


private static final String[] CONTENT = new String[] {" page1","page2","page3","page4"};

TestFragmentAdapter mAdapter;
 ViewPager mPager;
    PageIndicator mIndicator;
    TextView mSearchView;


    public String[] xmlURLArray = new String[]{"link1.xml","link2.xml","link3.xml","link4.xml"};


protected void onCreate(Bundle savedInstanceState) {
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.simple_tabs);

    Intent i = new Intent(this, transparentone.class);
    startActivity(i);

    mAdapter = new TestFragmentAdapter(getSupportFragmentManager());
    mPager = (ViewPager)findViewById(R.id.pager);
    mPager.setAdapter(mAdapter);
    mIndicator = (TabPageIndicator)findViewById(R.id.indicator);
    mIndicator.setViewPager(mPager);
    mIndicator.setCurrentItem(1);


}


class TestFragmentAdapter extends FragmentPagerAdapter {        
    private int mCount = CONTENT.length;

    public TestFragmentAdapter(FragmentManager fm) {
        super(fm);

    }

    @Override
    public Fragment getItem(int position) {
            Bundle args = new Bundle();
            args.putString("url", xmlURLArray[position]);

        return  customlist.newInstance(args);

    }



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


    public CharSequence getPageTitle(int position) {
        return VpiAbsTestActivitynouser.CONTENT[position % VpiAbsTestActivitynouser.CONTENT.length].toUpperCase();
    }

     @Override
        public void destroyItem(View collection, int position, Object view) {
             ((ViewPager) collection).removeView((View) view);

        }



 }

}

我的自定义列表代码是:

public class customlist extends SherlockFragment  {
int fragment_position_in_viewpager = 0;

// All static variables

 public static customlist newInstance(Bundle args) {
        customlist fragment = new customlist();
        fragment.setArguments(args);
        return fragment;
        }

ListView list;
LazyAdapterbeth adapter1;



public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);


        }


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (getArguments() != null) { 
        URL = getArguments().getString("url"); 
        } 
        new getFeed().execute(); 
} 

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     ViewGroup root = (ViewGroup) inflater.inflate(
                R.layout.dovomi, container, false);



        return root;
    }

public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    if (getArguments() != null) {
        fragment_position_in_viewpager = getArguments().getInt("position");
        //Update the Fragment on ViewPager Position.
    }


}


private class getFeed extends AsyncTask<Void, Void, Document> {

       @Override
       protected void onPreExecute() { 
       }

    protected Document doInBackground(Void... params) {
    }

    protected void onPostExecute(Document doc) {
    }



    list=(ListView)getView().findViewById(R.id.list);
    adapter1=new LazyAdapterbeth(getActivity(), songsList);
    adapter1.notifyDataSetChanged();
    list.setAdapter(adapter1);

将视图从第1页更改为第2页时,如何刷新视图?

1 个答案:

答案 0 :(得分:0)

由于您的代码很长,我没有深入分析,但我想到了以下解决方案:

1)在重新创建片段/重新创建视图层次结构时,会定期调用每个片段方法的onCreateView

因此,它可以是一个令人耳目一新的地方。但是,onCreateView的调用时间略早于分页显示之前(通常在显示页面nr 2的位置,执行第3页的onCreateView)。最好的想法是在onCreateView中放置一个显示Toast的短代码,然后你会知道它是否适合你。

2)你的主要活动(扩展FragmentActivity)可以获得3个在片段之间滑动时执行的方法。它将以下列方式看起来或多或少:

public MyActivity extends FragmentActivity implements OnPageChangeListener {

@Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
         mainActivityPageAdapter = new MainActivityPageAdapter (this, getSupportFragmentManager());
        mainActivityViewPager = (ViewPager) findViewById(R.id.main_activity_view_pager);
        mainActivityViewPager.setAdapter(mainActivityPageAdapter);

        //set listener (for navigator)
        mainActivityViewPager.setOnPageChangeListener(this);

}

//and now you have 3 methods
    @Override
    public void onPageScrollStateChanged(int state) {
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
}       

您可以阅读更多http://developer.android.com/reference/android/support/v4/view/ViewPager.OnPageChangeListener.html