如何从DialogFragment更改ImageView的图像

时间:2014-03-18 11:19:50

标签: android android-fragments android-imageview android-dialogfragment

所以基本上我有一个Fragment类,我有一个ImageView,我还没有设置任何图像。所以我想从一个对话框中执行此操作,其中我有列表,并且单击任何项​​目以在ImageView所在的片段中添加图像。图像是在xml文件中生成的矩形。如果我从它的类中添加Fragment类就可以了。但我需要它从对话框添加它。

我整天尝试过这一切,没有任何效果,我得到的只是nullPointerException,在同一个地方,我尝试以我尝试过的方式调用图像的添加。

这是我经常尝试后得到的代码:)。

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.ImageView;

public class BrickMessageBoxGreen extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.dialog_title_green);
        builder.setItems(R.array.greenBricks_array,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // The 'which' argument contains the index position
                        // of the selected item
                        Arkitektur ark = new Arkitektur();
                        ImageView img = (ImageView)ark.getView().findViewById(R.id.green_brick);
                        ark.addGreenBrick(img);
                    }
                });
        return builder.create();
    }
}

这是我有ImageView的课程:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.DragEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnDragListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class Arkitektur extends Fragment implements OnTouchListener, OnDragListener {

    private int _xDelta;
    private int _yDelta;
    ViewGroup _root;
    private View arkitektur;

    ImageView greenBrick;
    ImageView redBrick;
    ImageView yellowBrick;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        arkitektur = inflater.inflate(R.layout.arkitektur, container, false);
        //arkitektur.findViewById(R.id.green_brick).setOnTouchListener(this);
        //greenBrick = (ImageView) arkitektur.findViewById(R.id.green_brick);
        //arkitektur.findViewById(R.id.root).setOnDragListener(this);
        //arkitektur.findViewById(R.id.bottom_container).setOnDragListener(this);

        //addGreenBrick();

        _root = (ViewGroup) arkitektur.findViewById(R.id.root);
        _root.setOnDragListener(this);

        return arkitektur;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        final int X = (int) event.getRawX()*2;
        final int Y = (int) event.getRawY()*2;
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
                _xDelta = X - lParams.leftMargin;
                _yDelta = Y - lParams.topMargin;
                break;
            case MotionEvent.ACTION_UP:
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                break;
            case MotionEvent.ACTION_POINTER_UP:
                break;
            case MotionEvent.ACTION_MOVE:
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
                layoutParams.leftMargin = X - _xDelta;
                layoutParams.topMargin = Y - _yDelta;
//              layoutParams.rightMargin = -250;
//              layoutParams.bottomMargin = -250;
                v.setLayoutParams(layoutParams);
                break;
        }
        _root.invalidate();
        return true;
    }

    @Override
    public boolean onDrag(View v, DragEvent e) {
        if (e.getAction()==DragEvent.ACTION_DROP) {
            View view = (View) e.getLocalState();
            ViewGroup from = (ViewGroup) view.getParent();
            from.removeView(view);
            LinearLayout to = (LinearLayout) v;
            to.addView(view);
            view.setVisibility(View.VISIBLE);
        }
        return true;
    }

    public void addGreenBrick(View v)
    {
        v.findViewById(R.id.green_brick).setOnTouchListener(this);
        greenBrick = (ImageView) v.findViewById(R.id.green_brick);
        greenBrick.setImageResource(R.drawable.green_brick);
    }
}

谢谢!

1 个答案:

答案 0 :(得分:0)

我同意Tamilselvan Kalimuthu,让你的片段实现一个监听器并将其设置为BrickMessageBoxGreen类的属性。

修改

添加了一些代码来澄清这个想法。

BrickMessageBoxGreen类:

public OnClickDialogListener listener //Attribute of your class

public void setDialogListener(OnClickDialogListener  l){ //Setting the listener
    this.listener = l;
}


new DialogInterface.OnClickListener() { //Changing onClick method
                    public void onClick(DialogInterface dialog, int which) {
                        // The 'which' argument contains the index position
                        // of the selected item
            this.listener.onDialogClicked("green");
                    }
                });

Arkitektur班:

public interface OnClickDialogListener { //Your listener definition, you could place it elsewhere
    void onDialogClicked(String colorName);
}

public class Arkitektur extends Fragment implements OnTouchListener, OnDragListener, OnClickDialogListener  { //Implementing your listener interface

public void onDialogClicked(String colorName){//Method implementation
if(colorName.equals("green")){
    addGreenBrick();
}else if(colorName.equals("red")){
    addRedBrick();
}else if(colorName.equals("yellor")){
    addYellowBrick();
}
}