将活动更改为片段

时间:2014-12-08 13:11:26

标签: android android-fragments

此图库活动工作正常。我需要将活动作为片段。我试图将活动更改为片段但幻灯片不起作用。有人帮我改变

public class Cast_Activity extends Activity {

    private ViewFlipper myViewFlipper;
    private float initialXPoint;
    int[] image = {  R.drawable.istill_16,R.drawable.istill_15,
            R.drawable.istill_13,R.drawable.istill_14,
            R.drawable.istill_12,R.drawable.istill_11,
            R.drawable.istill_9,R.drawable.istill_10, };

    @Override
    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.slide_show);
    myViewFlipper = (ViewFlipper) findViewById(R.id.myflipper);

    for (int i = 0; i < image.length; i++) {
        ImageView imageView = new ImageView(Cast_Activity.this);
        imageView.setImageResource(image[i]);
        myViewFlipper.addView(imageView);
    }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        initialXPoint = event.getX();
        break;
    case MotionEvent.ACTION_UP:
        float finalx = event.getX();
        if (initialXPoint > finalx) {
            if (myViewFlipper.getDisplayedChild() == image.length)
                break;
            myViewFlipper.showNext();
        } else {
            if (myViewFlipper.getDisplayedChild() == 0)
                break;
            myViewFlipper.showPrevious();
        }
        break;
    }
    return false;
    }
}

这是我改变后的片段

public class Cast_Activity extends Fragment {

    private ViewFlipper myViewFlipper;
    private float initialXPoint;
    int[] image = {  R.drawable.istill_16,R.drawable.istill_15,
            R.drawable.istill_13,R.drawable.istill_14,
            R.drawable.istill_12,R.drawable.istill_11,
            R.drawable.istill_9,R.drawable.istill_10, };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        LinearLayout ll = (LinearLayout) inflater.inflate(R.layout.slide_show, container, false);
    myViewFlipper = (ViewFlipper) ll.findViewById(R.id.myflipper);

    for (int i = 0; i < image.length; i++) {
        ImageView imageView = new ImageView(getActivity());
        imageView.setImageResource(image[i]);
        myViewFlipper.addView(imageView);
    }
    return ll;
    }

    public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        initialXPoint = event.getX();
        break;
    case MotionEvent.ACTION_UP:
        float finalx = event.getX();
        if (initialXPoint > finalx) {
            if (myViewFlipper.getDisplayedChild() == image.length)
                break;
            myViewFlipper.showNext();
        } else {
            if (myViewFlipper.getDisplayedChild() == 0)
                break;
            myViewFlipper.showPrevious();
        }
        break;
    }
    return false;
    }

更新代码

public class Cast_Activity extends Fragment {


    private ViewFlipper myViewFlipper;
    private float initialXPoint;
    int[] image = {  R.drawable.istill_16,R.drawable.istill_15,
            R.drawable.istill_13,R.drawable.istill_14,
            R.drawable.istill_12,R.drawable.istill_11,
            R.drawable.istill_9,R.drawable.istill_10, };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

       View v = inflater.inflate(R.layout.slide_show, container, false);
       myViewFlipper = (ViewFlipper) v.findViewById(R.id.myflipper);

   for (int i = 0; i < image.length; i++) {
       ImageView imageView = new ImageView(getActivity());
       imageView.setImageResource(image[i]);
       myViewFlipper.addView(imageView);
   }
   return v;
   }

    public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        initialXPoint = event.getX();
        break;
    case MotionEvent.ACTION_UP:
        float finalx = event.getX();
        if (initialXPoint > finalx) {
            if (myViewFlipper.getDisplayedChild() == image.length)
                break;
            myViewFlipper.showNext();
        } else {
            if (myViewFlipper.getDisplayedChild() == 0)
                break;
            myViewFlipper.showPrevious();
        }
        break;
    }
    return false;
    }

3 个答案:

答案 0 :(得分:2)

更改

LinearLayout ll = (LinearLayout) inflater.inflate(R.layout.slide_show, container, false);
myViewFlipper = (ViewFlipper) ll.findViewById(R.id.myflipper);

View v = inflater.inflate(R.layout.slide_show, container, false);
myViewFlipper = (ViewFlipper)v.findViewById(R.id.myflipper);
return v;

结束时

onCreateView(...)

更正代码:

  @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.slide_show, container, false);
    myViewFlipper = (ViewFlipper) v.findViewById(R.id.myflipper);

for (int i = 0; i < image.length; i++) {
    ImageView imageView = new ImageView(getActivity());
    imageView.setImageResource(image[i]);
    myViewFlipper.addView(imageView);
}
return v;
}

答案 1 :(得分:0)

  

对触摸侦听器进行必要的更改以进行布局更改

public class Cast_Activity extends Fragment {
  

public class Cast_Activity extends Fragment implement android.view.View.OnTouchListener{
....
onActivityCreated(){
  myViewFlipper.setOnTouchListener(this);
}

答案 2 :(得分:0)

最后我找到了答案

myViewFlipper = (ViewFlipper) v.findViewById(R.id.myflipper);

       myViewFlipper.setOnTouchListener(new android.view.View.OnTouchListener() {

           @Override
           public boolean onTouch(View v, MotionEvent event) {
               OnTouchListener(event);
               return true;
           }


      });


   for (int i = 0; i < image.length; i++) {
       ImageView imageView = new ImageView(getActivity());
       imageView.setImageResource(image[i]);
       myViewFlipper.addView(imageView);
        }

   return v;

   }

    public boolean OnTouchListener(MotionEvent event) {
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        initialXPoint = event.getX();
        break;
    case MotionEvent.ACTION_UP:
        float finalx = event.getX();
        if (initialXPoint > finalx) {
            if (myViewFlipper.getDisplayedChild() == image.length)
                break;
            myViewFlipper.showNext();


        } else {
            if (myViewFlipper.getDisplayedChild() == 0)
                break;
            myViewFlipper.showPrevious();
        }
        break;
    }
    return false;
    }