如何设置的Andr​​oid ViewPager涵盖只有一个视图或布局? [英] How do you set Android ViewPager to encompass only one View or Layout?

查看:149
本文介绍了如何设置的Andr​​oid ViewPager涵盖只有一个视图或布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与正确实现视图寻呼机所需的概念挣扎。通过遵循一些教程和参考developer.android.com,我能得到一个视图寻呼机功能基本齐全。寻呼机将通过已经建立的说我的信息0到我的信息9几个文本视图翻转。问题是,认为寻呼机的的翻转上的活动的底部与红色块是右侧的按钮上面的按钮。

I am struggling with the concepts needed to properly implement a view pager. By following some tutorials and referencing developer.android.com, I am able to get a view pager almost fully functional. The pager will flip through several text views that have been setup to say "My Message 0" through "My Message 9". The problem is that the view pager also flips the button on the bottom of the activity and the red block that is right above the button.

我想通过文字有观点仅寻呼机周期。你能帮我明白我做错了吗?

I would like to have the view pager only cycle through the text. Would you please help me understand what I'm doing wrong?

我有重新$ P $活动psents仪表盘:

I have an activity that represents a dashboard:

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;
  }
  }

和页面片段的类:

and a class for the page fragment:

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仪表板:

and finally, my xml for the dashboard:

<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中的注意事项,我想包装在一些观点寻呼机标记文本视图例如:

A note about my xml, I tried wrapping the text view in some view pager tags eg:

<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>

但一切所做的是使文本视图从屏幕上消失,而按钮和红色块循环仍然在原来的问题。

But all that did was make the text view disappear from the screen, while the button and red block still cycled as in the original issue.

推荐答案

看来你正在使用相同的XML布局均活动,也为查看由创建了 PageFragment

It seems that you're using the same XML layout for both the Activity and also for the View that is created by your PageFragment.

您至少需要两个布局。所需的第一个布局是什么使用的主机 FragmentActivity 。这种布局将包含你的 ViewPager ,加上其他部分保持静态屏幕旁边的东西 ViewPager ,如按钮等等。这种布局,你已经证明很多东西(你的第一个XML上市)pretty。

You need at least two layouts. The first layout required is what's used by the hosting FragmentActivity. This layout would contain your ViewPager, plus other stuff that stays static on the screen alongside the ViewPager, such as the button, etc. This layout is pretty much what you've shown (your first XML listing).

第二个布局是将内 onCreateView充气一个()你的 PageFragment 。假设对于初学者你只是想简单的被翻转的TextView s,则是布局 onCreateView()会膨胀,回报将是只包含的TextView

The second layout is the one that would be inflated inside onCreateView() of your PageFragment. Assuming for starters you just want to be flipping between simple TextViews, then the layout that onCreateView() would inflate and return would be a dedicated XML layout that contains just a TextView.

在换句话说,你的XML布局的以下部分:

In other words, the following part of your XML layout:

<TextView
android:id="@+id/textViewPage"
android:layout_width = "match_parent"
android:layout_height= "wrap_content"
/>  

...需要采取离开那里,并把到一个单独的布局,如 fragment_layout.xml ,它是的布局,你会在膨胀 onCreateView()

...needs to be taken out of there and put into a separate layout, such as fragment_layout.xml, and it is that layout that you'd inflate in onCreateView().

这篇关于如何设置的Andr​​oid ViewPager涵盖只有一个视图或布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆