2

我有一个页面适配器,其中有两个来自 sherlockfragment 的页面,但是当我将页面从一页更改为二页时,页面不会更新并且屏幕是白页。

我的适配器页面代码是:

public class VpiAbsTestActivitynouser extends SherlockFragmentActivity {


private static final String[] CONTENT = new String[] {" 1","2"};

TestFragmentAdapter mAdapter;
 ViewPager mPager;
    PageIndicator mIndicator;

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

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

}



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

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

    @Override
    public Fragment getItem(int position) {
        switch(position) {

        case 0:

            return new customlistnotuser();

        case 1:

            return new anotherpage();


        default:
            return null;

        }
    }

    @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 customlistnotuser extends SherlockFragment  {

// All static variables
static final String URL = "url";
// XML node keys
static final String KEY_TEST = "test"; // parent node
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_Description = "description";
static final String KEY_DURATION = "duration";
static final String KEY_THUMB_URL = "thumb_url";
static final String KEY_PRICE = "price";
static final String KEY_URL = "url";



 private ProgressDialog pDialog;


ListView list;
LazyAdapterbeth adapter;
XMLParser parser = new XMLParser();
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);


}



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

    new getFeed().execute();

}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) 

{

    View thisfragment = inflater.inflate(R.layout.dovomi, container, false);

    return thisfragment;
}



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



       }

    protected Document doInBackground(Void... params) {

        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL); // getting XML from URL
        Document doc = parser.getDomElement(xml); // getting DOM element

        return doc;
    }

    protected void onPostExecute(Document doc) {

    ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

    NodeList nl = doc.getElementsByTagName(KEY_TEST);
    // looping through all song nodes <song>
    for (int i = 0; i < nl.getLength(); i++) {
        // creating new HashMap
        HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) nl.item(i);
        // adding each child node to HashMap key => value
        map.put(KEY_ID, parser.getValue(e, KEY_ID));
        map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
        map.put(KEY_Description, parser.getValue(e, KEY_Description));
        map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
        map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
        map.put(KEY_PRICE, parser.getValue(e, KEY_PRICE));
        map.put(KEY_URL, parser.getValue(e, KEY_URL));
        // adding HashList to ArrayList
        songsList.add(map);
         pDialog.dismiss();
    }



    list=(ListView)getActivity().findViewById(R.id.list);

    // Getting adapter by passing xml data ArrayList
    adapter=new LazyAdapterbeth(getActivity(), songsList);
    list.setAdapter(adapter);


    // Click event for single list row
    list.setOnItemClickListener(new OnItemClickListener() {

当我从第一页转到第二页时,如何更新我的视图?

我如何使用 instantiateItem 和 notifyDataSetChanged();在此代码上更新视图?

4

1 回答 1

0

首先你的 Fragment 需要返回一个 Fragment 的实例,你可以通过添加一个 newInstance(Bundle args); 来做到这一点。然后,您可以使用 FragmentArguments 根据 ViewPager 中的位置更新 Fragment。

public class CustomList extends SherlockListFragment{
int fragment_position_in_viewpager = 0;
....
public static CustomList newInstance(Bundle args) {
customlist fragment = new CustomList();
fragment.setArguments(args);
return fragment;
}
....

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
    fragment_position_in_viewpager = getArguments().getInt("position"); 
} 
String[] params = {xmlURLArray[fragment_position_in_viewpager]}

new getFeed().execute(params);

}

protected Document doInBackground(String... params) {

    XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(params[0]); // getting XML from URL
....



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

    //initialize your widgets here
    //button = (Button) root.findViewById(R.id.button);

    return root;
}

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

final int fragment_position_in_viewpager;
if (getArguments() != null) {
    fragment_position_in_viewpager = getArguments().getInt("position"); 
} 

     //Update the Fragment UI depending on the ViewPager Position.

....

然后在你的 TestFragmentAdapter (FragmentPagerAdaper)

....
@Override
public Fragment getItem(int position) {

         //Return a Fragment
        //from a new Instance with FragmentArguments
            Bundle args = new Bundle();
            args.putInt("position", position);
            return CustomList.newInstance(args);

//use a switch if you have different types Fragments
//switch(position) {
    //case 0:
        //From the Fragment Constructor 
            //    return (mCustomList = new CustomList());
....
于 2013-01-13T11:20:53.173 回答