如何在下面的代码中解决在操作栏下显示可滚动视图寻呼机的问题?

时间:2016-02-11 07:07:26

标签: android android-viewpager android-imageview

我正在尝试通过视图寻呼机显示滚动视图但是当我尝试执行它时只显示带有背景颜色的空白页面。它没有反映在屏幕上...实际上我的项目活动很少.i想要在操作栏菜单下显示我的视图寻呼机。这里有一些问题..当我在单独的活动中执行视图寻呼机时,它工作得非常好。但是当我尝试通过我的原始版本运行它时活动它没有正常工作。请给我解决方案。我的代码也是。

    public class SlideViewActivity extends FragmentActivity {
static final int NUM_ITEMS = 6;
ImageFragmentPagerAdapter imageFragmentPagerAdapter;
ViewPager viewPager;
public static final Integer[] IMAGE_NAME = {R.drawable.image1,
        R.drawable.image3,
        R.drawable.image3,
        R.drawable.image3,
        R.drawable.image3,
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_page);
    imageFragmentPagerAdapter = new ImageFragmentPagerAdapter(getSupportFragmentManager());
    viewPager = (ViewPager) findViewById(R.id.pager);
    viewPager.setAdapter(imageFragmentPagerAdapter);
}

public static class ImageFragmentPagerAdapter extends FragmentPagerAdapter {
    public ImageFragmentPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public int getCount() {
        return NUM_ITEMS;
    }

    @Override
    public Fragment getItem(int position) {
        SwipeFragment fragment = new SwipeFragment();
        return SwipeFragment.newInstance(position);
    }
}

public static class SwipeFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View swipeView = inflater.inflate(R.layout.menu, container, false);
        ImageView imageView = (ImageView) swipeView.findViewById(R.id.imageView);
        Bundle bundle = getArguments();
        int position = bundle.getInt("position");
        Integer imageFileName = IMAGE_NAME[position];
        int imgResId = getResources().getIdentifier(String.valueOf(imageFileName), "drawable", "com.javapapers.android.swipeimageslider");
        imageView.setImageResource(imgResId);
        //container.addView(swipeView);
        return swipeView;
    }

    static SwipeFragment newInstance(int position) {
        SwipeFragment swipeFragment = new SwipeFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("position", position);
        swipeFragment.setArguments(bundle);
        return swipeFragment;
    }


}}

menu.xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.brahmaiah.demo_proj_filpkart.MenuActivity">
<!--tools:showIn="@layout/activity_main"-->

<android.support.v7.widget.Toolbar
    android:id="@+id/my_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:elevation="4dp"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_below="@id/my_toolbar"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black">

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:id="@+id/imageView"
            android:layout_gravity="center_horizontal"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true" />


</RelativeLayout>

这是我的视图寻呼机通过xml支持 fragment_page.xml

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

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>

请仔细阅读。并给出一个方法

1 个答案:

答案 0 :(得分:0)

请移动:

<android.support.v7.widget.Toolbar
    android:id="@+id/my_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:elevation="4dp"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

到viewpager上方的activity.xml,并将viewpager的高度和宽度更改为match_parent