Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
826 views
in Technique[技术] by (71.8m points)

android - how to call getsupportfragmentmanager() from adapter class?

I have ViewPager as the first item inside a RecyclerView.I want to set FragmentStatePagerAdapter to the viewpager inside the onBindViewHolder() method. But i can't call getsupportfragmentmanager().Please help!!. Here's my code:

public class CustomListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<RowItem> rowItemList;
    private final int VIEW_TYPE_CUSTOM = 0;
    private final int VIEW_TYPE_NORMAL = 1;
    NewPagerAdapter mCustomPagerAdapter;

    public CustomListAdapter(Context context, List<RowItem> rowItemList) {
        this.context = context;
        this.rowItemList = rowItemList;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0)
            return VIEW_TYPE_CUSTOM;
        else
            return VIEW_TYPE_NORMAL;
    }

    @Override
    public int getItemCount() {
        return rowItemList.size()+1;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case VIEW_TYPE_CUSTOM:
                recViewHolder1 viewHolderSecond = (recViewHolder1) holder;
                    mCustomPagerAdapter = new NewPagerAdapter(getSupportFragmentManager());
                    viewHolderSecond.vPager.setAdapter(mCustomPagerAdapter);
                break;
            case VIEW_TYPE_NORMAL:
                recViewHolder2 viewHolderFirst = (recViewHolder2) holder;
                RowItem rowItem = rowItemList.get(position-1);
                viewHolderFirst.vName.setText(rowItem.getName());
                viewHolderFirst.vImage.setImageResource(rowItem.getImageId());
                break;

        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case VIEW_TYPE_CUSTOM:
                View itemView = LayoutInflater.
                        from(parent.getContext()).
                        inflate(R.layout.pagerlayout, parent, false);
                return new recViewHolder1(itemView);
            case VIEW_TYPE_NORMAL:
                View itemView1 = LayoutInflater.
                        from(parent.getContext()).
                        inflate(R.layout.rowlayout, parent, false);
                return new recViewHolder2(itemView1);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }


    public static class recViewHolder1 extends RecyclerView.ViewHolder {
        protected static ViewPager vPager;


        public recViewHolder1(View v) {
            super(v);
            vPager = (ViewPager) v.findViewById(R.id.vPager);
        }
    }

    public class recViewHolder2 extends RecyclerView.ViewHolder implements View.OnClickListener {
        protected  TextView vName;
        protected  ImageView vImage;

        public recViewHolder2(View v) {
            super(v);
            v.setOnClickListener(this);
            vName = (TextView) v.findViewById(R.id.vName);
            vImage = (ImageView) v.findViewById(R.id.vImage);
        } } }
question from:https://stackoverflow.com/questions/31961403/how-to-call-getsupportfragmentmanager-from-adapter-class

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

i solved it by casting mContext

((FragmentActivity)mContext).getSupportFragmentManager().beginTransaction()
                            .replace(R.id.item_detail_container, fragment)
                            .commit();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...