片段内的ViewPager图库

时间:2016-02-25 08:31:27

标签: android android-fragments android-viewpager

您好我是新的Android开发人员,我尝试做下一件事但没有成功 我有活动,在活动中我有片段,在里面我有**查看寻呼机图像厨房**。

问题是,当我运行应用程序时,带有片段的活动和应用程序内部的库已崩溃 但是当我试图分别用画廊和片段运行活动时,它们工作正常 有什么问题? 在这里我的代码:

活动

公共类infoActivity扩展了AppCompatActivity {

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

}

}

活动XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">

    <fragment
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:name="co.checkit.mobile.checkit.Model.gallery_fragment"
        tools:layout="@layout/imagegalleryfragment"
        android:id="@+id/fragmentid" />


</LinearLayout>
你可以看到我在活动中设置片段

片段类

public class Hotelsgallery_fragment extends Fragment {


    ImageAdapter adapter;
    ViewPager viewPager;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View myFragmentView = inflater.inflate(R.layout.imagegalleryfragment, container, false);
        viewPager = (ViewPager)myFragmentView.findViewById(R.id.viewpager);
        adapter = new ImageAdapter(this);
        viewPager.setAdapter(adapter);

        return myFragmentView;
    }
}


i set the gallery inside the fragment class

片段XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

<android.support.v4.view.ViewPager
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:id="@+id/viewpager">



</android.support.v4.view.ViewPager>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/textView4"
        android:layout_gravity="center_horizontal" />


</LinearLayout>

1 个答案:

答案 0 :(得分:0)

而不是将片段放在xml中,放置框架布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:id="@+id/frame_layout_id" />


</LinearLayout>

在活动中创建片段,使用帧布局ID

进行扩充
        YourFragment fragment = new YourFragment();
        FragmentTransaction ft = getSupportFragmentManager()
                .beginTransaction();
        ft.add(R.id.frame_layout_id, fragment);

        ft.commit();