Android 选项卡式活动通过 volley 添加新片段
Posted
技术标签:
【中文标题】Android 选项卡式活动通过 volley 添加新片段【英文标题】:Android Tabbed Activity Add New Fragment by volley 【发布时间】:2016-08-09 22:49:24 【问题描述】:最初我的想法是使用选项卡式活动创建一个儿童列表。 1-父母有一个或多个孩子。 2- 使用 volley 我想通过使用选项卡式活动的逻辑来管理为每个孩子本身创建一个新片段。
我试过了,你可以在下面看到我的活动,然后你会看到我的适配器:
private ViewPager mViewPager = null;
private ChildAdapter pagerAdapter = null;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_child);
// Set up the ViewPager with the sections adapter.
pagerAdapter = new ChildAdapter();
mViewPager = (ViewPager) findViewById(R.id.view_pager);
mViewPager.setAdapter(pagerAdapter);
LayoutInflater inflater = context.getLayoutInflater();
FrameLayout v0 = (FrameLayout) inflater.inflate(R.layout.fragment_profile, null);
pagerAdapter.addView(v0, 0);
//-----------------------------------------------------------------------------
// Here's what the app should do to add a view to the ViewPager.
public void addView(View newPage)
int pageIndex = pagerAdapter.addView(newPage);
// You might want to make "newPage" the currently displayed page:
mViewPager.setCurrentItem(pageIndex, true);
//-----------------------------------------------------------------------------
// Here's what the app should do to remove a view from the ViewPager.
public void removeView(View defunctPage)
int pageIndex = pagerAdapter.removeView(mViewPager, defunctPage);
// You might want to choose what page to display, if the current page was "defunctPage".
if (pageIndex == pagerAdapter.getCount())
pageIndex--;
mViewPager.setCurrentItem(pageIndex);
//-----------------------------------------------------------------------------
// Here's what the app should do to get the currently displayed page.
public View getCurrentPage()
return pagerAdapter.getView(mViewPager.getCurrentItem());
//-----------------------------------------------------------------------------
// Here's what the app should do to set the currently displayed page. "pageToShow" must
// currently be in the adapter, or this will crash.
public void setCurrentPage(View pageToShow)
mViewPager.setCurrentItem(pagerAdapter.getItemPosition(pageToShow), true);
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
【问题讨论】:
【参考方案1】: Adapter
public class ChildAdapter extends PagerAdapter
// This holds all the currently displayable views, in order from left to right.
private ArrayList<View> views = new ArrayList<View>();
//-----------------------------------------------------------------------------
// Used by ViewPager. "Object" represents the page; tell the ViewPager where the
// page should be displayed, from left-to-right. If the page no longer exists,
// return POSITION_NONE.
@Override
public int getItemPosition (Object object)
int index = views.indexOf (object);
if (index == -1)
return POSITION_NONE;
else
return index;
//-----------------------------------------------------------------------------
// Used by ViewPager. Called when ViewPager needs a page to display; it is our job
// to add the page to the container, which is normally the ViewPager itself. Since
// all our pages are persistent, we simply retrieve it from our "views" ArrayList.
@Override
public Object instantiateItem (ViewGroup container, int position)
View v = views.get (position);
container.addView (v);
return v;
//-----------------------------------------------------------------------------
// Used by ViewPager. Called when ViewPager no longer needs a page to display; it
// is our job to remove the page from the container, which is normally the
// ViewPager itself. Since all our pages are persistent, we do nothing to the
// contents of our "views" ArrayList.
@Override
public void destroyItem (ViewGroup container, int position, Object object)
container.removeView (views.get (position));
//-----------------------------------------------------------------------------
// Used by ViewPager; can be used by app as well.
// Returns the total number of pages that the ViewPage can display. This must
// never be 0.
@Override
public int getCount ()
return views.size();
//-----------------------------------------------------------------------------
// Used by ViewPager.
@Override
public boolean isViewFromObject (View view, Object object)
return view == object;
//-----------------------------------------------------------------------------
// Add "view" to right end of "views".
// Returns the position of the new view.
// The app should call this to add pages; not used by ViewPager.
public int addView (View v)
return addView (v, views.size());
//-----------------------------------------------------------------------------
// Add "view" at "position" to "views".
// Returns position of new view.
// The app should call this to add pages; not used by ViewPager.
public int addView (View v, int position)
views.add (position, v);
return position;
//-----------------------------------------------------------------------------
// Removes "view" from "views".
// Returns position of removed view.
// The app should call this to remove pages; not used by ViewPager.
public int removeView (ViewPager pager, View v)
return removeView (pager, views.indexOf (v));
//-----------------------------------------------------------------------------
// Removes the "view" at "position" from "views".
// Retuns position of removed view.
// The app should call this to remove pages; not used by ViewPager.
public int removeView (ViewPager pager, int position)
// ViewPager doesn't have a delete method; the closest is to set the adapter
// again. When doing so, it deletes all its views. Then we can delete the view
// from from the adapter and finally set the adapter to the pager again. Note
// that we set the adapter to null before removing the view from "views" - that's
// because while ViewPager deletes all its views, it will call destroyItem which
// will in turn cause a null pointer ref.
pager.setAdapter (null);
views.remove (position);
pager.setAdapter (this);
return position;
//-----------------------------------------------------------------------------
// Returns the "view" at "position".
// The app should call this to retrieve a view; not used by ViewPager.
public View getView (int position)
return views.get (position);
// Other relevant methods:
// finishUpdate - called by the ViewPager - we don't care about what pages the
// pager is displaying so we don't use this method.
【讨论】:
以上是关于Android 选项卡式活动通过 volley 添加新片段的主要内容,如果未能解决你的问题,请参考以下文章