如何在pageradapter中完成活动?

时间:2014-10-29 20:30:26

标签: android android-pageradapter

我使用pageradapter,其中我有一个调用其他活动的按钮。我可以在调用secondsetivity后完成pageractivity。我的pageradapter代码如下。方法finish()未定义类型new View.OnClickListener(){} 感谢。

public class ImageAdapterFromRes extends PagerAdapter {


    Context context;
    ArrayList<String> textArray;
    ArrayList<String> urlArray;
    ArrayList<Bitmap> bitmapArray;
    ImageView imageView;
    TextView textreklama1;
    Button btnZoznam;
    public Activity activity;

    private int[] GalImages = new int[] { 
            //Images from resource folder.
        R.drawable.one,  
        R.drawable.two,
        R.drawable.three
    };

    ImageAdapterFromRes(Context context, ArrayList<String> textArray, ArrayList<String> urlArray, ArrayList<Bitmap> bitmapArray){
        this.context=context;
        this.textArray=textArray;
        this.urlArray=urlArray;
        this.bitmapArray=bitmapArray;
    }

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

    public Object instantiateItem(ViewGroup collection, int position) {
         LayoutInflater inflater = (LayoutInflater) collection.getContext()
         .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

         View view = inflater.inflate(R.layout.reklamator_new, null);

         imageView = (ImageView) view.findViewById(R.id.imgreklama1);
         imageView.setImageBitmap(bitmapArray.get(position));

         textreklama1 = (TextView) view.findViewById(R.id.textreklama1);
         textreklama1.setText(textArray.get(position).toString());

         btnZoznam = (Button) view.findViewById(R.id.btnZoznam);
         btnZoznam.setOnClickListener(new View.OnClickListener() {

             @Override
             public void onClick(View v) {

                 Intent i = new Intent(v.getContext(), SecondActivity.class);
                 Bundle extras = new Bundle();
                 extras.putString("cat", "1");
                 i.putExtras(extras);
                 context.startActivity(i);
                 //finish(); ???????? HOW FINISH ?

             }
         });

         ((ViewGroup) collection).addView(view, 0);
         return view;
         }

         @Override
         public void destroyItem(ViewGroup arg0, int arg1, Object arg2) {
         ((ViewGroup) arg0).removeView((View) arg2);
         }

         @Override
         public boolean isViewFromObject(View arg0, Object arg1) {
         return arg0 == ((View) arg1);
         }


}

2 个答案:

答案 0 :(得分:2)

您可以使用演员表完成活动:

((Activity) context).finish();

答案 1 :(得分:1)

我建议活动处理它的作用(你可以在以后将它放在一个片段中)。

您可以简单地((Activity) context).finish(),但我倾向于favour composition over aggregation

这是一个快速摘录:

public class ImageAdapterFromRes extends PagerAdapter {

    OnPagerItemSelected mListener;
    Context context;

    ImageAdapterFromRes(Context context, ArrayList<String> textArray, ArrayList<String> urlArray, ArrayList<Bitmap> bitmapArray,
        OnPagerItemSelected listener
        ){

        this.context=context;
        this.textArray=textArray;
        this.urlArray=urlArray;
        this.bitmapArray=bitmapArray;

        this.mListener = listener;
    }

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

    public Object instantiateItem(ViewGroup collection, int position) {

         btnZoznam.setOnClickListener(new View.OnClickListener() {

             @Override
             public void onClick(View v) {

                 Intent i = new Intent(v.getContext(), SecondActivity.class);
                 Bundle extras = new Bundle();
                 extras.putString("cat", "1");
                 i.putExtras(extras);
                 context.startActivity(i);
                 //finish(); ???????? HOW FINISH ?

                 mListener.pagerItemSelected();
             }
         });
     }

     public interface OnPagerItemSelected {
         void pagerItemSelected();
     }


}

注意有一个监听器,单击一个项目时会调用pagerItemSelected()方法。

这意味着您可以对您的活动(或片段或其他组件)执行此操作:

public class MyActivity extends Activity implements OnPagerItemSelected {

    void setUpPager() {
        new ImageAdapterFromRes(this, ..., ..., ..., this);
    }

    void pagerItemSelected() {
        finish();
    }

}