2016-03-01 58 views
0

但是,尝试为大学创建基本幻灯片应用程序时,我很难在我的活动中实现ViewPager功能。使用ViewPager时在模拟器中没有图像显示

该代码运行时没有任何错误,但恼人地显示只是一个空白的屏幕。我想让它显示我在Drawable文件夹中的5/6图像。

任何想法显示图像将不胜感激。

感谢

这是我的Java文件:

package com.example.uuj.belfastphotoslider; 

import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentStatePagerAdapter; 
import android.support.v4.view.PagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 

import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 

public class BelfastActivity extends FragmentActivity { 
    /** 
    * The number of pages (wizard steps) to show in this demo. 
    */ 
    private static final int NUM_PAGES = 5; 

    /** 
    * The pager widget, which handles animation and allows swiping horizontally to access previous 
    * and next wizard steps. 
    */ 
    private ViewPager mPager; 

    /** 
    * The pager adapter, which provides the pages to the view pager widget. 
    */ 
    private PagerAdapter mPagerAdapter; 

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

      // Instantiate a ViewPager and a PagerAdapter. 
      mPager = (ViewPager) findViewById(R.id.pager); 
      mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager()); 
      mPager.setAdapter(mPagerAdapter); 

     int[] mResources = { 
       R.drawable.ni_map, 
       R.drawable.map 
     }; 

     } 

     @Override 
     public void onBackPressed() { 
      if (mPager.getCurrentItem() == 0) { 
       // If the user is currently looking at the first step, allow the system to handle the 
       // Back button. This calls finish() on this activity and pops the back stack. 
       super.onBackPressed(); 
      } else { 
       // Otherwise, select the previous step. 
       mPager.setCurrentItem(mPager.getCurrentItem() - 1); 
      } 
     } 

     /** 
     * A simple pager adapter that represents 5 ScreenSlidePageFragment objects, in 
     * sequence. 
     */ 
     private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter { 
      public ScreenSlidePagerAdapter(FragmentManager fm) { 
       super(fm); 
      } 

      @Override 
      public Fragment getItem(int position) { 
       return new ScreenSlidePageFragment(); 
      } 

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

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
      getMenuInflater().inflate(R.menu.menu_belfast, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 

,这里是我的.xml文件:

<RelativeLayout 

xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" 
tools:context="com.example.uuj.belfastphotoslider.BelfastActivity" 
android:background="#020202"> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/content" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/imageView" /> 

</LinearLayout> 

<android.support.v4.view.ViewPager 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/pager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

我有点初学者用Android工作室要小心错误。

在此先感谢,伙计们。

回答

0

我在看这段代码:

 @Override 
     public Fragment getItem(int position) { 
      return new ScreenSlidePageFragment(); 
     } 

,我没有看到你给片段的什么需要显示的任何想法。应该有一些基于位置发生变化的东西,比如片段的参数或其他东西。如果您将代码发布到ScreenSlidePageFragment将会很有帮助。