以下是我的应用的截图。
此屏幕是一个具有滑动标签布局的片段。它将包含另一个将在listview中显示数据的片段。问题是,为了加载数据,从微调器中选择的值需要在选项卡中的片段内传递。我不知道该怎么做。一种方法是选项卡片段实现回调,并且应该加载回调数据。但我没有得到如何在微调器的onItemSelected
中注册该回调。
注意:选项卡中的所有片段仅在listview中显示数据,因此我创建了一个公共片段。
这是我的代码到目前为止: 截图的片段
public class BuyListingFragment2 extends BaseFragment {
private Context ctx;
private Spinner vehicle_type;
private ArrayList<ListingTabModel> mListingTabs = new ArrayList<ListingTabModel>();
private ArrayAdapter<String> spinnerAdapter;
private ArrayList<String> vehicleTypeSpinnerlist;
private int spinnerPosition;
private SlidingTabLayout sliding_tabs;
private BuyListingPagerAdapter buyListingPagerAdapter;
public static BuyListingFragment2 newInstance(String category,
int position, String preselectedFilters) {
BuyListingFragment2 fragment = new BuyListingFragment2();
Bundle args = new Bundle();
args.putString("vehicle_type", category);
args.putInt("spinner_position", position);
fragment.setArguments(args);
return fragment;
}
public BuyListingFragment2() {
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.vehicleType = getArguments().getString("vehicle_type");
this.selectedVehicle = this.vehicleType;
this.spinnerPosition = getArguments().getInt("spinner_position");
ArrayList<CategoryType> vehicleTypeList = RegistrationResponse
.getInstance().getVehicleTypeList();
spinnerAdapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, vehicleTypeList);
buyListingPagerAdapter = new BuyListingPagerAdapter(
getChildFragmentManager(), mListingTabs);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ctx = getActivity();
vehicle_type = (Spinner) view.findViewById(R.id.vehicle_type);
vehicle_type.setAdapter(spinnerAdapter);
vehicle_type.setSelection(spinnerPosition, false);
if (mListingTabs.isEmpty()) {
String[] tabNames = getResources().getStringArray(
R.array.listing_tab_names);
for (int i = 0; i < tabNames.length; i++) {
String tabName = tabNames[i];
ListingTabModel mListingTabModel = new ListingTabModel();
mListingTabModel.setTagName(tabName);
mListingTabs.add(mListingTabModel);
}
}
buyListingPagerAdapter.notifyDataSetChanged();
listing_layout_viewpager = (ViewPager) view
.findViewById(R.id.listing_layout_viewpager);
listing_layout_viewpager.setAdapter(buyListingPagerAdapter);
sliding_tabs = (SlidingTabLayout) view.findViewById(R.id.sliding_tabs);
sliding_tabs.setDistributeEvenly(true);
sliding_tabs.setViewPager(listing_layout_viewpager);
vehicle_type.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
spinnerPosition = position;
//How to register listener here
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
标签内的常见片段
public class ListingFragment extends BaseFragment implements
OnSpinnerDataSelected {
private InfiniteListView mListView;
private BuyListingListAdapter buyListingAadapter;
private RobotoLightTextView emptyMessage;
private int currentPageNumber = 1;
private int totalPages;
private HashMap<String, String> params = new HashMap<String, String>();
private int apiCallCount = 0;
private Context ctx;
private String vehicleType;
private ProgressBar progressBar;
public ListingFragment() {
}
public static ListingFragment newInstance(ListingTabModel mListingTabModel) {
ListingFragment mFragment = new ListingFragment();
Bundle bundle = new Bundle();
// bundle.putBoolean("is_grid_view", mListingTabModel.isShowGridView());
// bundle.putString("vehicle_type", mListingTabModel.getVehicleType());
mFragment.setArguments(bundle);
return mFragment;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ctx = getActivity();
emptyMessage = (RobotoLightTextView) view
.findViewById(R.id.empty_message);
mListView = (InfiniteListView) view.findViewById(R.id.lstVw_buy);
boolean isGrid = getArguments().getBoolean("is_grid_view");
vehicleType = getArguments().getString("vehicle_type");
buyListingAadapter = new BuyListingListAdapter(ctx,
mVehicleListingList, isGrid);
mListView.setAdapter(buyListingAadapter);
progressBar = new ProgressBar(ctx);
}
@Override
public int getLayoutId() {
return R.layout.layout_messages;
}
@Override
public void onSpinnerDataSelected(String vehicleCategory) {
// TODO: fetch listing data
}
}
由ListingFragment实施的回调
public interface OnSpinnerDataSelected {
void onSpinnerDataSelected(String vehicleCategory);
}
FragmentStatePagerAdapter
public class BuyListingPagerAdapter extends FragmentStatePagerAdapter {
ArrayList<ListingTabModel> mFragmentsList;
public BuyListingPagerAdapter(FragmentManager fm,
ArrayList<ListingTabModel> mFragmentsList) {
super(fm);
this.mFragmentsList = mFragmentsList;
}
@Override
public Fragment getItem(int index) {
ListingFragment listingFragment = ListingFragment
.newInstance(mFragmentsList.get(index));
return listingFragment;
}
@Override
public int getCount() {
return mFragmentsList.size();
}
@Override
public CharSequence getPageTitle(int position) {
String tagName = mFragmentsList.get(position).getTagName();
tagName = tagName.replace("_", " ");
return tagName;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return object == view;
}
}
答案 0 :(得分:3)
当使用一个活动和多个片段时,我建议让片段管理UI 并使用Activity有一个控制器/模型。
微调器与其他片段通信的工作流程:
答案 1 :(得分:2)