.setImageResource()不起作用

时间:2016-07-31 17:37:53

标签: android onclick imageview fragment android-resources

我想在点击后动态更改drawable ImageView。所以我写了下面的代码。所有其他功能都在工作,包括setImageResource没有任何错误,但没有在UI中显示任何更改。我也试过打电话给setImageDrawable,但它也无法正常工作......请帮帮我

package com.example.runwaylabz.canvi;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class BlankFragment extends Fragment implements View.OnClickListener {
private String title;
private int page;
private static ImageView icon1, icon2, icon3, icon4, icon5, icon6, icon7, icon8;
boolean icon1boolean, icon2boolean, icon3boolean, icon4boolean, icon5boolean, icon6boolean, icon7boolean, icon8boolean;
private static String ip = "192.168.4.1";
private static int port = 23;
private static SocketConnection socketConnection;


public static BlankFragment newInstance(int page, String title) {
    BlankFragment blankFragment = new BlankFragment();
    Bundle args = new Bundle();
    args.putInt("ind", page);
    args.putString("tit", title);
    blankFragment.setArguments(args);
    socketConnection = SocketConnection.getInstance(ip, port);
    return blankFragment;
}


@Override
public void onCreate(@Nullable Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    page = getArguments().getInt("ind", 0);
    title = getArguments().getString("tit", "Add");


}

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

    getActivity().startService(new Intent(getActivity(), socketConnection.getClass()));

    View v = inflater.inflate(R.layout.fragment_blank, container, false);

    set_icon(v);

    return v;
}

@Override
public void onDestroyView() {

    getActivity().stopService(new Intent(getActivity(), socketConnection.getClass()));

    super.onDestroyView();
}


@Override
public void onClick(View view) {
    if (view.getTag().equals("icon")) {
        icon_click_switch(view);
    }

}


private void set_icon(View v) {

    icon1 = (ImageView) v.findViewById(R.id.icon1);
    icon2 = (ImageView) v.findViewById(R.id.icon2);
    icon3 = (ImageView) v.findViewById(R.id.icon3);
    icon4 = (ImageView) v.findViewById(R.id.icon4);
    icon5 = (ImageView) v.findViewById(R.id.icon5);
    icon6 = (ImageView) v.findViewById(R.id.icon6);
    icon7 = (ImageView) v.findViewById(R.id.icon7);
    icon8 = (ImageView) v.findViewById(R.id.icon8);
    icon1.setOnClickListener(this);
    icon2.setOnClickListener(this);
    icon3.setOnClickListener(this);
    icon4.setOnClickListener(this);
    icon5.setOnClickListener(this);
    icon6.setOnClickListener(this);
    icon7.setOnClickListener(this);
    icon8.setOnClickListener(this);
    icon1.setTag("icon");
    icon2.setTag("icon");
    icon3.setTag("icon");
    icon4.setTag("icon");
    icon5.setTag("icon");
    icon6.setTag("icon");
    icon7.setTag("icon");
    icon8.setTag("icon");

}

private void icon_click_switch(View view) {

    switch (view.getId()) {

        case R.id.icon1:
            if (!icon1boolean) {
                icon1.setImageResource(R.drawable.poweron);
                socketConnection.writeouputstream(getString(R.string.icon1on));
                icon1boolean = true;
            } else {
                icon1.setImageResource(R.drawable.power);
                socketConnection.writeouputstream(getString(R.string.icon1off));
                icon1boolean = false;
            }
            break;
        case R.id.icon2:
            if (!icon2boolean) {
                icon2.setImageResource(R.drawable.poweron);
                socketConnection.writeouputstream(getString(R.string.icon2on));
                icon2boolean = true;
            } else {
                icon2.setImageResource(R.drawable.power);
                socketConnection.writeouputstream(getString(R.string.icon2off));
                icon2boolean = false;
            }
            break;
        case R.id.icon3:
            if (!icon3boolean) {
                icon3.setImageResource(R.drawable.poweron);
                socketConnection.writeouputstream(getString(R.string.icon3on));
                icon3boolean = true;
            } else {
                icon3.setImageResource(R.drawable.power);
                socketConnection.writeouputstream(getString(R.string.icon3off));
                icon3boolean = false;
            }
            break;
        case R.id.icon4:
            if (!icon4boolean) {
                icon4.setImageResource(R.drawable.poweron);
                socketConnection.writeouputstream(getString(R.string.icon4on));
                icon4boolean = true;
            } else {
                icon4.setImageResource(R.drawable.power);
                socketConnection.writeouputstream(getString(R.string.icon4off));
                icon4boolean = false;
            }
            break;
        case R.id.icon5:
            if (!icon5boolean) {
                icon5.setImageResource(R.drawable.poweron);
                socketConnection.writeouputstream(getString(R.string.icon5on));
                icon5boolean = true;
            } else {
                icon5.setImageResource(R.drawable.power);
                socketConnection.writeouputstream(getString(R.string.icon5off));
                icon5boolean = false;
            }
            break;
        case R.id.icon6:
            if (!icon6boolean) {
                icon6.setImageResource(R.drawable.poweron);
                socketConnection.writeouputstream(getString(R.string.icon6on));
                icon6boolean = true;
            } else {
                icon6.setImageResource(R.drawable.power);
                socketConnection.writeouputstream(getString(R.string.icon6off));
                icon6boolean = false;
            }
            break;
        case R.id.icon7:
            if (!icon7boolean) {
                icon7.setImageResource(R.drawable.poweron);
                socketConnection.writeouputstream(getString(R.string.icon7on));
                icon7boolean = true;
            } else {
                icon7.setImageResource(R.drawable.power);
                socketConnection.writeouputstream(getString(R.string.icon7off));
                icon7boolean = false;
            }
            break;
        case R.id.icon8:
            if (!icon8boolean) {
                icon8.setImageResource(R.drawable.poweron);
                socketConnection.writeouputstream(getString(R.string.icon8on));
                icon8boolean = true;
            } else {
                icon8.setImageResource(R.drawable.power);
                socketConnection.writeouputstream(getString(R.string.icon8off));
                icon8boolean = false;
            }
            break;
    }
  }
} 

0 个答案:

没有答案