Android ViewPager无法显示

时间:2015-11-03 07:06:44

标签: android android-viewpager

我有一个MainActivity,它由RecyclerView组成。当点击一个项目时,主布局被替换为被点击项目的相应片段。片段由ViewPager组成。当单击该项目时,Complete片段工作得很漂亮。当我按下后退按钮,它将转到包含recyclerview的主页面。但是当我再次按下RecyclerView中的项目时,无法看到ViewPager中的图像。添加addToBackStak(null)后,我注意到了这个问题。我已经处理了RecyclerViewAdapter.Its代码中的itemClicks也在下面给出。是没有错误显示在log.Below是ViewPager的代码:

MascotPagerAdapter.java

package com.example.sabudaniel61.ktdc;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

/**
 * Created by Sabudaniel61 on 2015-10-29.
 */
public class MascotPagerAdapter extends FragmentPagerAdapter {
    int fragmentcount=2;
    public MascotPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position){
            case 0:
                MascotImageFragment1 imageFragment1=new MascotImageFragment1();
                return imageFragment1;
            case 1:
                MascotImageFragment2 imageFragment2=new MascotImageFragment2();
                return imageFragment2;
            default:
                 imageFragment1=new MascotImageFragment1();
                return imageFragment1;
        }

    }



    @Override
    public int getCount() {
        return fragmentcount;
    }
}

MascotImageFragment1:

package com.example.sabudaniel61.ktdc;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

/**
 * Created by Sabudaniel61 on 2015-10-29.
 */
public class MascotImageFragment1 extends Fragment{
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       View v=inflater.inflate(R.layout.mascotimages1,container,false);
        ImageView imageView=(ImageView)v.findViewById(R.id.imageView1);
        return v;
    }
}

MascotImageFrgment2:

package com.example.sabudaniel61.ktdc;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

/**
 * Created by Sabudaniel61 on 2015-10-29.
 */
public class MascotImageFragment2 extends Fragment{
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.mascotimage2,container,false);
        ImageView imageView=(ImageView) v.findViewById(R.id.imageView2);
        return v;
    }
}

RecyclerAdapter:

package com.example.sabudaniel61.ktdc;

import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

/**
 * Created by Sabudaniel61 on 2015-10-15.
 */
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder>{
    HotelData[] hoteldata;public int count;View v1;MainActivity main;

    View view;
    RecyclerAdapter(HotelData[] hoteldata,MainActivity main) {

this.main=main;
        this.hoteldata = hoteldata;

    }

    @Override
    public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recyclerlayout, null);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public long getItemId(int position) {

        return super.getItemId(position);
    }

    @Override
    public void onBindViewHolder(RecyclerAdapter.ViewHolder viewHolder, int i) {
//        viewHolder.imageView.setImageResource(hoteldata[i].getImageUrl());
//        Bitmap bitmap= BitmapFactory.decodeResource(Resources.getSystem(),hoteldata[i].getImageUrl());
//        Bitmap bMapScaled = Bitmap.createScaledBitmap(bitmap, 150, 100, true);
//        viewHolder.imageView.setImageBitmap(bMapScaled);
        Picasso.with(main).load(hoteldata[i].getImageUrl()).into(viewHolder.imageView);


        viewHolder.textView1.setText(hoteldata[i].getHotel());
        viewHolder.textView2.setText(hoteldata[i].getPlace());



    }

    @Override
    public int getItemCount() {
        return hoteldata.length;

    }

    public class ViewHolder extends RecyclerView.ViewHolder  {
        TextView textView1, textView2; RecyclerView recyclerView;
        ImageView imageView;

        @Override
        public String toString() {
            return super.toString();
        }

        public ViewHolder(final View itemLayoutView) {
            super(itemLayoutView);
            v1=itemLayoutView;
            textView1 = (TextView) itemLayoutView.findViewById(R.id.textView1);
            textView2 = (TextView) itemLayoutView.findViewById(R.id.textView2);

            imageView = (ImageView) itemLayoutView.findViewById(R.id.imageView1);
//            Picasso.with(main).load(R.drawable.aryahomepage).into(imageView);
//            Picasso.with(main).load(R.drawable.bolgattyhomepage).into(imageView);
//            Picasso.with(main).load(R.drawable.mascothomepage).into(imageView);
//            Picasso.with(main).load(R.drawable.waterhomepage).into(imageView);
//            Picasso.with(main).load(R.drawable.samudrahomepage).into(imageView);
//            Picasso.with(main).load(R.drawable.goldenhomepage).into(imageView);
//            Picasso.with(main).load(R.drawable.rainhomepage).into(imageView);
//            Picasso.with(main).load(R.drawable.periyarhomepage).into(imageView);
//            Picasso.with(main).load(R.drawable.pepperhomepage).into(imageView);
//            Picasso.with(main).load(R.drawable.nandanamhomepage).into(imageView);
//            Picasso.with(main).load(R.drawable.tamarindhomepage).into(imageView);
//

            itemLayoutView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    switch (getAdapterPosition()){

                        case 2:
                            MascotFragment mascotFragment = new MascotFragment();
                            Toast.makeText(itemLayoutView.getContext(),"Position"+getAdapterPosition(),Toast.LENGTH_LONG).show();
//                            FrameLayout layout = (FrameLayout) main.findViewById(R.id.frame);
//
//                             layout.removeAllViews();
                             FragmentTransaction transaction = main.getSupportFragmentManager().beginTransaction();
                             transaction.replace(R.id.frame, mascotFragment).addToBackStack(null).commit();

                             break;
                    }
                }
            });

        }
//        public void clicks(){
//
//        v1.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                 count = getAdapterPosition();
//                if (count == 1) {
//                    Toast.makeText(v1.getContext(),"Clicked Bolgatty",Toast.LENGTH_LONG).show();
//
//                }
//
//            }});
//
//
//
//}

    }
    }

1 个答案:

答案 0 :(得分:2)

您需要借助此

删除ViewPager中所有附加的片段
getChildFragmentManager().beginTransaction().remove(mFragment1).commitAllowingStateLoss();

然后致电

 pagerAdapter.notifyDataSetChanged();

然后再次使用新片段

设置viewpagerAdapter