2

我想讓這樣的事情:如何用Viewpager和Fragment分割窗口?

enter image description here

正如你所看到的,ViewPager是在頂部和窗口是在Viewpager和片段吐盡。

我曾嘗試以下操作:

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


} 

但是,我無法見下文片段米。只顯示ViewPager。有沒有什麼想法可以讓我們像上面一樣在Viewpager和Fragment中分割屏幕?

回答

2

也可能有其他問題,但首先,爲什麼不直接使用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> 
0

我很迷惑你的xml主頁:「activity_homescreen」。 您使用父佈局的相對佈局,但您也爲兩個子佈局添加參數「weight」,其中一個使用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> 
相關問題