2015-11-03 103 views
0

我有一個MainActivity,它由RecyclerView組成。當一個項目被點擊時,主要佈局被替換爲被點擊項目的相應片段。該片段由ViewPager組成。當該項目被點擊完成片段工作當我按下後退按鈕時,它會轉到由recyclerview組成的主頁面。但是當我再次按下RecyclerView中的項目時,ViewPager中的圖像無法看到。添加addToBackStak(null)後,我一直注意到這個問題。我在RecyclerViewAdapter.Its代碼也給出below.Is它沒有錯誤在log.Below顯示處理的itemClicks是ViewPager代碼:Android 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(); 
// 
//    } 
// 
//   }}); 
// 
// 
// 
//} 

    } 
    } 

回答

2

您需要刪除所有連接的片段ViewPager與此

幫助
getChildFragmentManager().beginTransaction().remove(mFragment1).commitAllowingStateLoss(); 

然後調用

pagerAdapter.notifyDataSetChanged(); 

然後用新鮮的片段設置viewpagerAdapter再次

+0

在哪一類,我應該包括這 – jobin

+0

你需要做的這個片段中的片段,如果再次恢復 –