在ViewPager中替换片段显示空白屏幕?

时间:2014-01-31 03:05:01

标签: android android-fragments android-viewpager android-fragmentactivity

我在使用Tabs / Swiping处理碎片方面遇到了很多麻烦。我有一个活动和两个碎片,一个有ListView。我想要的是当我使用ListView从片段1滑动到片段时,刷新ListView或者如果可能的话重新创建第二个片段....

我现在正在尝试的是像这样使用FragmentTransaction.replace():

public void onTabSelected(Tab arg0, android.app.FragmentTransaction arg1) {
    // TODO Auto-generated method stub

    Fragment frag = new FragmentTwo();

    FragmentTransaction trans = getSupportFragmentManager().beginTransaction();

    if (arg0.getPosition() == 1) {
        trans.replace(R.id.pager, frag);
        trans.addToBackStack(null);
        trans.commit();
    }   
}

所以我认为当我滑动到选项卡2(也就是ListView的选项卡)时,它会用它创建的新选项替换它。实际发生的是当我滑动到选项卡2时,它使用ListView重新创建片段(我知道因为我在onCreateView中有一个对话框显示在屏幕上),但片段/选项卡是空白/黑色。我究竟做错了什么?这个片段在哪里创建?

我不知道这些片段的代码是否相关,但如果我发布它,请告诉我,我可以这样做。这是我的PagerAdapter类。

public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int index) {

    switch (index) {
    case 0:
        return new FragmentOne();
    case 1:
        return new FragmentTwo();
    case 2:
        return new FragmentThree();
    }
    return null;
}

@Override
public int getCount() {
    // get item count - equal to number of tabs
    return 3;
}
}

感谢您的帮助!我真的把头发拉过这一头:(

编辑:

当您从选项卡1滑动到选项卡2时,选项卡2显示我的对话框,然后加载选项卡,但如果我返回选项卡1,它全部为黑色......如果我再次转到选项卡2,则会创建再次显示我的对话,然后变黑......

编辑:(添加listview片段代码)

公共类FragmentTwo扩展了Fragment {

// Progress Dialog
private ProgressDialog pDialog;

// Creating JSON Parser object
JSONParser_Helpers2 jParser = new JSONParser_Helpers2();

ArrayList<HashMap<String, String>> tripList;

// url to get all products list
private static String url_all_trips = "http://10.0.2.2/android_connect/get_all_trips.php";

// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_TRIPS = "trips";
private static final String TAG_TRIPID = "tripid";
private static final String TAG_TRIPNAME = "tripName";
private static final String TAG_UID = "uid32";

// products JSONArray
JSONArray trips = null;

View rootView;
Button btnRefresh;


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

    rootView = inflater.inflate(R.layout.fragment_two, container, false);
    btnRefresh = (Button) rootView.findViewById(R.id.refresh);

    // Hashmap for ListView
    tripList = new ArrayList<HashMap<String, String>>();

    // Loading products in Background Thread
    new LoadAllProducts().execute();

    // Get listview
    ListView lv = (ListView) rootView.findViewById(R.id.list);


    // on seleting single product
    // launching Edit Product Screen
    lv.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //getting values from selected ListItem
        String tripid = ((TextView) view.findViewById(R.id.pid)).getText().toString();

        //starting new intent
        Intent in = new Intent(getActivity(), EditTrip_Activity.class);
        //sending pid to next activity
        in.putExtra(TAG_TRIPID, tripid);

        //starting new activity and expecting some response back
        startActivityForResult(in, 100);
    }

    });
    return rootView;
}

/**
 * Background Async Task to Load all product by making HTTP Request
 * */
class LoadAllProducts extends AsyncTask<String, String, String> {

    ListView lv = (ListView) rootView.findViewById(R.id.list);
    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Loading your trips. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    /**
     * getting All products from url
     * */
    protected String doInBackground(String... args) {
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        // getting JSON string from URL
        JSONObject json = jParser.makeHttpRequest(url_all_trips, "GET", params);

        // Check your log cat for JSON response
        Log.d("All Trips: ", json.toString());

        try {
            // Checking for SUCCESS TAG
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // products found
                // Getting Array of Products
                trips = json.getJSONArray(TAG_TRIPS);

                // looping through All Products
                for (int i = 0; i < trips.length(); i++) {
                    JSONObject c = trips.getJSONObject(i);

                    // Storing each json item in variable
                    String tripid = c.getString(TAG_TRIPID);
                    String tripname = c.getString(TAG_TRIPNAME);
                    String userId = c.getString("uid");

                    // creating new HashMap
                    DatabaseHandler_Helpers db = new DatabaseHandler_Helpers(getActivity());
                    HashMap<String, String> map = new HashMap<String, String>();

                    if (userId.equals(db.getUserDetails().get("uid"))) {
                        // adding each child node to HashMap key => value
                        map.put(TAG_TRIPID, tripid);
                        map.put(TAG_TRIPNAME, tripname);

                        // adding HashList to ArrayList
                        tripList.add(map);
                    } //else {
                        //map.put(TAG_TRIPID, "");
                        //map.put(TAG_TRIPNAME, "You have no tracked trips.");

                        //tripList.add(map);
                    //}
                }
            } else {
                // no products found
                // Launch Add New product Activity
                Intent i = new Intent(getActivity(),
                        NewTrip_Activity.class);
                // Closing all previous activities
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all products
        pDialog.dismiss();

        // updating UI from Background Thread
        ((Activity) getActivity()).runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */

                ListAdapter adapter = new SimpleAdapter(
                        getActivity(), tripList,
                        R.layout.list_item, new String[] { TAG_TRIPID,
                                TAG_TRIPNAME},
                        new int[] { R.id.pid, R.id.name });
                // updating listview
                ((ListView) lv.findViewById(R.id.list)).setAdapter(adapter);
            }
        });
     }
 }

}

3 个答案:

答案 0 :(得分:12)

尝试扩展FragmentStatePagerAdapter而不是FragmentPagerAdapter。

这是我的片段代码,它对我来说很好用:

//Adapter
public class AllPagesAdapter extends FragmentStatePagerAdapter {



            public AllPagesAdapter(FragmentManager fm) {

                super(fm);

            }

            @Override
            public Fragment getItem(int index) {

        switch (index) {
            case 0:

                return new Android();

            case 1:

                return new CoreJava();
            case 2:

                return new J2EE();

            case 3:

                return new Database();

            case 4:

                return new WebServices();


        }
        return null;


    }

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

}

//For example, this is one fragment:

public class Database extends Fragment {

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

        View databaseview = inflater.inflate(R.layout.database, container, false);

        return databaseview;

    }


}

//My MainActivity

public class AllActivities extends FragmentActivity implements ActionBar.TabListener {

    public ViewPager viewPager;
    private AllPagesAdapter mAdapter;
    private ActionBar actionBar;
    private String [] tabs = {"Android","CoreJava","J2EE","Database","Web Services"};

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

        //Initializing all stuff
        viewPager = (ViewPager)findViewById(R.id.pager);

        actionBar = getActionBar();
        mAdapter = new AllPagesAdapter(getSupportFragmentManager());
        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(true);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);



        //Add the tabs here
        for(String tab_name:tabs){
            actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
        }

        viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){

            @Override
        public void onPageSelected(int position){

                //on Page change, that particular page should be selected
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
                public void onPageScrolled(int arg0,float arg1,int arg2){

            }
            @Override
        public void onPageScrollStateChanged(int position){

            }

        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction fragmentTransaction) {

        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction fragmentTransaction) {



    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction fragmentTransaction) {

        viewPager.setCurrentItem(tab.getPosition());

    }
}

如果您还希望在任何一个片段中实现listview,请告诉我。

希望这会有所帮助.. :)

答案 1 :(得分:2)

我的孩子片段在ViewPager中。我只是用FragmentStatePagerAdapter而不是FragmentPagerAdapter

替换ViewPage适配器

儿童片段 ....

mViewPager.setAdapter(new SearchOriginTabsPagerAdapter(getFragmentManager()));

...适配器

public class SearchOriginTabsPagerAdapter extends FragmentStatePagerAdapter {

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

@Override
public Fragment getItem(int index) {

    switch (index) {
    case 0:
        return new NearbyFragment();
    case 1:
        return new RecentOriginFragment();
    }

    return null;
}

@Override
public int getCount() {
    // get item count - equal to number of tabs
    return 2;
}
}

答案 2 :(得分:0)

好吧..这就是事情..

我并没有真正使用您的代码,而是使用了类似的概念。它将是一个完整的解决方案。

//这是应用的起点。它有很多片段,我使用自己的json来获取数据。

public class AllActivities extends FragmentActivity implements ActionBar.TabListener {

    public ViewPager viewPager;
    private AllPagesAdapter mAdapter;
    private ActionBar actionBar;
    private String [] tabs = {"Android","CoreJava","J2EE","Database","Web Services"};

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

        //Initializing all stuff
        viewPager = (ViewPager)findViewById(R.id.pager);

        actionBar = getActionBar();
        mAdapter = new AllPagesAdapter(getSupportFragmentManager());
        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(true);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);



        //Add the tabs here
        for(String tab_name:tabs){
            actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
        }

        viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){

            @Override
        public void onPageSelected(int position){

                //on Page change, that particular page should be selected
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
                public void onPageScrolled(int arg0,float arg1,int arg2){

            }
            @Override
        public void onPageScrollStateChanged(int position){

            }

        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction fragmentTransaction) {

        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction fragmentTransaction) {



    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction fragmentTransaction) {

        viewPager.setCurrentItem(tab.getPosition());

    }
}

//This is one of the fragment and assume that Expandablelistview is your list view.. In both the cases, i am setting the adapter in the onActivityCreated() method.
public class Android extends android.support.v4.app.Fragment {



    ExpandableListAdapter listAdapter;
    // private ExpandableListView expListView;
    List<String> listDataHeader;
    HashMap<String, List<String>> listDataChild;
    ListView lv1;
    private static final String QUESTION = "question";
    private static final String ANSWERS = "answer";


    // ArrayList<HashMap<String, String>> jsonlist = new ArrayList<HashMap<String, String>>();

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


        View mikeview = inflater.inflate(R.layout.androidlayout, container, false);

        return mikeview;

    }



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


        ExpandableListView expListView = null;
        try{
            expListView = (ExpandableListView) getActivity().findViewById(R.id.androidExpandableList);
            }
        catch (Exception e){

            e.printStackTrace();
        }




        new Thread(){


            @Override
            public void run(){



            }
        }.start();

        try {
            setParent();
            prepareChild();
        } catch (Exception e) {
            e.printStackTrace();
        }




        ExpandableListAdapter listAdapter = new ExpandableListAdapter(getActivity(), listDataHeader, listDataChild);
        expListView.setAdapter(listAdapter);




        // Listview on child click listener
        expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                                        int groupPosition, int childPosition, long id) {
                /*Toast.makeText(
                        getActivity(),
                        listDataHeader.get(groupPosition)
                                + " : "
                                + listDataChild.get(
                                listDataHeader.get(groupPosition)).get(
                                childPosition), Toast.LENGTH_SHORT)
                        .show();*/
                return true;
            }
        });


        // Listview Group expanded listener
        expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

            @Override
            public void onGroupExpand(int groupPosition) {
               /* Toast.makeText(getActivity(),
                        listDataHeader.get(groupPosition) + " Expanded",
                        Toast.LENGTH_SHORT).show();*/
            }
        });

        // Listview Group collasped listener
        expListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {

            @Override
            public void onGroupCollapse(int groupPosition) {
                /*Toast.makeText(getActivity(),
                        listDataHeader.get(groupPosition) + " Collapsed",
                        Toast.LENGTH_SHORT).show();*/

            }
        });


    }


    public void setParent(){

        listDataHeader = new ArrayList<String>();

        try{

            JSONObject json = new JSONObject(loadJSONFromAsset());

            JSONArray array = json.getJSONArray("androidquestion");

            for(int my =0;my<array.length();my++){

                JSONObject c = array.getJSONObject(my);


                String topics = c.getString(QUESTION);
                listDataHeader.add(topics);
            }
        }

        catch (JSONException e) {

            e.printStackTrace();

        }

    }

    public void prepareChild() throws JSONException {

        listDataChild = new HashMap<String, List<String>>();

        try{

            JSONObject json = new JSONObject(loadJSONFromAsset());
            JSONArray array = json.getJSONArray("androidquestion");
            for(int mz = 0;mz<array.length();mz++){

                ArrayList<String> child = new ArrayList<String>();
                JSONObject d = array.getJSONObject(mz);
                String ans = d.getString(ANSWERS);
                child = new ArrayList<String>();

                child.add(ans);
                int position = mz ;

                listDataChild.put(listDataHeader.get( position),child);

            }
        }
        catch(JSONException e) {
            e.printStackTrace();

        }

    }


    public String loadJSONFromAsset() {

        String json = null;

        try {

            InputStream is = getActivity().getAssets().open("android.json");

            int size = is.available();

            byte[] buffer = new byte[size];

            is.read(buffer);

            is.close();

            json = new String(buffer, "UTF-8");

        } catch (IOException ex) {

            ex.printStackTrace();

            return null;
        }
        return json;

    }

}

    //This is the adapter.
    public class AllPagesAdapter extends FragmentStatePagerAdapter {



            public AllPagesAdapter(FragmentManager fm) {

                super(fm);

            }

            @Override
            public Fragment getItem(int index) {

        switch (index) {
            case 0:

                return new Android();

            case 1:

                return new CoreJava();
            case 2:

                return new J2EE();

            case 3:

                return new Database();

            case 4:

                return new WebServices();


        }
        return null;


    }

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

}

//This is the adapter for my expandable list view.. in your case, your can just use the listadapter or any other adapter you want to use. This is just an optional part.
public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Context _context;
    private List<String> _listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap<String, List<String>> _listDataChild;


    public ExpandableListAdapter(Context context, List<String> listDataHeader,
                                 HashMap<String, List<String>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {

        final String childText = (String) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.newlistitems,null);

        }

        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.newlistviewitems);

        txtListChild.setText(childText);
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return this._listDataHeader.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater myInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = myInflater.inflate(R.layout.newlistgroup, null);
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.newlistviewgroup);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}