簡體   English   中英

Tabhost + viewPager + ListView; 在捆綁Android中傳遞不同的值

[英]Tabhost + viewPager + ListView; Different value being passed in bundle android

我有一個列表視圖,其中填充了不同的信息,具體取決於所選的選項卡。 我期望的是,歷史記錄選項卡將獲取getPastBookings(),計划選項卡將獲取getFutureBookings(),收藏夾選項卡將獲取getFavoriteBookings()。

不幸的是,歷史和計划選項卡調用getFavoriteBookings或getFutureBookings,而“收藏夾”選項卡則什么也不調用。

代碼可能是什么問題?

先感謝您。 :)

TabsAdapter.java

public class TabsAdapter extends FragmentPagerAdapter implements
        TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {
    private final Context mContext;
    private final TabHost mTabHost;
    private final ViewPager mViewPager;
    private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

        static final class TabInfo {
        private final String tag;
        private final Class<?> clss;
        private final Bundle args;

        TabInfo(String _tag, Class<?> _class, Bundle _args) {
            tag = _tag;
            clss = _class;
            args = _args;
        }    
    }

    static class DummyTabFactory implements TabHost.TabContentFactory {
        private final Context mContext;

        public DummyTabFactory(Context context) {
            mContext = context;
        }

        @Override
        public View createTabContent(String tag) {
            View v = new View(mContext);
            v.setMinimumWidth(0);
            v.setMinimumHeight(0);
            return v;
        }
    }

    public TabsAdapter(FragmentActivity activity, TabHost tabHost,
            ViewPager pager) {
        super(activity.getSupportFragmentManager());
        mContext = activity;
        mTabHost = tabHost;
        mViewPager = pager;
        mTabHost.setOnTabChangedListener(this);
        mViewPager.setAdapter(this);
        mViewPager.setOnPageChangeListener(this);
    }

    public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
        tabSpec.setContent(new DummyTabFactory(mContext));
        String tag = tabSpec.getTag();

        TabInfo info = new TabInfo(tag, clss, args);
        mTabs.add(info);
        mTabHost.addTab(tabSpec);
        notifyDataSetChanged();
    }

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

    @Override
    public Fragment getItem(int position) {
        TabInfo info = mTabs.get(position);
        return Fragment.instantiate(mContext, info.clss.getName(), info.args);
    }

    @Override
    public void onTabChanged(String tabId) {
        int position = mTabHost.getCurrentTab();
        mViewPager.setCurrentItem(position);

    }

    @Override
    public void onPageScrolled(int position, float positionOffset,
            int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {

        TabWidget widget = mTabHost.getTabWidget();
        int oldFocusability = widget.getDescendantFocusability();
        widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
        mTabHost.setCurrentTab(position);
        widget.setDescendantFocusability(oldFocusability);
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }
}

TripsListTabsFragment.java

public class TripsListTabsFragment extends BaseSupportFragment {

    private static final String LOG_TAG = "TripsListTabsFragment";

    @InjectView(R.id.pager) ViewPager mViewPager;
    @InjectView(android.R.id.tabhost) TabHost mFragTabHost;

    TabsAdapter mTripsPagerAdapter;

    public static Fragment newInstance() {
        Bundle bundle = new Bundle();
        TripsListTabsFragment fragment = new TripsListTabsFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return createTabhost(inflater, container);

    }


    private View createTabhost(LayoutInflater inflater, ViewGroup container) {
        View rootView = inflater.inflate(
                R.layout.fragment_tab_contents, container,
                false);

        ButterKnife.inject(this, rootView);

        mFragTabHost.setup();

        mTripsPagerAdapter = new TabsAdapter(getActivity(), mFragTabHost, mViewPager);

        TabSpec scheduledTab = mFragTabHost.newTabSpec("scheduled")
                .setIndicator(getResources().getString(R.string.scheduled));
        Bundle scheduledTabBundle = new Bundle();
        scheduledTabBundle.putSerializable(TripsListFragment.EXTRA_LIST_TYPE,         ListType.SCHEDULED);
        mTripsPagerAdapter.addTab(scheduledTab, TripsListFragment.class,     scheduledTabBundle);

        TabSpec historicalTab = mFragTabHost.newTabSpec("historical")
                .setIndicator(getResources().getString(R.string.history));
        Bundle historicalTabBundle = new Bundle();
        historicalTabBundle.putSerializable(TripsListFragment.EXTRA_LIST_TYPE,     ListType.HISTORICAL);
        mTripsPagerAdapter.addTab(historicalTab, TripsListFragment.class,     historicalTabBundle);

        TabSpec favoritesTab = mFragTabHost.newTabSpec("favorites")
                .setIndicator(getResources().getString(R.string.button_favorites));
        Bundle favoritesTabBundle = new Bundle();
        favoritesTabBundle.putSerializable(TripsListFragment.EXTRA_LIST_TYPE,     ListType.FAVORITES);
        mTripsPagerAdapter.addTab(favoritesTab, TripsListFragment.class,     favoritesTabBundle);

        setTabColor(mFragTabHost);

        return rootView;
    }

    private void setTabColor(TabHost tabhost) {
        for(int i=0;i<tabhost.getTabWidget().getChildCount();i++) {
            tabhost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.beck_tab_indicator_ora    nge);
        }
    }
}

TripsListFragment.java

public class TripsListFragment extends BaseSupportListFragment {

public static final String EXTRA_LIST_TYPE = "com.becktaxi.customer.TRIP_LIST_TYPE";

// @formatter:off
@InjectView (android.R.id.list) ListView mListView;
@InjectView(android.R.id.empty) TextView mEmptyTextView;
// @formatter:on

List<Booking> mBookings;
BookingAdapter mArrayAdapter;

ListType mListType;

public static Fragment newInstance(ListType listType) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(EXTRA_LIST_TYPE, listType);

    TripsListFragment fragment = new TripsListFragment();
    fragment.setArguments(bundle);

    return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_trips_list_layout,
            container, false);

    ButterKnife.inject(this, rootView);

    mBookings = new ArrayList<Booking>();
    ListType lt = (ListType) getArguments().getSerializable(EXTRA_LIST_TYPE);
    showBookingsInList();

    return rootView;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {

    super.onViewCreated(view, savedInstanceState);
    showBookingsInList();
}

public void showBookingsInList() {
    if (mArrayAdapter != null) {
        mArrayAdapter = null;
    }

    mListType = (ListType) getArguments().getSerializable(EXTRA_LIST_TYPE);
    String listType = mListType.name();

    switch (mListType) {
    case SCHEDULED: {
        mBookings = getFutureBookings();
        if(mBookings.size() < 1) {
            mEmptyTextView.setText(R.string.empty_scheduled_bookings);
        }
        break;
    }
    case HISTORICAL: {
        mBookings = getPastBookings();
        if(mBookings.size() < 1) {
            mEmptyTextView.setText(R.string.empty_past_bookings);
        }
        break;
    }

    case FAVORITES: {
        mBookings = getFavoriteBookings();
        if(mBookings.size() < 1) {
            mEmptyTextView.setText(R.string.empty_favourite_bookings);
        }
        break;
    }
    default:
        break;
    }

    mArrayAdapter = new BookingAdapter(getActivity(),
            R.layout.view_booking_single_item_layout, R.id.cbv_booking,
            mBookings, mCallbacks.getHelper().getBookingDao());
    setListAdapter(mArrayAdapter);

}

private List<Booking> getPastBookings() {
    try {
        return DbService.getAllPastBookings(mCallbacks.getHelper()
                .getBookingDao());
    } catch (SQLException e) {
        e.printStackTrace();
    }

    return new ArrayList<Booking>();
}

private List<Booking> getFutureBookings() {
    try {
        return DbService.getAllFutureBookings(mCallbacks.getHelper()
                .getBookingDao());
    } catch (SQLException e) {
        e.printStackTrace();
    }

    return new ArrayList<Booking>();
}

private List<Booking> getFavoriteBookings() {
    try {
        return DbService.getAllFavoriteBookings(mCallbacks.getHelper()
                .getBookingDao());
    } catch (SQLException e) {
        e.printStackTrace();
    }

    return new ArrayList<Booking>();
}

public enum ListType {
    SCHEDULED, HISTORICAL, FAVORITES
}

}

您向您的片段發送一個空包。

public static Fragment newInstance() {
    Bundle bundle = new Bundle();
    TripsListTabsFragment fragment = new TripsListTabsFragment();
    fragment.setArguments(bundle);
    return fragment;
}

我可以通過以下方式修復它:

  1. FragmentPagerAdapter更改為FragmentStatePagerAdapter
  2. FragmentStatePagerAdapter添加了getItemPosition() ,它將返回Position.NONE
  3. 新增notifySetDataChanged()onTabChanged()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM