我有一个。我成功地viewpager
适应了我的。PagerTitleStrip
FragmentStatePagerAdapter
这是我的来源:
public class CardsDetailsNewFragment extends MainFragment {
private Card mCurrentCard;
private View mView;
private boolean mStatusAction;
private ViewPager mViewPager;
private PagerTitleStrip mPagerTitleStrip;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.fragment_card_new_details, container, false);
mCurrentCard = (Card) getData();
Log.e("primary",CardsNewFragment.card_list.get(0).getPrimary()+"primary");
mViewPager = (ViewPager) mView.findViewById(R.id.u_transaction_details_viewpager);
mViewPager.setAdapter(new ScreenSlidePagerAdapter(getChildFragmentManager()));
mPagerTitleStrip = (PagerTitleStrip) mView.findViewById(R.id.u_transaction_details_header);
mViewPager.setCurrentItem(CardsNewFragment.cardposition);
return mView;
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
TransactionDetailsFragment transactionDetailsFragment=new TransactionDetailsFragment();
transactionDetailsFragment.setCard(CardsNewFragment.card_list.get(position));
transactionDetailsFragment.setViewPagerPosition(position);
syncTitles(position);
return transactionDetailsFragment;
}
@Override
public int getCount() {
return CardsNewFragment.card_list.size();
}
@Override
public CharSequence getPageTitle(int position) {
String title = CardsNewFragment.card_list.get(position).getName();
SpannableString whiteSpannable = new SpannableString(title);
whiteSpannable.setSpan(new ForegroundColorSpan(Color.WHITE), 0, title.length(), 0);
return whiteSpannable;
}
}
void syncTitles(int position) {
for (int counter = 0; counter < mPagerTitleStrip.getChildCount(); counter++) {
if (mPagerTitleStrip.getChildAt(counter) instanceof TextView) {
TextView textView = ((TextView) mPagerTitleStrip.getChildAt(counter));
textView.setTextColor(mViewPager.getCurrentItem() == position ? getResources().getColor(R.color.u_white) : getResources().getColor(R.color.u_color));
textView.setTextSize(14);
}
}
}
class TransactionDetailsFragment extends Fragment {
private Card card;
private int possiton;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.testlayout, container, false);
printCardInfo(card,view,possiton);
Log.e("card",card.getName()+"name is");
final SwitchButton primaryCardSwitch = (SwitchButton) view.findViewById(R.id.u_cards_default);
if(card.getPrimary()==1)
{
primaryCardSwitch.setChecked(true);
primaryCardSwitch.setEnabled(false);
}
else
{
primaryCardSwitch.setChecked(false);
primaryCardSwitch.setEnabled(true);
}
return view;
}
public void setCard(Card card) {
this.card = card;
}
public void setViewPagerPosition(int position)
{
this.possiton=position;
}
}
private void printCardInfo( final Card card, View view,final int position) {
((TextView) view.findViewById(R.id.u_card_number)).setText(card.getCardBrand() + " - " + card.getCardNumber());
((TextView) view.findViewById(R.id.u_card_bank_title)).setText(card.getBankName());
((TextView) view.findViewById(R.id.u_card_holder)).setText(card.getHolder());
((TextView) view.findViewById(R.id.u_card_status)).setText(CardStatus.getByStatus(card.getStatus()).getStatusName());
((TextView) view.findViewById(R.id.u_card_status)).setTextColor(getResources().getColor(CardStatus.getByStatus(card.getStatus()).getColor()));
mView.findViewById(R.id.u_cards_transfer).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
transfer(card);
Log.e("position,",position +"position");
Log.e("uploaddddddd", card + "get primary");
}
});
}
void transfer(final Card card) {
removeOnDetach();
mData = card;
mainCallbacks.addFragment(TransferNewVersion.class);
}
}
我有一个问题:我的setViewPagerPositio
n 方法不完美。当我调用 transfermethod(button click)
时,我的代码必须按位置选择卡片,但是例如,如果我有 4 个元素并在我的日志中viewpager
选择 first items(Card)
,我可以看到 second Item (Card)
。位置 +1
Log.e("uploaddddddd", card + "get primary");
我不知道我的代码有什么问题:我的选项中有位置问题