如何使按钮加载Android中的上一页?

时间:2016-02-29 12:45:35

标签: java android

我在不同的网站上搜索了很多来解决这个问题,但我现在还不能。

我有一个简单的应用程序,包含50篇文章,两个按钮,上一页和下一页。

一切正常,直到最后一篇文章50,问题是用户无法选择上一个按钮49,只能从头开始加载。

这是我的代码:MainActivity.java

public class MainActivity extends AppCompatActivity {

private TextView meditCenterText;
private Button mStartButton;

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


    meditCenterText = (TextView) findViewById(R.id.editCenterText);
    mStartButton = (Button) findViewById(R.id.startButton);

    meditCenterText.setMovementMethod(new ScrollingMovementMethod());

    mStartButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String TextView = meditCenterText.getText().toString();
            startStory(TextView);

        }
    });
}

private void startStory(String TextView) {
    Intent intent = new Intent(this, StoryActivity.class);
    intent.putExtra("TextView", TextView);
    startActivity(intent);
}





@Override
protected void onResume() {
    super.onResume();

    meditCenterText.setText("..... ");
}}

StoryActivity.java

public class StoryActivity extends AppCompatActivity {

public static final String TAG = StoryActivity.class.getSimpleName();


private Story mStory = new Story();

private ImageView mImageView;
private TextView mTextView;
private Page mCurrentPage;
private String mName;
private Button mPreviousButton;
private Button mNextButton;
private ScrollView mScrollView;
private Button mStartButton;


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

  //Intent intent = getIntent();
   //mName = intent.getStringExtra(getString(R.string.key_name));

    if(mName == null){
        mName = "";
    }

    Log.d(TAG, mName);

    mImageView = (ImageView)findViewById(R.id.storyImageView);
    mTextView = (TextView)findViewById(R.id.storyTextView);
   mPreviousButton = (Button)findViewById(R.id.previousButton);
   mNextButton = (Button)findViewById(R.id.nextButton);

    mTextView.setMovementMethod(new ScrollingMovementMethod());

    loadPage(0);
}
private void loadPage(int choice){
 mCurrentPage = mStory.getPage(choice);

    Drawable drawable  = getResources().getDrawable(mCurrentPage.getImageId());
    mImageView.setImageDrawable(drawable);

    String pageText = mCurrentPage.getText();
    //add the name if placeholder included.
   //pageText  = String.format(pageText,mName);
    mTextView.setText(pageText);

    if(mCurrentPage.isSingle()){
        mPreviousButton.setVisibility(View.VISIBLE);
        mNextButton.setText("Start From The Beginning");
        mNextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPreviousButton.setVisibility(View.VISIBLE);
                loadPage(0);
            }
        });

    }else{

        mPreviousButton.setText(mCurrentPage.getChoices().getText1());
        mNextButton.setText(mCurrentPage.getChoices().getText2());

        mPreviousButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int previousPage = mCurrentPage.getChoices().getPreviousPage();
                loadPage(previousPage);
            }
        });

        mNextButton.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            int nextPage = mCurrentPage.getChoices().getNextPage();
            loadPage(nextPage);



        }
    });
}}
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

这可能是您的Page类中的逻辑。正确看看它,否则从android方面一切都没问题。最后一件事,您可能应该为您的应用使用ViewPager,而不是每次加载新的oage时更改文本