使用PagerAdapter从ViewPagerAdapter内部创建片段

时间:2018-08-30 19:38:21

标签: android android-viewpager fragment

好的,所以我会尽量简短地讲清楚这一点。我有以下代码:

public class PopMessSlidingAdapter extends PagerAdapter {
Context context;
LayoutInflater inflater;
private FragmentManager fragmentManager;



String[] dis = {

        "cat",
        "dog",
        "sheep"
};
SparseArray<Fragment> FragmentList = new SparseArray<>();


public PopMessSlidingAdapter(Context context, FragmentManager fm){

    this.context=context;
    fragmentManager=fm;


}
@Override
public int getCount() {
    return dis.length;
}

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

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

@Override
public Object instantiateItem(final ViewGroup container, final int position) {
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View view = inflater.inflate(R.layout.popsliderow_layout, container, false);
    Log.d("ALLTHEVIEWSTT", String.valueOf(position));
    final LinearLayout slide = view.findViewById(R.id.slideLinear);
    final LinearLayout contain = view.findViewById(R.id.userProfileContainer);
    contain.setTag(position);

    TextView author = view.findViewById(R.id.slideauthor);
    author.setText(dis[position]);

    //view.setTag(position, "Hacky");
    //fragments(position);
    view.setOnTouchListener(new OnSwipTouchListener(context){
        public void onSwipeTop() {
            Toast.makeText(context, "DOG BISCUITS", Toast.LENGTH_SHORT).show();

            fragments(position);

            final Animation animate= AnimationUtils.loadAnimation(context,R.anim.shift_up);
            animate.setFillAfter(true);
            slide.startAnimation(animate);

        }
        public void onSwipeBottom(){
//                Toast.makeText(context, String.valueOf(position), 
Toast.LENGTH_SHORT).show();
//                int count = fragmentManager.getBackStackEntryCount();
//                Toast.makeText(context, String.valueOf(count), 
Toast.LENGTH_SHORT).show();
//                if(count 
>0&&fragmentManager.findFragmentByTag(String.valueOf(position)) instanceof 
SlideUserFrag){
//
//
//                    final Animation animate= 
AnimationUtils.loadAnimation(context,R.anim.shift_down);
//                    animate.setFillAfter(true);
//                    slide.startAnimation(animate);
//                    FragmentTransaction fragmentTransaction =   
fragmentManager.beginTransaction();
//                    
fragmentTransaction.setCustomAnimations(R.anim.slide_in_up, 
R.anim.slide_out_down);
//                    fragmentTransaction.
//                            

remove(fragmentManager.findFragmentById(R.id.userProfileContainer)).commit();
//                }
        }
    });

    container.addView(view);

    return view;//super.instantiateItem(container, position);
}

 void fragments(int position){

    Log.d("ALLTHEVIEWS", String.valueOf(position));
    final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.setCustomAnimations(R.anim.slide_in_up, R.anim.slide_out_down);
    final SlideUserFrag fragment = new SlideUserFrag();
    FragmentList.put(position, fragment);

    fragmentTransaction.add(R.id.userProfileContainer, fragment, String.valueOf(position));

    //fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();

}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    //super.destroyItem(container, position, object);
    FragmentList.remove(position);
    container.removeView((RelativeLayout)object);

}

}

这显然是我的ViewPager适配器。我需要做的是创建一个从屏幕底部向上滑动的片段。出现的片段将填充自定义信息(字符串,图像,作品)。现在,我可以显示片段了。问题是片段显示在错误的幻灯片上或根本不显示。我认为该片段是在不了解我当前正在播放的幻灯片的情况下创建的。我几乎不知道如何解决此问题,并且无法将适配器更改为FragmentPagerAdapter之类的方法将无法工作,因为我需要能够动态创建幻灯片的功能。更确切地说,我不确定这将如何工作。我认为这可能是碎片管理问题。帮助会很棒。

0 个答案:

没有答案