从片段更新视图寻呼机指示器

时间:2015-02-09 19:00:13

标签: android android-fragments android-viewpager viewpagerindicator fragmentstatepageradapter

我正在尝试从我的片段更新我的查看寻呼机指示器。 我的问题是项目结构如下: MainActivity - > FragmentA - > FragmentStatePagerAdapter - > FragmentProduct

我尝试在适配器中实现接口但是适配器无法捕获事件,只有mainActivity正在捕获事件。 所以我唯一希望它从片段中更新视图寻呼机(FragmentStatePagerAdapter)。

MainActivity:

public class ProductDetailActivity extends ActivityBase implements ProductFragment.OnFragmentInteractionListener, TabIndicatorListener {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_product_detail);
}


@Override
public void onFragmentInteraction(Uri uri) {

}

@Override
public void updateTabIndicator(ProductFragment productFragment) {
} }

** FragmentA - 设置适配器等等**

public class ProductFragmentDetails extends FragmentBase {

private NavigationHelper navHelper = NavigationHelper.getInstance();

ViewPager viewPager;
PagerAdapter adapter;

TabPageIndicator indicator;

List<ProductPagerAdapter.RegisterValue> prices;
private FragmentManager fm;

ProductPagerAdapter pagerUpdater;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

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

    View mainView = inflater.inflate(R.layout.fragment_product_details, container);
    viewPager = (ViewPager) mainView.findViewById(R.id.pager);

    viewPager.setVisibility(View.VISIBLE);
    indicator = (TabPageIndicator) mainView.findViewById(R.id.indicator);

    fm = getActivity().getSupportFragmentManager();
    adapter = new ProductPagerAdapter(fm, getActivity().getApplicationContext(), indicator, prices);
    viewPager.setAdapter(adapter);

    indicator.setViewPager(viewPager);
    indicator.setVisibility(View.VISIBLE);


    return mainView;
}}

FragmentStateAdapter

public class ProductPagerAdapter extends FragmentStatePagerAdapter implements IconPagerAdapter, TabIndicatorListener {

... init vars etc etc 

@Override
public void startUpdate(ViewGroup container) {
    super.startUpdate(container);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
    Fragment fragment = (Fragment) super.instantiateItem(container, position);
    registeredFragments.put(position, fragment);
    return fragment;
}

@Override
public CharSequence getPageTitle(int position) {
    return Integer.toString(position + 1);
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    super.destroyItem(container, position, object);
}

@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {        super.setPrimaryItem(container, position, object);
}

@Override
public void finishUpdate(ViewGroup container) {
    super.finishUpdate(container);
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return super.isViewFromObject(view, object);
}

@Override
public Parcelable saveState() {
    return super.saveState();
}

@Override
public void restoreState(Parcelable state, ClassLoader loader) {
    super.restoreState(state, loader);
}

/**
 * Get icon representing the page at {@code index} in the adapter.
 *
 * @param index
 */
@Override
public int getIconResId(int index) {
    return prices.get(index).icon;
}

@Override
public int getCount() {
    return prices.size();    //The number of items in the pagerAdapter must be equal to number of products
}

// Instantiate the fragment to display for that page
@Override
public Fragment getItem(int position) {
    String productPrice = Double.toString(prices.get(position).price);
    return ProductFragment.newInstance(productPrice, "OBS");
}

@Override
public void updateTabIndicator(ProductFragment productFragment) {
}

FragmentProduct

public class ProductFragment extends FragmentBase {
init some vars and fields....

     Context context;
TabIndicatorListener tabIndicatorListener;

 public static ProductFragment newInstance(String price, String observations) {
    ProductFragment fragment = new ProductFragment();
    Bundle args = new Bundle();
    args.putString(ARG_PRICE, price);
    args.putString(ARG_OBSERVATIONS, observations);
    fragment.setArguments(args);
    return fragment;
}


public ProductFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    context = getActivity();
    if (getArguments() != null) {
        mParamPrice = getArguments().getString(ARG_PRICE);
        mParamObservation = getArguments().getString(ARG_OBSERVATIONS);
    }

    try {
        tabIndicatorListener = (TabIndicatorListener) getActivity();
    } catch (ClassCastException e) {
        throw new ClassCastException(getActivity().toString()
                + " must implement TabIndicatorListener");
    }
}

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

    View productView = inflater.inflate(R.layout.fragment_product, container, false);

    initViews(productView); //Its initialized the views..

    btnAbsent.setOnClickListener(clickOnBtnAbsent);
    btnValidate.setOnClickListener(clickOnBtnValidate);

    editTxtPrice.setText(String.valueOf(mParamPrice));

    return productView;
}

/**
 * @param productView
 */
private void initViews(View productView) {
    txtViewProductName = (TextView) productView.findViewById(R.id.txtViewNameProduct);
    txtViewPrice = (TextView) productView.findViewById(R.id.txtViewPrice);
   }

@Override
protected int getActionBarMenuRes() {
    return 0;
}

@Override
protected int getMainLayoutRes() {
    return 0;
}

public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}


public View.OnClickListener clickOnBtnAbsent = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        tabIndicatorListener.updateTabIndicator(ProductFragment.this);
              //Here is where i want to update my view pager indicator
      }
};

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        mListener = (OnFragmentInteractionListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}


       public interface OnFragmentInteractionListener {
            public void onFragmentInteraction(Uri uri);
         }

1 个答案:

答案 0 :(得分:1)

首先,我感到惊讶的是,由于您使用的FragmentManager,您没有遇到任何问题。而不是:

fm = getActivity().getSupportFragmentManager();

您应该使用专用的FragmentManager来嵌套片段:

fm = getChildFragmentManager();

其次,与侦听器目标相关,您可以使用片段类getParentFragment()中的一种方法。对于嵌套片段,此方法返回容器片段:

// in ProductFragment
@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    mListener = ((ProductFragmentDetails) getParentFragment()).getListener(); 
}

在ProductFragmentDetails中,您将拥有以下方法:

public (OnFragmentInteractionListener) getListener() {
     return (OnFragmentInteractionListener) adapter;
}