我已经设法在片段中实现ViewPager,在每个选项卡上填充listviews。如果单击其中一个列表视图中的某个项目,则新片段应填满整个屏幕以显示所选项目的一些详细信息。问题是,新片段没有显示任何内容。它只是空洞而且令人沮丧!
VerlaufFragment.java,持有viewPager
public class VerlaufFragment extends Fragment {
public static ViewPager mViewPager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.viewpager_main, container, false);
// Locate the ViewPager in viewpager_main.xml
mViewPager = (ViewPager) view.findViewById(R.id.viewPager);
// Set the ViewPagerAdapter into ViewPager
mViewPager.setAdapter(new ViewPagerAdapter(getChildFragmentManager()));
mViewPager.setCurrentItem(3);
return view;
}
public class ViewPagerAdapter extends FragmentPagerAdapter {
// Declare the number of ViewPager pages
final int PAGE_COUNT = 7;
private String titles[] = new String[] { "Tab1", "Tab2" , "Tab3" , "Tab4" , "Tab5" , "Tab6" , "Tab7" };
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
// Open FragmentTab1
case 0:
UebungenMainFragment fragmenttab1 = new UebungenMainFragment();
return fragmenttab1;
// Open FragmentTab2
case 1:
UebungenMainFragment fragmenttab2 = new UebungenMainFragment();
return fragmenttab2;
//Open FragmentTab3
default:
UebungenMainFragment fragmenttab3 = new UebungenMainFragment();
return fragmenttab3;
}
}
public CharSequence getPageTitle(int position) {
return titles[position];
}
@Override
public int getCount() {
return PAGE_COUNT;
}
}
@Override
public void onDetach() {
super.onDetach();
}
}
UebungenMainFragment.java包含所有Listviews
public class UebungenMainFragment extends ListFragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] values = new String[] { "Item", "Item", "Item",
"Item", "Item", "Item", "Item", "Item",
"Item", "Item", "Item", "Item", "Item", "Item"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(((ViewGroup) getView().getParent()).getId(), new UebungDetailedFragment());
transaction.addToBackStack(null);
transaction.commit();
}
}
最后是UebungDetailedFragment.java,其中包含项目的详细信息
public class UebungDetailedFragment extends Fragment{
TextView tvItemName;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container,
false);
tvItemName = (TextView) view.findViewById(R.id.frag1_text);
tvItemName.setText("HELLO WORLD");
return view;
}
}
xml文件:
fragment_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/frag1_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
如果我点击其中一个项目,标签仍然可见,列表视图已消失,但我的&#34; HELLO WORLD&#34;没有出现。如果我留在这&#34;空&#34;查看和滑动视野,我仍然可以在列表视图之间滑动,我也不想要。有什么想法使它有效吗?