android - 在片段之间进行通信 - 访问片段内的类?

时间:2014-12-01 22:50:54

标签: android android-fragments

我正在尝试在android程序中的两个片段之间进行通信。我一直在关注本指南Communicating with Other Fragments,到目前为止一切顺利。我有一个片段将数据发送到其主机活动,并且活动通过Log语句正确地确认数据。我还成功引用了活动中的第二个片段,并且都设置为将此信息发送到另一个片段,但其中存在问题;我需要从主机活动调用的第二个片段中的方法位于片段内部的另一个类中,而且该方法依赖于片段内部定义的数据。我试图直接调用这个方法,但我似乎无法做到。有没有办法克服这些限制?

主持人活动:

package com.example.chris.drawingtest;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;


public class DrawingActivity extends Activity
    implements ToolbarFragment.ToolSelectionListener {

    public void sendNewValue(int newValue) {
        Log.d("Data received from Toolbar Fragment: ", "The ID of the button pressed is " + newValue);
        DrawingFragment drawFrag = (DrawingFragment)getFragmentManager().findFragmentById(R.id.Drawing);

        if (drawFrag != null) {
            //changeTool (part of DrawView) This is where I want to call the method
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drawing);
    }

}

片段代码:

package com.example.chris.drawingtest;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.example.chris.drawingtest.R;

import java.text.AttributedCharacterIterator;

/**
 * Created by Chris on 11/28/2014.
 */
public class DrawingFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.d("onCreateView: ","This ran successfully");
        View v = inflater.inflate(R.layout.fragment_drawing, container, false);
        DrawView drawView = new DrawView(v.getContext());
        return v;
    }

    public static class DrawView extends View {

        private Path drawpath = new Path();
        private Paint drawpaint = new Paint();
        private Paint canvaspaint;
        private Canvas drawcanvas;
        private Bitmap canvasBitmap;

        private int paintColor = 0xFF000000;    //opaque black for pencil
        private int canvasColor = 0xFFFFFFFF;   //pure white for canvas

        public DrawView(Context context) {
            super(context);

            Log.d("DrawView: ", "method is called");

            drawpaint.setColor(paintColor);

            drawpaint.setStrokeWidth(5);
            drawpaint.setStyle(Paint.Style.STROKE);
            drawpaint.setStrokeJoin(Paint.Join.ROUND);
            drawpaint.setStrokeCap(Paint.Cap.ROUND);

            canvaspaint = new Paint(Paint.DITHER_FLAG);

            Point p = getScreenSize(context);
            int w = p.x;
            int h = p.y;

            onSizeChanged(w,h,0,0);

        }

        public DrawView(Context context, AttributeSet attrs) {
            super(context);

            Log.d("DrawView: ", "method is called");

            drawpaint.setColor(paintColor);

            drawpaint.setStrokeWidth(20);
            drawpaint.setStyle(Paint.Style.STROKE);
            drawpaint.setStrokeJoin(Paint.Join.ROUND);
            drawpaint.setStrokeCap(Paint.Cap.ROUND);

            canvaspaint = new Paint(Paint.DITHER_FLAG);

            Point p = getScreenSize(context);
            int w = p.x;
            int h = p.y;

            onSizeChanged(w,h,0,0);

        }

        public DrawView(Context context, AttributeSet attrs, int defStyle) {
            super(context);

            Log.d("DrawView: ", "method is called");

            drawpaint.setColor(paintColor);

            drawpaint.setStrokeWidth(10);
            drawpaint.setStyle(Paint.Style.STROKE);
            drawpaint.setStrokeJoin(Paint.Join.ROUND);
            drawpaint.setStrokeCap(Paint.Cap.ROUND);

            canvaspaint = new Paint(Paint.DITHER_FLAG);

            Point p = getScreenSize(context);
            int w = p.x;
            int h = p.y;

            onSizeChanged(w,h,0,0);

        }

// ------------------------ the method that I need to call ----------------------

        public boolean changeTool(int toolCode){
            switch(toolCode){
                case 0:
                    drawpaint.setColor(paintColor);
                    break;
                case 1:
                    drawpaint.setColor(canvasColor);
                    break;
                default:
                    return false;
            }
            return true;
        }

// ------------------------ the method that I need to call ----------------------


        protected Point getScreenSize(Context context) {
            WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            Display display = wm.getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            return size;
        }

        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w,h,oldw,oldh);

            Log.d("OnSizeChanged: ", "method is called");

            canvasBitmap = Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888);
            drawcanvas = new Canvas(canvasBitmap);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);

            Log.d("onDraw: ", "method is called");

            canvas.drawBitmap(canvasBitmap,0,0,canvaspaint);
            canvas.drawPath(drawpath, drawpaint);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            float touchX = event.getX();
            float touchY = event.getY();

            Log.d("onTouchEvent: ", "method is called");

            switch(event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    drawpath.moveTo(touchX,touchY);
                    break;
                case MotionEvent.ACTION_MOVE:
                    drawpath.lineTo(touchX,touchY);
                    break;
                case MotionEvent.ACTION_UP:
                    drawcanvas.drawPath(drawpath, drawpaint);
                    drawpath.reset();
                    break;
                default:
                    return false;
            }
            invalidate();
            return true;
        }
    }
}

感谢您提供任何帮助!

1 个答案:

答案 0 :(得分:1)

您需要在片段中创建一个从活动中调用的方法,然后片段方法可以将其传递给视图。

public class DrawingFragment extends Fragment {

    DrawView drawView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.d("onCreateView: ","This ran successfully");
        View v = inflater.inflate(R.layout.fragment_drawing, container, false);
        drawView = new DrawView(v.getContext());
        return v;
    }

    public boolean changeTool(int toolCode){
        if (drawView != null) {
            drawView.changeTool(toolCode);
        }
    }

如果你想要调用很多这样的方法,并且编写所有这些函数变得乏味,你可以考虑将一些DrawView字段移到DrawingFragment,然后DrawingFragment可以完成实际的工作,而DrawView根据需要引用了片段的字段。