Android的 - 片段之间的通信 - 访问片段里上课? [英] android - Communicating between fragments - accessing class inside fragment?

查看:110
本文介绍了Android的 - 片段之间的通信 - 访问片段里上课?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Android程序的两个片段之间的通信。我一直在关注这个指南与其他片段沟通,然后一切都会好起来好为止。我有一个片段发送数据到其主机的活动,该活动正确地承认通过日志报表中的数据。我还成功地引用在活动范围内的第二个片段,和我所有的设置将此信息发送到另一片段,但其中存在的问题;在第二个片段,我需要从主机活动调用该方法,是该片段的内部另一个类的内部,并且更重要的是,该方法依赖于该片段的内部定义的数据。我试图直接调用该方法,但我似乎无法做到这一点。有没有让过去这些限制的一些方法?

I am attempting to communicate between two fragments in an android program. I've been following this guide Communicating with Other Fragments and everything is going well so far. I have the one fragment sending data to its host activity, and the activity correctly acknowledges the data through Log statements. I have also successfully referenced the second fragment within the activity, and am all set to send this information to another fragment, but therein lies the problem; the method in the second fragment, which I need to call from the host activity, is inside of another class inside of the fragment, and what's more, that method relies on data which is defined inside of the fragment. I've tried to call the method directly, but I cannot seem to do it. Is there some way of getting past these limitations?

主机活动:

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);
    }

}

片段code:

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;
        }
    }
}

感谢您的帮助,您可以给!

Thanks for any help you can give!

推荐答案

您需要在您的片段的方法,你从活动中调用,然后将片段方法能够将它传递给视图。

You'll need to have a method in your fragment that you call from the activity, and then the fragment method can pass it on to the view.

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简称片段的领域需要。

If you have a lot of such methods that you want to call, and it gets tedious to write all of these functions, you could consider moving some of your DrawView fields up to DrawingFragment, and then DrawingFragment could do the actual work, while DrawView referred to the fragment's fields as needed.

这篇关于Android的 - 片段之间的通信 - 访问片段里上课?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆