Справочная информация: В настоящее время у меня работает AppCompatActivity. Но сейчас все в методе onCreate
. Я обеспокоен тем, что это вызывает некоторые небольшие ошибки, с которыми я столкнулся в последнее время с моим PagerAdapter, имеющим java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged!
Ошибки возникают только в том случае, когда я переключаю действия взад и вперед очень быстро. Интересно, исправляет ли это исправление ошибки.Как использовать onCreateView AppCompatActivity
Системные требования: Min SDK: 15, Target SDK: 23 (на момент написания этой статьи, или то, что актуальнейшей)
Вопрос: Как переместить мой пользовательский код инициализации интерфейса в метод onCreateView
?
Я знаком с тем, как реализовать метод onCreate
из android.support.v4.app.Fragment
, как это:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_songdetail, container, false);
rootView.findViewById(R.id.info).setVisibility(View.INVISIBLE);
return rootView;
}
Но нет никаких вариантов, как, что для AppCompatActivity. Это мои варианты, от AppCompatActivity документы:
Просмотр onCreateView (имя String, контекст Контекст, AttributeSet ATTRS)
Просмотр onCreateView (Просмотр родителя, имя String, контекст Контекст, AttributeSet ATTRS)
Вот мой код: (я опустил методы кроме OnCreate и onCreateView)
public class DealPage extends AppCompatActivity
{
private Deal deal;
private Poll poll;
private Video video;
@Override
protected void onCreate(Bundle savedInstanceState)
{
RealmDatabase.setRealmInstance(this);
//----------- UNPACK EXTRAS -----------
String date = getIntent().getExtras().getString(KeyStrings.EXTRA_DATE);
//---------------- PREREQUISITE INITIALIZATION ----------
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_deal_page);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_with_spinner);
setSupportActionBar(toolbar);
//------------ Enable "UP" navigation ---------------
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getDataFromDatabase(date);
//----------------- THEMES AND COLORS ------------------
//Set up colors
final int backgroundColor = this.deal.getTheme().getBackgroundColor().getColor();
final int accentColor = this.deal.getTheme().getAccentColor().getColor();
String themeForeground = this.deal.getTheme().getForeground();
final int foreground = generateForegroundColor(themeForeground);
final String foregroundWebView = generateForegroundWebViewString(themeForeground); // This is necessary because HTML does not have an alpha channel.
//Set toolbar colors
toolbar.setBackgroundColor(accentColor);
toolbar.setTitleTextColor(backgroundColor);
//Set Page Background Color
RelativeLayout dealPageBackground = (RelativeLayout) findViewById(R.id.deal_page_background);
dealPageBackground.setBackgroundColor(backgroundColor);
//----------- INITIALIZE THE ACTUAL DEAL PAGE STUFF ----------------
//Title
TextView title = (TextView) findViewById(R.id.title);
title.setText(this.deal.getTitle());
title.setTextColor(foreground);
//Price
TextView price = (TextView) findViewById(R.id.price);
NumberFormat fmt = NumberFormat.getCurrencyInstance();
price.setText(fmt.format(this.deal.getItems().first().getPrice()));
price.setTextColor(foreground);
//ViewInBrowser
setUpViewInBrowserButton(backgroundColor, accentColor);
AndDown andDown = new AndDown();
//Set up "linkColorHTML"
String linkColorHTML = generateLinkColorHTML(accentColor);
//Features
setUpFeaturesView(andDown, backgroundColor, linkColorHTML, foregroundWebView);
//More Specs button
setUpMoreSpecsButton(backgroundColor, foreground, (Spinner) findViewById(R.id.spinner));
//Story Title
TextView storyTitle = (TextView) findViewById(R.id.story_title);
storyTitle.setText(this.deal.getStory().getTitle());
storyTitle.setTextColor(accentColor);
//Story Body
setUpStoryBody(andDown, backgroundColor, linkColorHTML, foregroundWebView);
//Specs Title
TextView specsTitle = (TextView) findViewById(R.id.specs_title);
specsTitle.setText(this.deal.getTitle());
specsTitle.setTextColor(accentColor);
//Specs
setUpSpecificationsView(andDown, backgroundColor, linkColorHTML, foregroundWebView);
//Set up ViewPager
ViewPager viewPager = (ViewPager) findViewById(R.id.photos_view_pager);
viewPager.setAdapter(new PhotoPagerAdapter(this, this.deal.getPhotos()));
// Setup spinner
setUpSpinner(accentColor, backgroundColor, toolbar);
}
@Override
public View onCreateView(String name, Context context, AttributeSet attrs)
{
return super.onCreateView(name, context, attrs);
}
}