1

私は現在、教育目的でゲームのような小さな本を作成しており、ページビューアを使用して各ページにStrings.xmlからの情報を配置しています。これはかなり小さい本なので、私はこのようにしています。すべてのページが表示され、問題なくスワイプできます。私の問題は、特定のページ間隔を表示したいときに始まります。ゲームには3つのレッスンがあります。レッスン1には2ページ(1-2)、レッスン2には(3-9)、レッスン3には(10-15)があります。合計15ページの情報があります。

ページビューアに特定の間隔のページのみを表示させる方法はありますか?

ユーザーにボタンを選択してもらうと、選択したレッスンのインテントがBookActivityという別のクラスに送信されます。そこから、どのレッスンがクリックされたかを把握し、それに基づいて特定のページを表示したいと考えています。これがbookActivityクラスです。

    public class BookActivity extends Activity
{
    private String getLessonName;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Intent intent = getIntent();
        // getting object's properties from LoginActivity class.

        getLessonName = intent.getStringExtra("nameOfLesson");
        MyPageAdapter adapter = new MyPageAdapter();
        ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);

       // Button addBasicQuestionsForFirstLesson = (Button)findViewById(R.id.addBasicQuestionsButton);
        myPager.setAdapter(adapter);

        if(getLessonName.equals("one"))
        {
        myPager.setCurrentItem(0);
        if(myPager.getCurrentItem() == 1)
        {
            myPager.setCurrentItem(1);
        }
        }
        else if(getLessonName.equals("two"))
        {
            myPager.setCurrentItem(2);
            if(myPager.getCurrentItem() == 8)
            {
                myPager.setCurrentItem(8);
            }
        }
        else if(getLessonName.equals("three"))
        {
            myPager.setCurrentItem(9);
            if(myPager.getCurrentItem()==14)
            {
                myPager.setCurrentItem(14);
            }
        }


    }

}

これを見てくれてありがとう。誰かがこのようなことをする方法を知っていれば、私は本当に感謝します。これをより良くする方法があれば、別々のページに対して複数のアクティビティを作成したくありません。

 class MyPageAdapter extends PagerAdapter

{{

public int getCount()
{
    return 15;
}

public Object instantiateItem(View collection, int position)
{

    LayoutInflater inflater = (LayoutInflater) collection.getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    int resId = 0;
    switch (position)
    {
    case 0:

        resId = R.layout.activity_page_one;

        break;
    case 1:
        // second page with button to add questions!
        resId = R.layout.activity_page_two;

        View view = inflater.inflate(resId, null);
        LinearLayout layout = (LinearLayout) view
                .findViewById(R.id.linearbasic);
        Button addButton = (Button) layout
                .findViewById(R.id.addBasicQuestionsButton);
        ((ViewPager) collection).addView(view, 0);

        addButton.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Log.d("clicked", "clclcl");
                LessonActivity.
                importQuestions("lessononebasicquestions");
                // LessonActivity.clicked = true;
                // Toast.makeText(getApplicationContext(), "yay",
                // Toast.LENGTH_LONG).show();

            }
        });

        return view;

        // break;
    case 2:

        resId = R.layout.activity_page_three;

        break;
    case 3:
        resId = R.layout.activity_page_four;
        break;
    case 4:
        resId = R.layout.activity_page_five;
        break;
    case 5:
        resId = R.layout.activity_page_six;
        break;
    case 6:
        resId = R.layout.activity_page_seven;
        break;
    case 7:
        resId = R.layout.activity_page_eight;
        break;
    case 8:
        resId = R.layout.activity_page_nine;

        View view2 = inflater.inflate(resId, null);
        LinearLayout layout2 = (LinearLayout) view2
                .findViewById(R.id.linearlife);
        Button lifeCycleButton = (Button) layout2
                .findViewById(R.id.addLifeCycleButton);
        ((ViewPager) collection).addView(view2, 0);

        lifeCycleButton.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Log.d("clicked", "clclcl");
                LessonActivity
                .importQuestions("lessontwolifecyclequestions");
                // LessonActivity.clicked = true;
                // Toast.makeText(getApplicationContext(), "yay",
                // Toast.LENGTH_LONG).show();

            }
        });

        return view2;

    case 9:
        resId = R.layout.activity_page_ten;
        break;
    case 10:
        resId = R.layout.activity_page_eleven;
        break;
    case 11:
        resId = R.layout.activity_page_twelve;
        break;
    case 12:
        resId = R.layout.activity_page_thirteen;
        break;
    case 13:
        resId = R.layout.activity_page_fourteen;
        break;
    case 14:
        resId = R.layout.activity_page_fifteen;

        View view3 = inflater.inflate(resId, null);
        LinearLayout layout3 = (LinearLayout) view3
                .findViewById(R.id.lineartools);
        Button toolsButton = (Button) layout3
                .findViewById(R.id.addToolsQuestionsButton);
        ((ViewPager) collection).addView(view3, 0);

        toolsButton.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Log.d("clicked", "clclcl");
                LessonActivity.
                importQuestions("lessonthreetoolsquestions");
                // LessonActivity.clicked = true;
                // Toast.makeText(getApplicationContext(), "yay",
                // Toast.LENGTH_LONG).show();

            }
        });

        return view3;

    }
    View view = inflater.inflate(resId, null);

    ((ViewPager) collection).addView(view, 0);

    return view;

}

@Override
public void destroyItem(View arg0, int arg1, Object arg2)
{
    ((ViewPager) arg0).removeView((View) arg2);

}

@Override
public Parcelable saveState()
{
    return null;
}

@Override
public boolean isViewFromObject(View arg0, Object arg1)
{
    // TODO Auto-generated method stub
    return arg0 == ((View) arg1);
}

}

ありがとうございました。これが私の追加したコードです!

4

1 に答える 1

1

ページの変更にリスナーを使用して、ユーザーが範囲外になった場合に特定のページに強制的に移動させることができます。使用onPageSelected()すると、範囲外のページを検出し、ユーザーを前のページに強制的に移動させることができます。リスナーは次のとおりです: http://developer.android.com/reference/android/support/v4/view/ViewPager.OnPageChangeListener.html

ただし、ユーザーが選択したレッスンに応じて、特定のページのみを ViewPager に追加することをお勧めします。レッスンを選択するためのボタンを備えたメインのアクティビティがある場合、クリックすると、必要なページで満たされた ViewPagers を持つさまざまなアクティビティを開始できます。したがって、次のようになります。

MainActivity
- Button1
    -Activity with ViewPager for pages 1-2
- Button2
    -Activity with ViewPager for pages 3-9
- Button3
    -Activity with ViewPager for pages 10-15

このように構造化する方が整理されていると思います。

于 2012-08-09T16:57:08.173 に答える