我正在为正确实现视图寻呼机所需的概念而苦苦挣扎。通过遵循一些教程并参考 developer.android.com,我能够获得一个几乎功能齐全的视图寻呼机。寻呼机将翻阅几个已设置为从“我的消息 0”到“我的消息 9”的文本视图。问题是视图寻呼机还翻转了活动底部的按钮和按钮正上方的红色块。
我想让视图寻呼机只循环浏览文本。你能帮我理解我做错了什么吗?
我有一个代表仪表板的活动:
public class DashBoard extends FragmentActivity
{
private static final int NUMBER_OF_PAGES = 10;
private ViewPager mViewPager;
private MyFragmentPagerAdapter mMyFragmentPagerAdapter;
public void onCreate(Bundle icicle){
super.onCreate(icicle);
setContentView(R.layout.dashboard);
mViewPager = (ViewPager) findViewById(R.id.viewpager);
mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mMyFragmentPagerAdapter);
}
private static class MyFragmentPagerAdapter extends FragmentPagerAdapter{
public MyFragmentPagerAdapter(FragmentManager fm)
{
super(fm);
}
@Override
public Fragment getItem(int index)
{
return PageFragment.newInstance("My Message " + index);
}
@Override
public int getCount(){
return NUMBER_OF_PAGES;
}
}
和页面片段的类:
public class PageFragment extends Fragment {
public static PageFragment newInstance(String title){
PageFragment pageFragment = new PageFragment();
Bundle bundle = new Bundle();
bundle.putString("title", title);
pageFragment.setArguments(bundle);
return pageFragment;
}
@Override
public void onCreate(Bundle icicle){
super.onCreate(icicle);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle icicle){
View view = inflater.inflate(R.layout.dashboard, container, false);
TextView textView = (TextView) view.findViewById(R.id.textViewPage);
textView.setText(getArguments().getString("title"));
return view;
}
}
最后,我的仪表板 xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/dashbaordLabel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
/>
<TextView
android:id="@+id/textViewPage"
android:layout_width = "match_parent"
android:layout_height= "wrap_content"
/>
<Button
android:id="@+id/newGoalButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/stringNewGoal"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:onClick="createNewGoal"
/>
<RelativeLayout
android:id="@+id/SpaceBottom"
android:layout_width="match_parent"
android:layout_height="75dp"
android:layout_above="@id/newGoalButton"
android:background="@color/red"
>
</RelativeLayout>
</RelativeLayout>
关于我的 xml 的注释,我尝试将文本视图包装在一些视图寻呼机标签中,例如:
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
>
<TextView
android:id="@+id/textViewPage"
android:layout_width = "match_parent"
android:layout_height= "wrap_content"
/>
</android.support.v4.view.ViewPager>
但所做的只是让文本视图从屏幕上消失,而按钮和红色块仍然像原来的问题一样循环。