如何从java类中调用片段,既没有在android中扩展活动也没有片段?

时间:2015-03-24 17:51:08

标签: android android-fragments

public class PhotoGalleryExample extends FrameLayout implements SpringListener    {


 private static final int ROWS = 3;
 private static final int COLS = 2;
 private final List<ImageView> mImageViews = new ArrayList<ImageView>();
 private final List<Point> mPositions = new ArrayList<Point>();
 private final SpringChain mSpringChain = SpringChain.create();
 private final Spring mSpring = SpringSystem
  .create()
  .createSpring()
  .addListener(this)
  .setSpringConfig(SpringConfig.fromOrigamiTensionAndFriction(40, 6));
 private int mActiveIndex;
 private int mPadding;
private final Context con;
private  ImageDto idto=new ImageDto();
private int res1;
int res;
public static ImageView img;
public PhotoGalleryExample(Context context,String imgCode) {

super(context);

con=context;

int viewCount = ROWS * COLS;
    for (int i = 0; i < viewCount; i++) {
        final int j = i;

  // Create the View.
        //idto=l;
  final ImageView imageView = new ImageView(context);
  mImageViews.add(imageView);
  addView(imageView);
  imageView.setAlpha(0f);
  imageView.setBackgroundColor(Util.randomColor());
  imageView.setLayerType(LAYER_TYPE_HARDWARE, null);

  // Add an image for each view.
  Log.d("PhotoGallery i ",""+i);
  Log.d("PhotoGallery imagename  ",""+LoadImages.imgList.get(i).getImagename());

   /* res = getResources().getIdentifier(LoadImages.imgList.get(i)
                      .getImagename(), "drawable", context.getPackageName());*/
    res=getResources().getIdentifier(imgCode.toLowerCase()+"_sample_"+i, "drawable", context.getPackageName());

  imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
  imageView.setImageResource(res);

  img=imageView;

  // Add a click listener to handle scaling up the view.
  imageView.setOnClickListener(new OnClickListener() {
      @Override
    public void onClick(View v) {

        res1=getResources().getIdentifier(LoadImages.imgList.get(j).getImagename(), "drawable", con.getPackageName());
        SelectImageActivity sf= new SelectImageActivity();
        Bundle data= new Bundle();
        data.putInt("index",j);
        sf.setArguments(data);




      //////// this getFragmentManager() is showing error
        FragmentManager fr=getFragmentManager();
        FragmentTransaction ft=fr.beginTransaction();
        ft.replace(R.id.main_fragment, sf);
        ft.commit();

        Intent intent = new Intent(con, SelectImageActivity.class);
        intent.putExtra("index",j);
        con.startActivity(intent);

    }
  });

  // Add a spring to the SpringChain to do an entry animation.
  mSpringChain.addSpring(new SimpleSpringListener() {
    @Override
    public void onSpringUpdate(Spring spring) {
      render();
    }
  });
}




// Wait for layout.
getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
  @Override
  public void onGlobalLayout() {
    layout();
    getViewTreeObserver().removeOnGlobalLayoutListener(this);

    postOnAnimationDelayed(new Runnable() {
      @Override
      public void run() {
        mSpringChain.setControlSpringIndex(0).getControlSpring().setEndValue(1);
      }
    }, 500);
  }
});

 }

这是java类用于imageview动画。在任何imageview的click事件中我想打开一个片段。 但getFragmentManager()显示错误 - &gt; (无法解决错误) 请建议我如何从这个班级调用片段。

提前谢谢

2 个答案:

答案 0 :(得分:0)

您的PhotoGalleryExampleView,因此您可以将Context投射到拥有Activity,因为大部分时间Activity主机观看:

(Activity) getContext()

答案 1 :(得分:0)

另一种方法是在imageview类中实现一个接口,并在图像上点击

将回调推送到您的活动(使用此imageview的地方)