每个选项卡都是一个片段,每个片段显示文章,类别和其他一些信息的列表视图.我想做的是,当我从片段视图中的列表视图中点击一个项目时,用完整的文章或更多信息打开一个新的片段.我已经读到这与嵌套的Fragments有关,并且我必须使用方法getChildFragmentManager().我正在使用ViewPager在选项卡中显示片段,这是我的主要活动:
public class MainActivity extends FragmentActivity{
private String tabs[]={"Tab1, Tab2,.<other Fragments>..,VideosFragment"};
ViewPager viewPager=null;
FragmentManager fragmentManager=getSupportFragmentManager();
android.support.v4.app.FragmentTransaction ft=fragmentManager.beginTransaction();
AdapterView adapterView;
public static final int TIME_ENTRY_REQUEST_CODE=1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager=(ViewPager)findViewById(R.id.pager);
viewPager.setonPagechangelistener(new ViewPager.SimpleOnPagechangelistener(){
public void onPageSelected(int position){
getActionBar().setSelectednavigationItem(position);
}
});
viewPager.setAdapter(new AdapterView(fragmentManager));
viewPager.setoffscreenPageLimit(tabs.length);
adapterView=new AdapterView(getSupportFragmentManager());
/*initialization of the action bar: color, icon & title.*/
final android.app.ActionBar actionbar=getActionBar();
ColorDrawable color=new ColorDrawable(Color.parseColor("#cd853f"));
actionbar.setBackgroundDrawable(color);
actionbar.setTitle("Title");
ActionBar.TabListener tabListener=new ActionBar.TabListener() {
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
ft=getFragmentManager().beginTransaction();
//ft.replace(layouts[tab.getPosition()], (adapterView.getItem(tab.getPosition()));
ft.commit();
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
};
/*displaying Tabs for the app */
for(int i=0;i<tabs.length;i++)
actionbar.addTab(actionbar.newTab().setText(tabs[i]).setTabListener(tabListener));
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
viewPager.setonPagechangelistener(new ViewPager.OnPagechangelistener() {
@Override
public void onPageSelected(int position) {
actionbar.setSelectednavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
}
编辑:当我点击一个列表视图的项目时,我想打开一个新的Fragment,这是我的listView适配器:
public class ItemAdapter extends BaseAdapter implements OnItemClickListener{
Item item=new Item();
private ArrayList<Item> news=new ArrayList<Item>();
private static final int NO_PICTURE_VIEW=0;
private static final int PICTURE_VIEW=1;
public ItemAdapter(Context context,ArrayList<Item> items) {
super();
news.addAll(items);
}
@Override
public int getCount() {
// Todo Auto-generated method stub
return news.size();
}
@Override
public Item getItem(int index) {
// Todo Auto-generated method stub
return getItem(index);
}
@Override
public long getItemId(int index) {
// Todo Auto-generated method stub
return index;
}
public int getViewTypeCount() {
return 2;
}
public int getItemViewType(int position) {
Item article=news.get(position);
// if(article.getimageUrl()!=null&&!article.getimageUrl().equals("")&&article.getimageUrl().indexOf("no_pic.png")==-1)//if there's no pic
if(article.getimage()!=null)
return PICTURE_VIEW;
else
return NO_PICTURE_VIEW;
}
@Override
public View getView(int index, View view, ViewGroup parent) {
item=news.get(index);
int type=getItemViewType(index);
if(view==null){
LayoutInflater inflater=LayoutInflater.from(parent.getContext());
if(type==NO_PICTURE_VIEW){
view=inflater.inflate(R.layout.item_no_picture_list,parent,false);
TextView titleView=(TextView)view.findViewById(R.id.titlenopicture);
titleView.setText(item.getTitle());
}
else{
view=inflater.inflate(R.layout.item_picture_list,parent,false);
TextView titleView=(TextView)view.findViewById(R.id.titleArticle);
ImageView image=(ImageView)view.findViewById(R.id.imageItem);
titleView.setText(item.getTitle());
image.setimageBitmap(item.getimage());
}
}
return view;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Log.d("ItemAdapter","clicked on "+position);
}
}
这是ViewPager中的片段之一:
public class VideosFragment extends Fragment{
static ListView listvideo;
ItemAdapter itemAdapter;
Context context;
Activity activity;
FragmentManager fm=getChildFragmentManager();
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
super.onCreateView(inflater, container, savedInstanceState);
View view=inflater.inflate(R.layout.videos_activity, container,false);
listvideo=(ListView)view.findViewById(R.id.videoslist);
context=getActivity();
setRetainInstance(true);
return view;
}
public void onViewCreated(View view, Bundle savedInstanceState){
super.onViewCreated(view, savedInstanceState);
ViewPager vp=(ViewPager)view.findViewById(R.id.pager);
vp.setAdapter(new AdapterView(fm));
}
public void displayVideos(final List<Item> videos){
try {
if(videos==null || videos.size()==0){
Log.d("videos activity","the list is null!!");
}
} catch (Exception e) {
e.printstacktrace();
}
for(int i=0;i<videos.size();i++){
}
itemAdapter=new ItemAdapter(context,(ArrayList<Item>) videos);
listvideo.setAdapter(itemAdapter);
listvideo.setonItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(android.widget.AdapterView<?> parent,
View view, int position, long id) {
Log.d("HomeActivity","Tapped on "+position);
//FragmentTransaction ft=fm.beginTransaction();
//Fragment vp=new VideoPlayer();
//ft.add(R.id.videoview, vp);
///ft.addToBackStack("VideosActivity");
//ft.commit();
}
});
}
public static class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return 4;
}
@Override
public Fragment getItem(int position) {
return new VideoPlayer();//ChildFragment
}
}
}
带注释的fragmenttransaction行以及此后的行给了我NPE,所以我想我缺少了一些东西. VideoPlayer是ChildFragment,现在我只想显示没有信息的片段,只是想知道它是如何工作的.这是我的片段适配器:
class AdapterView extends FragmentPagerAdapter{
public static int NUM_ITEMS=3;
Tab1 tab1=new Tab1();
Tab2 tab2=new Tab2();
VideoFragment va=new VideoFragment();
public AdapterView(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
Log.d("FRAGMENT","EN: "+i);
switch(i){
case 0:
return tab1;
case 1:
return tab2;
case 2:
return va;
default:
return null;
}
}
@Override
public int getCount() {
return NUM_ITEMS;
}
}
我还没有找到想要的教程,所以希望你们中的一些可以帮助我理解它的工作原理.我知道这是可能的,因为Android API 4.2允许这样做.我只想知道嵌套片段的工作方式,以及如何以我的应用程序需要的方式使用它们.任何信息表示赞赏,谢谢.
解决方法:
如果要用另一个片段替换viewpager中的一个片段,请看一下this.但是我建议您创建另一个Fragment(一个容器),然后将viewpager和片段移动到这个新片段中:
这样,当用户单击列表项时,可以将MainFragment替换为Details Fragment.
关于ChildFragmentManager(),当您嵌套了片段时将使用它.如果按照我的建议更改实现,则需要将ChildFragmentManager传递给PagerAdapter:
viewPager.setAdapter(new AdapterView(getChildFragmentManager()));
关于您在VideosFragment中注释的代码行的另一件事.使您的活动提交片段事务,而不是在片段内部进行.有关更好的解释,请阅读this.