【问题标题】:How to split window with a Viewpager and Fragment?如何使用 Viewpager 和 Fragment 拆分窗口?
【发布时间】:2015-01-05 09:43:33
【问题描述】:

我想做这样的事情:

如您所见,ViewPager 位于顶部,窗口被吐在 Viewpager 和 Fragment 中。

我尝试了以下方法:

activity_homescreen.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/container"

    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context=".HomeScreenActivity" tools:ignore="MergeRootFrame"


    >
    <android.support.v4.view.ViewPager
        android:id="@+id/home_screen_view_pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>

    <fragment
        android:id="@+id/homescreen_fragment"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        class="com.krupalandharsh.touristguideexperiments.HomeScreenFragment" />

    </RelativeLayout>

fragment_homescreen.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:orientation="horizontal">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="login"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="signup"/>
</LinearLayout>

HomeScreenActivity.java

public class HomeScreenActivity extends Activity{

  ViewPager homescreen_viewpager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_screen);


        homescreen_viewpager = (ViewPager) findViewById(R.id.home_screen_view_pager);
        HomeScreenImageAdapter adapter = new HomeScreenImageAdapter(this);

        homescreen_viewpager.setAdapter(adapter);
    }

} HomeScreenFragment.java

public class HomeScreenFragment extends Fragment{


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
                 View rootView = inflater.inflate(R.layout.fragment_homescreen, container, false);

                 return rootView;
        }
    }

HomeScreenImageAdapter.java

public class HomeScreenImageAdapter extends PagerAdapter {

    private Context context;


    private int[] HomeScreenImages = new int[] {

            android.R.drawable.ic_menu_report_image,
            android.R.drawable.ic_btn_speak_now,
            android.R.drawable.ic_dialog_dialer,
    };

    public HomeScreenImageAdapter(Context context)
    {
        this.context = context;
    }

    @Override
    public int getCount() {
        return HomeScreenImages.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {

       return (view == ((ImageView)object));

    }

    @Override
    public Object instantiateItem (ViewGroup container, int position){

        ImageView imageview = new ImageView(context);

        imageview.setImageResource(HomeScreenImages[position]);

        imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);

        ((ViewPager)container).addView(imageview);

        return imageview;

    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((ImageView) object);
    }


}

但是,我在片段下面看不到 m。只有 ViewPager 显示。您有什么想法可以让我们在上面的 Viewpager 和 Fragment 中分屏吗?

【问题讨论】:

    标签: android android-layout android-activity android-fragments android-viewpager


    【解决方案1】:

    可能还有其他问题,但首先,为什么不直接使用 LinearLayout 作为容器,并具有适当的等权重?

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".HomeScreenActivity"
    tools:ignore="MergeRootFrame" >
    
    <android.support.v4.view.ViewPager
        android:id="@+id/home_screen_view_pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
    
    <fragment
        android:id="@+id/homescreen_fragment"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="0dp"
        class="com.krupalandharsh.touristguideexperiments.HomeScreenFragment" />
    
    </LinearLayout>
    

    【讨论】:

      【解决方案2】:

      我对您的主页 xml 感到困惑:“activity_homescreen”。 您对父布局使用相对布局,但您还为两个子布局添加参数“权重”,其中一个使用 0dp 宽度。 您想使用“权重”为您的 viewPager 和 Fragment 分配空间吗?

      我添加和更改了一些代码并在下面显示。我将父布局更改为 LinearLayout 并添加“weightSum”。 对于子布局,我为高度指定 0dp,为宽度指定 match_parent,为分配它们的高度指定权重。您可以更改参数以适合您的设计。

      我没有尝试过,所以我不确定它是否会起作用。你可以试试看。

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
      
          android:id="@+id/container"
      
          android:layout_width="match_parent"
          android:layout_height="match_parent"
      
          tools:context=".HomeScreenActivity" tools:ignore="MergeRootFrame"
      
          android:weightSum="6"
      
          >
          <android.support.v4.view.ViewPager
              android:id="@+id/home_screen_view_pager"
              android:layout_width="match_parent"
              android:layout_height="0dp"
              android:layout_weight="5"/>
      
          <fragment
              android:id="@+id/homescreen_fragment"
              android:layout_width="match_parent"
              android:layout_height="0dp"
              android:layout_weight="1"
              class="com.krupalandharsh.touristguideexperiments.HomeScreenFragment" />
      
      </LinearLayout>
      

      【讨论】:

        猜你喜欢
        • 2012-01-10
        • 1970-01-01
        • 1970-01-01
        • 2012-03-17
        • 2011-12-11
        • 2018-12-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多