我使用ViewPager创建了一个活动,该活动应该显示对象列表中的项目 起始位置通过活动意图传递,这很有效。 在正在创建的片段的OnCreateView中,我从列表中加载值并显示它们 我知道ViewPager会自动加载所选项的上一个和下一个项目 问题是在活动中我看到最后创建的片段而不是当前片段。
这是活动代码:
public class SwipeLupettoDetail extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
private int lupetto_id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_swipe_lupetto_detail);
Toolbar toolbar = (Toolbar) findViewById(R.id.detail_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
lupetto_id = getIntent().getIntExtra("ID_Lupetto", 0);
int position = getIntent().getIntExtra("POSITION", 0);
Log.d("RECIEVED", "POSITION from intent: " + position);
Log.d("RECIEVED", "ID_Lupetto from intent: " + lupetto_id);
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setCurrentItem(position-1);
mViewPager.setPageTransformer(true, new ZoomOutPageTransformer());
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { }
@Override
public void onPageSelected(int position) {
Log.d("POSITION", "OnPageSelected CurrentItem: " + mViewPager.getCurrentItem());
}
@Override
public void onPageScrollStateChanged(int state) { }
});
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), Edit_Lupetto.class);
intent.putExtra("ID_Lupetto", lupetto_id);
Log.d("Activity Transition", "Transazione, Inviato ID_lupetto: " + lupetto_id);
startActivity(intent);
finish();
}
});
}
public static class PlaceholderFragment extends Fragment {
public static final List<Lupetto> lupettoList = Lupetto.listAll(Lupetto.class, "SESTIGLIA, PISTA " + "DESC");
private static final String POSITION = "position";
private int lupetto_id;
private Lupetto lupetto;
private Anagrafica anagrafica;
private View rootView;
public PlaceholderFragment() { }
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(POSITION, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.lupetto_detail, container, false);
if (getArguments().containsKey(POSITION)) {
Activity activity = this.getActivity();
lupetto_id = getArguments().getInt(POSITION, 0);
if (lupetto_id < 0) {
lupetto_id = activity.getIntent().getIntExtra(POSITION, 0);
}
lupetto = lupettoList.get(lupetto_id);
anagrafica = lupetto.Anagrafica;
Log.d("PAGEVIEW", "OnCreateView Position: " + lupetto_id);
Log.d("LOAD", "OnCreateView LoadingObject: " + lupetto.Nome + " " + lupetto.Cognome);
CollapsingToolbarLayout appBarLayout = (CollapsingToolbarLayout) activity.findViewById(R.id.toolbar_layout);
if (appBarLayout != null) {
appBarLayout.setTitle(lupetto.Nome + " " + lupetto.Cognome);
}
}
List<Specialità> specs;
List<Prova> provs;
if (lupetto != null) {
((TextView) rootView.findViewById(R.id.txt_email)).setText(anagrafica.Email);
((TextView) rootView.findViewById(R.id.txt_indirizzo)).setText(anagrafica.Indirizzo);
((TextView) rootView.findViewById(R.id.txt_pista)).setText(lupetto.Pista.toString());
((TextView) rootView.findViewById(R.id.txt_sestiglia)).setText(lupetto.Sestiglia.toString());
((CheckedTextView) rootView.findViewById(R.id.ctb_cda)).setChecked(lupetto.CdA);
((TextView) rootView.findViewById(R.id.txt_fisso)).setText(anagrafica.Tel_fisso);
((TextView) rootView.findViewById(R.id.txt_madre)).setText(anagrafica.Cell_Madre);
((TextView) rootView.findViewById(R.id.txt_padre)).setText(anagrafica.Cell_Padre);
((TextView) rootView.findViewById(R.id.tv_data)).setText(anagrafica.DataNascita);
((TextView) rootView.findViewById(R.id.tv_luogo)).setText(anagrafica.Luogo_Nascita);
specs = Specialità.stringToIDs(lupetto.Specialità);
provs = Prova.IDStringToProveList(lupetto.Prove);
//POPOLAZIONE ListView Spec
TextView tv_spec;
ScrollView lv = (ScrollView) rootView.findViewById(R.id.lv_Specialità);
LinearLayout linearLayout = (LinearLayout) lv.getChildAt(0);
for (Specialità spec : specs) {
tv_spec = new TextView(getContext());
tv_spec.setText(spec.Nome);
tv_spec.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
linearLayout.addView(tv_spec);
}
TypedValue typedValue = new TypedValue();
getActivity().getTheme().resolveAttribute(R.attr.selectableItemBackgroundBorderless,
typedValue, true);
lv = (ScrollView) rootView.findViewById(R.id.lv_Prove);
linearLayout = (LinearLayout) lv.getChildAt(0);
for (Prova p : provs) {
final int index = Prova.allProve.indexOf(p);
tv_spec = new TextView(getContext());
tv_spec.setText(p.Nome);
tv_spec.setSingleLine(true);
tv_spec.setHorizontallyScrolling(false);
tv_spec.setEllipsize(TextUtils.TruncateAt.END);
tv_spec.setTag(index);
tv_spec.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
tv_spec.setClickable(true);
tv_spec.setBackgroundResource(typedValue.resourceId);
tv_spec.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(
getActivity().getApplicationContext(),
ProvaDettaglio.class);
i.putExtra("ID_Prova", (int) v.getTag());
i.putExtra("ID_Lupetto", lupetto_id);
startActivity(i);
}
}
);
linearLayout.addView(tv_spec);
}
}
return rootView;
}
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return PlaceholderFragment.newInstance(position);
}
@Override
public int getCount() {
return (int) Lupetto.count(Lupetto.class);
}
@Override
public CharSequence getPageTitle(int position) {
Lupetto l = PlaceholderFragment.lupettoList.get(position);
return l.Nome + " " + l.Cognome;
}
}
}
这是我在创建活动时以及在向左和向右滑动之后看到的日志:
10-28 10:17:28.425 D/RECIEVED: POSITION: 1
10-28 10:17:28.425 D/RECIEVED: ID_Lupetto: 2
10-28 10:17:28.521 D/PAGEVIEW: OnCreateView Position: 0
10-28 10:17:28.522 D/LOAD: OnCreateView LoadingObject: A
10-28 10:17:28.526 D/PAGEVIEW: OnCreateView Position: 0
10-28 10:17:28.526 D/LOAD: OnCreateView LoadingObject: A
10-28 10:17:31.324 D/POSITION: OnPageSelected CurrentItem: 1
10-28 10:17:31.867 D/PAGEVIEW: OnCreateView Position: 1
10-28 10:17:31.867 D/LOAD: OnCreateView LoadingObject: B
10-28 10:17:32.027 D/POSITION: OnPageSelected CurrentItem: 2
10-28 10:17:32.299 D/PAGEVIEW: OnCreateView Position: 2
10-28 10:17:32.299 D/LOAD: OnCreateView LoadingObject: C
10-28 10:17:32.621 D/POSITION: OnPageSelected CurrentItem: 3
10-28 10:17:33.868 D/POSITION: OnPageSelected CurrentItem: 2
10-28 10:17:34.126 D/PAGEVIEW: OnCreateView Position: 0
10-28 10:17:34.126 D/LOAD: OnCreateView LoadingObject: A
10-28 10:17:34.453 D/POSITION: OnPageSelected CurrentItem: 1
10-28 10:17:34.738 D/PAGEVIEW: OnCreateView Position: 0
10-28 10:17:34.738 D/LOAD: OnCreateView LoadingObject: A
10-28 10:17:35.018 D/POSITION: OnPageSelected CurrentItem: 0
10-28 10:17:36.136 D/POSITION: OnPageSelected CurrentItem: 1
10-28 10:17:36.353 D/PAGEVIEW: OnCreateView Position: 1
10-28 10:17:36.353 D/LOAD: OnCreateView LoadingObject: B
10-28 10:17:36.651 D/POSITION: OnPageSelected CurrentItem: 2
10-28 10:17:36.823 D/PAGEVIEW: OnCreateView Position: 2
10-28 10:17:36.823 D/LOAD: OnCreateView LoadingObject: C
10-28 10:17:37.163 D/POSITION: OnPageSelected CurrentItem: 3
10-28 10:17:38.901 D/POSITION: OnPageSelected CurrentItem: 2
10-28 10:17:39.169 D/PAGEVIEW: OnCreateView Position: 0
10-28 10:17:39.169 D/LOAD: OnCreateView LoadingObject: A
10-28 10:17:39.870 D/POSITION: OnPageSelected CurrentItem: 1
10-28 10:17:40.070 D/PAGEVIEW: OnCreateView Position: 0
10-28 10:17:40.070 D/LOAD: OnCreateView LoadingObject: A
对于列表中的这个示例,我只插入了4个项目(A,B,C,D),但是项目A显示了2次,D从未显示,它显示创建的最后一个片段而不是它应该显示的片段。
有什么建议吗?
编辑: Lupetto类代码:
package gestionalebranco.walker93.com.gestionalebranco;
import com.orm.SugarRecord;
/**
* Created by Workstation 2 on 26/09/2016.
*/
public class Lupetto extends SugarRecord{
public String Nome;
public String Cognome;
public Sestiglie Sestiglia;
public Pista Pista;
public String Specialità;
public boolean CdA;
public Anagrafica Anagrafica;
public String Prove;
public static String Assenze;
public Lupetto(){
}
public Lupetto(String nome, String cognome, Sestiglie sestiglia, Pista pista,
String specialità, boolean cda, Anagrafica anagrafica, String prove, String assenze) {
this.Nome = nome;
this.Cognome = cognome;
this.Sestiglia = sestiglia;
this.Pista = pista;
this.Specialità = specialità;
this.CdA = cda;
this.Anagrafica = anagrafica;
this.Prove = prove;
this.Assenze = assenze;
}
}