在ListView项目中将片段转换为ViewPager,单击Android中的Listener

时间:2015-10-11 11:00:49

标签: android listview android-viewpager fragment fragmenttransaction

我正在为零售商店网络开发应用程序 我在Android中遇到了碎片问题。  是否可以在ListView项目内单击侦听器启动  ListItem和View Pager之间的片段事务  自定义页面布局?   任何帮助将不胜感激。谢谢。

这是我的OffersFragment类。

 public class OffersFragment extends Fragment{  
    @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

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


    categoriesListView = (ListView) getActivity().findViewById(R.id.offerCategoriesList);

    categoriesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();

            Bundle bundle = new Bundle();

            bundle.putInt("offerItem", position);



            OfferByCategoryFragment offerByCategoryFragment = new OfferByCategoryFragment();// here is my viewpager fragment                offerByCategoryFragment.setArguments(bundle);
            fragmentTransaction.replace(R.id.offerItemFragment, offerByCategoryFragment).commit();  
        }
    });


    return rootView;
}
}

这是我的ViewPager片段

public class OfferByCategoryFragment extends Fragment {

public static final int PAGE_COUNT = 3;
ViewPager pager;
PagerAdapter pagerAdapter;


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

    View rootView = inflater.inflate(R.layout.offers_category_viewpager, container,
            false);
    pager = (ViewPager) getActivity().findViewById(R.id.pager);
    pagerAdapter = new OffersCategoryFragmentPagerAdapter(getFragmentManager());
    pager.setAdapter(pagerAdapter);

    return rootView;
}

private class OffersCategoryFragmentPagerAdapter extends FragmentPagerAdapter {

    public OffersCategoryFragmentPagerAdapter(FragmentManager fm) {
        super(fm);
    }


    @Override
    public Fragment getItem(int i) {
        return null;
    }

    @Override
    public int getCount() {
        return 0;
    }
}
}`

这是我的查看分页器项目片段

public class OfferItemFragment extends Fragment {

    static final String ARGUMENT_PAGE_NUMBER = "arg_page_number";
    static final String SAVE_PAGE_NUMBER = "save_page_number";
    int pageNumber;

    public static OfferItemFragment newInstance(int page) {
        OfferItemFragment pageFragment = new OfferItemFragment();
        Bundle arguments = new Bundle();
        arguments.putInt(ARGUMENT_PAGE_NUMBER, page);
        pageFragment.setArguments(arguments);
        return pageFragment;
}

    private ImageView offerProductImageView;
    private TextView offerProductTextView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pageNumber = getArguments().getInt(ARGUMENT_PAGE_NUMBER);

        int savedPageNumber = -1;
        if (savedInstanceState != null) {
            savedPageNumber = savedInstanceState.getInt(SAVE_PAGE_NUMBER);
        }

}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.offer_item,null);

    offerProductImageView = (ImageView) getActivity().findViewById(R.id.offerImage);
    offerProductTextView = (TextView) getActivity().findViewById(R.id.offerText);


    return view;
}
}

0 个答案:

没有答案