gpt4 book ai didi

android - ViewPager 显示了错误的 fragment

转载 作者:行者123 更新时间:2023-11-30 03:47:43 26 4
gpt4 key购买 nike

我试图在网络上到处搜索找到解决我的问题的方法,但我还没有解决。无论如何,这是我的问题。基本上,当我启动我的 TestWeekActivity(包含一个 ViewPager)时,它会显示第一个 fragment/页面的第二个 fragment/页面,当我滑动到第二个 fragment/页面时,我会看到第三个……换句话说,它总是显示下一个或上一个 fragment/页面取决于滑动方向(向左或向右)。

将日志无处不在 我了解到每次滑动 FragmentPagerAdapter.getItem(int) 时都会调用两次(我读到此行为允许缓存近页)。但我不明白的是,为什么在调用 getItem 方法时,也会调用相关 fragment 方法 onStart,因此当我从第一页滑动到第二页时,由于第二次调用了 getItem 方法,我看到了第三个 fragment 。我希望已经足够清楚了。

这是我的代码:测试周 Activity

package com.bord.shutup;

import java.util.ArrayList;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

public class TestWeekActivity extends FragmentActivity implements TabListener {

private static final String TAG = "TestWeekActivity";

ActionBar mActionBar;
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
ArrayList<Fragment> fragments;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_week);
mActionBar = getActionBar();
mActionBar.setDisplayHomeAsUpEnabled(true);
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

// create the Fragment list
fragments = new ArrayList<Fragment>();
for (int i = 0; i < 7; i++) {
Bundle args = new Bundle();
args.putInt("day", i);
Fragment f = DayFragment.newInstance(this);
f.setArguments(args);
fragments.add(f);
}

mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager(), fragments, getResources()
.getStringArray(R.array.day_of_week));

mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {

@Override
public void onPageSelected(int position) {
Log.d(TAG, "onPageSelected " + position);
mActionBar.setSelectedNavigationItem(position);
}
});

for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
mActionBar.addTab(mActionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}

// ///////// TabListener stuff ////////
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
mViewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}

// ///////// Menu stuff ///////////
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_events, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
startActivity(new Intent(this, ChooserActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
return true;
case R.id.add_event:
startActivity(new Intent(this, CreateWeeklyEventActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
return true;
}

return false;
}

public class SectionsPagerAdapter extends FragmentStatePagerAdapter {

private ArrayList<Fragment> fragments;
private String[] titles;

public SectionsPagerAdapter(FragmentManager fm,
ArrayList<Fragment> fragments, String[] titles) {
super(fm);
this.fragments = fragments;
this.titles = titles;
}

@Override
public Fragment getItem(int i) {
Log.d(TAG, "getItem called " + i);
return fragments.get(i);
}

@Override
public int getCount() {
return fragments.size();
}

@Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
}

}

日 fragment

package com.bord.shutup;

import java.util.Calendar;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.ActionMode;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.MultiChoiceModeListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class DayFragment extends Fragment implements
LoaderManager.LoaderCallbacks<Cursor>, MultiChoiceModeListener,
OnItemClickListener {
private static final String TAG = "DayFragment"; // For debugging

ListView list_events;
SimpleCursorAdapter adapter;
private int day;

public static DayFragment newInstance(Context context) {
DayFragment f = new DayFragment();
return f;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate called");
day = getArguments().getInt("day");
Log.d(TAG, "fragment day: " + day);
adapter = new SimpleCursorAdapter(getActivity(), R.layout.row_weekly_event, null, new String[] {
EventsProvider.C_TITLE, EventsProvider.C_NOTE,
EventsProvider.C_START_TIME_HOUR,
EventsProvider.C_START_TIME_MINUTE,
EventsProvider.C_END_TIME_HOUR,
EventsProvider.C_END_TIME_MINUTE }, new int[] {
R.id.title, R.id.note, R.id.hour_start_time,
R.id.minute_start_time, R.id.hour_end_time,
R.id.minute_end_time }, 0);
}

@Override
public void onStart() {
super.onStart();
int day = getArguments().getInt("day");
Toast.makeText(getActivity(), "Day: " + day , Toast.LENGTH_SHORT).show();
}

// Associate a layout
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG, "onCreateView called " + day);
return inflater.inflate(R.layout.activity_events, container, false);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.d(TAG, "onActivityCreated called " + day);
Log.d(TAG, "set adapter " + day);
list_events = (ListView) getActivity().findViewById(
R.id.list_events);
list_events.setAdapter(adapter);
list_events.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
list_events.setMultiChoiceModeListener(this);
list_events.setOnItemClickListener(this);
getActivity().getSupportLoaderManager().initLoader(day, null, this);
}

// ////////// Loader callback ///////////
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
Log.d(TAG, "onCreateLoader called with args: " + id);
switch (id) {
case 0:
return new CursorLoader(getActivity(), EventsProvider.URI_T_WEEK,
null, EventsProvider.C_REPEAT_ON + "=" + Calendar.MONDAY,
null, EventsProvider.C_ID + " DESC");
case 1:
return new CursorLoader(getActivity(), EventsProvider.URI_T_WEEK,
null, EventsProvider.C_REPEAT_ON + "=" + Calendar.TUESDAY,
null, EventsProvider.C_ID + " DESC");
case 2:
return new CursorLoader(getActivity(), EventsProvider.URI_T_WEEK,
null,
EventsProvider.C_REPEAT_ON + "=" + Calendar.WEDNESDAY,
null, EventsProvider.C_ID + " DESC");
case 3:
return new CursorLoader(getActivity(), EventsProvider.URI_T_WEEK,
null, EventsProvider.C_REPEAT_ON + "=" + Calendar.THURSDAY,
null, EventsProvider.C_ID + " DESC");
case 4:
return new CursorLoader(getActivity(), EventsProvider.URI_T_WEEK,
null, EventsProvider.C_REPEAT_ON + "=" + Calendar.FRIDAY,
null, EventsProvider.C_ID + " DESC");
case 5:
return new CursorLoader(getActivity(), EventsProvider.URI_T_WEEK,
null, EventsProvider.C_REPEAT_ON + "=" + Calendar.SATURDAY,
null, EventsProvider.C_ID + " DESC");
case 6:
return new CursorLoader(getActivity(), EventsProvider.URI_T_WEEK,
null, EventsProvider.C_REPEAT_ON + "=" + Calendar.SUNDAY,
null, EventsProvider.C_ID + " DESC");
}
return null;
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
Log.d(TAG, "onLoadFinished called");
adapter.swapCursor(cursor);
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
Log.d(TAG, "onLoaderReset called");
adapter.swapCursor(null);
}

}

提前谢谢大家!

PS:我是新手,别喷我。 ;-)PPS:抱歉英语不好。

最佳答案

您不应该自己管理 fragment 列表,适配器会为您完成。从 onCreate() 中删除“创建 fragment 列表”代码,并使用适配器的 getItem() 中的参数创建所请求页面 fragment 的新实例。

关于android - ViewPager 显示了错误的 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14696016/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com