我的任务是将活动转换为片段。
我做了一些更改,但仍然无效。我错过了什么,但我无法弄清楚。我看到的一个问题是公共类QuoteFragment扩展SingleFregmentActivity是强调的......不确定我忘了做什么。求救!
当我将活动转换为片段时,我忘记了什么?
活动代码......
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.content.Intent;
/**
* Main activity for the application.
* Displays a series of quotes
*/
public class QuoteFragment extends SingleFragmentActivity{
/** Key for fact about author stored in Intent sent to AuthorFactActivity. */
public static final String EXTRA_AUTHOR_FACT =
"edu.andrews.cptr252.stephanien.quoteoftheday.author_fact";
private static final String KEY_QUOTE_INDEX = "quoteIndex";
/**ImageView used to display inspirational image*/
private ImageView mImageView;
private TextView mQuoteTextView;
private TextView mAuthorTextView;
private Button mNextButton;
/**Quotes used in app */
private Quote[] mQuoteList = new Quote[]{
new Quote(R.string.quote_text_0, R.string.quote_author_0,
R.string.author_fact_0, R.drawable.mountain_pic),
new Quote(R.string.quote_text_1, R.string.quote_author_1,
R.string.author_fact_1, R.drawable.lier),
new Quote(R.string.quote_text_2, R.string.quote_author_2,
R.string.author_fact_2, R.drawable.math),
new Quote(R.string.quote_text_3, R.string.quote_author_3,
R.string.author_fact_3, R.drawable.smiley),
new Quote(R.string.quote_text_4, R.string.quote_author_4,
R.string.author_fact_4, R.drawable.th),
};
/** Index of current quote in list */
private int mCurrentIndex = 0;
/** Launch activity to display author fact */
private void displayAuthorFact(){
//Create intent with name of class for second activity.
//This intent will be sent to the Activity Manager in the OS
//Which will launch the activity.
Intent i = new Intent(QuoteFragment.this, AuthorFactActivity.class);
//Add extra containing resource id for fact
i.putExtra(EXTRA_AUTHOR_FACT, mQuoteList[mCurrentIndex].getAuthorFact());
//Send the intent to the activity manager.
startActivity(i);
}
/**
* Remember the current quote when the activity is destroyed
* @param savedInstanceState Bundle used for saving identity of current quote
*/
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
//Stored the index of the current quote in the bundle.
//Use our key to access the value later.
savedInstanceState.putInt(KEY_QUOTE_INDEX, mCurrentIndex);
}
/**
* Setup and inflate layout.
* @param savedInstanceState Previously saved Bundle
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View v = inflater.inflate(R.layout.activity_fragment, container, false);
//mQuoteTextView.setText("This should generate an error. Do you see why?");
//Re-display the same quote we were on when activity destroyed
if(savedInstanceState != null){
mCurrentIndex = savedInstanceState.getInt(KEY_QUOTE_INDEX);
}
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
//Display that text for the quote
mQuoteTextView = (TextView) findViewById(R.id.quoteTextView);
int quote = mQuoteList[mCurrentIndex].getQuote();
mQuoteTextView.setText(quote);
mQuoteTextView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
displayAuthorFact();
}
});
//Display the author of the quote
mAuthorTextView = (TextView) findViewById(R.id.authorTextView);
int author = mQuoteList[mCurrentIndex].getAuthor();
mAuthorTextView.setText(author);
mAuthorTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
displayAuthorFact();
}
});
//Display image
mImageView = (ImageView) findViewById(R.id.imageView);
mImageView.setImageResource(R.drawable.mountain_pic);
//set up listener to handle next button presses
mNextButton = (Button) findViewById(R.id.nextButton);
mNextButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// move to the next quote in the list
//if index reaches end array,
//reset index to zero (first quote)
mCurrentIndex++;
if(mCurrentIndex == mQuoteList.length){
mCurrentIndex = 0;
}
updateQuote();
}
});
return v;
}
/** Display the quote at the current index. */
private void updateQuote(){
int quote = mQuoteList[mCurrentIndex].getQuote();
int author = mQuoteList[mCurrentIndex].getAuthor();
int picture = mQuoteList[mCurrentIndex].getPicture();
mQuoteTextView.setText(quote);
mAuthorTextView.setText(author);
mImageView.setImageResource(picture);
}
@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_quote, 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);
}
}
答案 0 :(得分:2)
QuoteFragment
QuoteActivity
setContent(R.layout.activity_quote)
并实施onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
方法onViewCreated (View view, Bundle savedInstanceState)
方法activity_quote.xml
重命名为fragment_quote.xml
创建xml文件activity_quote.xml
并添加QuoteFragment
作为内容:
<fragment android:name="com.example.android.fragments.QuoteFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
请在此处查看详细信息:http://developer.android.com/training/basics/fragments/creating.html#AddInLayout
并删除QuoteActivity
以外的所有代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quote);
}