package com.sunvote.xpadapp.fragments; import android.annotation.TargetApi; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.graphics.Color; import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.support.v4.content.ContextCompat; import android.util.DisplayMetrics; import android.util.Log; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; import com.sunvote.util.LogUtil; import com.sunvote.xpadapp.MainActivity; import com.sunvote.xpadapp.R; import com.sunvote.xpadapp.base.BaseFragment; import com.sunvote.xpadapp.db.modal.BillInfo; import com.sunvote.xpadapp.db.modal.UIType; import com.sunvote.xpadapp.db.modal.VoteResult; import com.sunvote.xpadapp.fragments.MultiContentDetailFragment.ContentVoteOnBack; import com.sunvote.xpadapp.utils.SharedPreferencesUtil; import com.sunvote.xpadcomm.XPadApi; import com.sunvote.xpadcomm.XPadApiInterface.VoteInfo; import com.ycanfunc.util.StringUtils; public class SingleTitleFragment extends BaseFragment implements ContentVoteOnBack { private TextView tvTitle; private TextView tvTips; private TextView ivReuslt; private Button btnA; private Button btnB; private Button btnC; private Button btnModify; private String[] options; private BillInfo bill; private VoteInfo voteInfo; private RelativeLayout singlevoteLayout; private RelativeLayout singlevoteTvTipsLayout; private TextView votetype; private String TAG = "SingleTitleFragment"; MultiContentDetailFragment fDetail; private int voteValue; private RelativeLayout confirmLayout; private TextView tvConfirmText; MainActivity mact; public String[] voteOptions = new String[13]; //btn一行个数 private int btnNum = 6; //自定义评测 private final int customVote=101; public void setInfo(BillInfo info) { bill = info; options = bill.billOptions.split("/"); } public void setInfo(BillInfo info, VoteInfo vote) { bill = info; if (bill == null) { bill = new BillInfo(); } voteInfo = vote; } private void initOptions() { String str = getString(R.string.zc_fd); voteOptions[0] = str; voteOptions[1] = getString(R.string.zc_fd_qq); voteOptions[2] = getString(R.string.ty_fd_qq); voteOptions[3] = getString(R.string.my_jbmy_bmy); voteOptions[4] = getString(R.string.my_jbmy_bmy_blj); voteOptions[5] = getString(R.string.my_jbmy_yb_bmy); voteOptions[6] = getString(R.string.fcmy_my_blj_bmy_fcbmy); voteOptions[7] = getString(R.string.fcty_ty_bqd_bty_fcbty); voteOptions[8] = getString(R.string.yx_cz_bcz); voteOptions[9] = getString(R.string.yx_cz_jbcz_bcz); voteOptions[10] = getString(R.string.y_l_z_c); voteOptions[11] = getString(R.string.my_bmy_fcbmy); if (voteInfo.mode == XPadApi.VoteType_Vote) { if (voteInfo.mode1_msgType == 1) { options = voteOptions[1].split("/"); } else if (voteInfo.mode1_msgType == 2) { options = voteOptions[0].split("/"); } else if (voteInfo.mode1_msgType == 3) { if(voteInfo.mode4==customVote){ //自定义评测 if(bill!=null && !StringUtils.isEmpty(bill.billOptions)){ options = bill.billOptions.split("/"); } }else { options = voteOptions[voteInfo.mode4 - 1].split("/"); } } } else if (voteInfo.mode == XPadApi.VoteType_Evaluate) { if (voteInfo.mode1_msgType > 0) { options = voteOptions[voteInfo.mode1_msgType - 1].split("/"); } }else{ options = voteOptions[1].split("/"); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.d(TAG, "onCreateView"); View view = inflater.inflate(R.layout.fragment_singlevote, container, false); singlevoteLayout=(RelativeLayout)view.findViewById(R.id.singlevote_btn_layout1); votetype = view.findViewById(R.id.votetype); initOptions(); mact = (MainActivity) getActivity(); mact.setBackgroundColor(Color.parseColor("#450603")); view.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ; } }); tvTitle = (TextView) view.findViewById(R.id.singlevote_content); singlevoteTvTipsLayout = view.findViewById(R.id.singlevote_tv_tips_layout); if (bill != null && bill.title != null && bill.title.length() > 0) { tvTitle.setText(bill.title); } if(voteInfo.mode1_msgType <= 2){ votetype.setText(R.string.yianbiaojue); } if(voteInfo.mode1_msgType > 2){ if(voteInfo.mode4 > 2){ votetype.setText(R.string.yianceping); }else{ votetype.setText(R.string.yianbiaojue); } } tvTitle.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { showMultiContentDetailFragment(); } }); ImageView iv = (ImageView) view.findViewById(R.id.singlevote_img_detail); iv.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { showMultiContentDetailFragment(); } }); if (bill != null && bill.billFile != null && bill.billFile.length() > 0 && (voteInfo != null && voteInfo.file == 1)) { iv.setVisibility(View.VISIBLE); } else { iv.setVisibility(View.INVISIBLE); } tvTips = (TextView) view.findViewById(R.id.singlevote_tv_tips); tvTips.setText(""); ivReuslt = (TextView) view.findViewById(R.id.singlevote_tv_result); ivReuslt.setVisibility(View.INVISIBLE); btnA = (Button) view.findViewById(R.id.singlevote_btnA); btnA.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (voteInfo.mode2_modify == 1) { doVoteWithIndex(1); } else { showConfirmWithValue(1); } } }); btnB = (Button) view.findViewById(R.id.singlevote_btnB); btnB.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (voteInfo.mode2_modify == 1) { doVoteWithIndex(2); } else { showConfirmWithValue(2); } } }); btnC = (Button) view.findViewById(R.id.singlevote_btnC); btnC.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (options.length == 2) { if (voteInfo.mode2_modify == 1) { doVoteWithIndex(2); } else { showConfirmWithValue(2); } } else { if (voteInfo.mode2_modify == 1) { doVoteWithIndex(3); } else { showConfirmWithValue(3); } } } }); btnModify = (Button) view.findViewById(R.id.singlevote_btn_modify); btnModify.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { btnModify.setVisibility(View.INVISIBLE); showVote(); ivReuslt.setVisibility(View.VISIBLE); } }); tvConfirmText = (TextView) view.findViewById(R.id.singlevote_confirm_textview); confirmLayout = (RelativeLayout) view.findViewById(R.id.singlevote_confirm_panel); Button btnConfirmOK = (Button) view.findViewById(R.id.singlevote_btn_confirm_ok); btnConfirmOK.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { hideConfirm(); doVoteWithIndex(voteValue); } }); Button btnConfirmCancel = (Button) view.findViewById(R.id.singlevote_btn_confirm_cancel); btnConfirmCancel.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { hideConfirm(); } }); //添加自定义评测 wutaian 2017.08.23 if(voteInfo.mode4==customVote){ if(options!=null){ setButton(options); } btnA.setVisibility(View.INVISIBLE); btnB.setVisibility(View.INVISIBLE); btnC.setVisibility(View.INVISIBLE); }else{ // options = bill.billOptions.split("/"); if (options.length == 2) { btnA.setText(options[0]); btnB.setVisibility(View.INVISIBLE); btnC.setText(options[1]); } else if (options.length == 3) { btnA.setText(options[0]); btnB.setText(options[1]); btnC.setText(options[2]); } setBtnModle(1,false); } checkIsVoted(); return view; } /** * 自定评测按钮 wutaian 2017.08.23 * @param data */ @TargetApi(Build.VERSION_CODES.JELLY_BEAN) private void setButton(String [] data){ Button btn [] = new Button[data.length]; DisplayMetrics dm = new DisplayMetrics(); getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm); int width = dm.widthPixels; int height = dm.heightPixels; int j = -1; int f = btn.length; for (int i=0;i 9){ btn[i].setTextSize(18); } btn[i].setOnClickListener(btnClick); //TextViewUtils.adjustTvTextSize(btn[i],(width-50)/btnNum,data[i].toString()); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((width-180)/btnNum, 100); //设置按钮的宽度和高度 if (i%btnNum == 0) { j++; } f--; if(f 0) { if (voteInfo != null && voteInfo.file == 1) { FragmentManager fm = getFragmentManager(); FragmentTransaction tx = fm.beginTransaction(); tx.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out); fDetail = new MultiContentDetailFragment(); fDetail.setInfo(bill, voteInfo, true,1, 1); fDetail.contentBackListener = SingleTitleFragment.this; mact.multiContentFragment = fDetail; tx.add(R.id.frame_content, fDetail, "ffDetail"); tx.addToBackStack(null); tx.commitAllowingStateLoss(); } } } private void hideConfirm() { confirmLayout.setVisibility(View.GONE); if(voteInfo.mode4==customVote){ setBtnModle(2,true); }else{ btnA.setVisibility(View.VISIBLE); if (options.length == 3) { btnB.setVisibility(View.VISIBLE); } btnC.setVisibility(View.VISIBLE); } } private void doVoteWithIndex(int index) { if (bill != null) { singlevoteTvTipsLayout.setVisibility(View.VISIBLE); tvTips.setText(getString(R.string.submiting)); bill.voteResult = index; } voteValue = index; mMainActivity.presenter.submitVote(XPadApi.AnsType_Single, String.valueOf(index)); disableVote(); showResult(); } private void showResult() { if (bill.voteResult > 0) { ivReuslt.setVisibility(View.INVISIBLE); new Handler(Looper.getMainLooper()).postDelayed(new Runnable() { @Override public void run() { if (voteInfo.mode3_secret == 0 && options != null) {// if(voteValue -1 >= 0 && voteValue - 1 < options.length) { ivReuslt.setText(options[voteValue - 1]); ivReuslt.setBackgroundResource(R.drawable.voted_empty); tvTitle.setTextColor(Color.parseColor("#B5B5B5")); } } else { ivReuslt.setText(""); ivReuslt.setBackgroundResource(R.drawable.voted); tvTitle.setTextColor(Color.parseColor("#B5B5B5")); } ivReuslt.setVisibility(View.VISIBLE); } },200); } else { ivReuslt.setVisibility(View.INVISIBLE); } } private void hideVote() { if(voteInfo.mode4==customVote){ setBtnModle(1,false); setBtnModle(2,false); }else{ btnA.setVisibility(View.INVISIBLE); btnB.setVisibility(View.INVISIBLE); btnC.setVisibility(View.INVISIBLE); } btnModify.setVisibility(View.INVISIBLE); } private void showVote() { if(voteInfo.mode4==customVote){ setBtnModle(2,true); setBtnModle(1,true); }else{ btnA.setVisibility(View.VISIBLE); if (options.length == 3) { btnB.setVisibility(View.VISIBLE); } btnC.setVisibility(View.VISIBLE); enableVote(); } ivReuslt.setVisibility(View.INVISIBLE); btnModify.setVisibility(View.INVISIBLE); } private void showModify() { hideVote(); btnModify.setVisibility(View.VISIBLE); // ivReuslt.setVisibility(View.VISIBLE); } private void enableVote(){ if(voteInfo.mode4==customVote){ setBtnModle(2,true); }else{ btnA.setEnabled(true); btnB.setEnabled(true); btnC.setEnabled(true); } } private void disableVote() { // if(voteInfo.mode4==customVote){ // setBtnModle(2,false); // }else{ // btnA.setEnabled(false); // btnB.setEnabled(false); // btnC.setEnabled(false); // } hideVote(); } private void showModifyOrDisable() { if (bill.voteResult > 0) { if (voteInfo.mode2_modify == 1) { showModify(); singlevoteTvTipsLayout.setVisibility(View.VISIBLE); // tvTips.setText(getString(R.string.submited)); tvTips.setText(""); } else { disableVote(); singlevoteTvTipsLayout.setVisibility(View.VISIBLE); tvTips.setText(""); } } } private void closeDetail() { if (fDetail != null) { if(fDetail.isClose){ FragmentManager fm = getFragmentManager(); FragmentTransaction tx = fm.beginTransaction(); tx.remove(fDetail); tx.commit(); //tx.commitAllowingStateLoss(); } } } /* @Override public void onVoteEvent(VoteInfo info) { if (info.mode == XPadApi.VoteType_Stop) { hideVote(); saveVoteInfo(); } else { voteInfo = (VoteInfo) info; if (voteInfo.mode2_modify == 1) { tvTips.setText(getString(R.string.please_vote_can_modify)); } else { tvTips.setText(getString(R.string.please_vote_can_not_modify)); } showVote(); checkIsVoted(); } } */ @Override public void onVoteSubmitSuccess() { super.onVoteSubmitSuccess(); showModifyOrDisable(); if(voteValue > 0){ saveVoteInfo(); } } @Override public void onVoteSubmitError() { super.onVoteSubmitError(); showVote(); } private void saveVoteInfo(){ SharedPreferencesUtil.saveData(getActivity(),DATAPOS,Integer.valueOf(voteInfo.dataPos)); SharedPreferencesUtil.saveData(getActivity(),VOTEMODE,Integer.valueOf(voteInfo.mode)); SharedPreferencesUtil.saveData(getActivity(),VOTEVALUE,Integer.valueOf(voteValue)); } private void checkIsVoted(){ Log.i(TAG, "checkIsVoted: "); Integer datapos = (Integer) SharedPreferencesUtil.getData(getActivity(),DATAPOS,Integer.valueOf(-1)); Integer votemode = (Integer) SharedPreferencesUtil.getData(getActivity(),VOTEMODE,Integer.valueOf(-1)); Integer val = (Integer) SharedPreferencesUtil.getData(getActivity(),VOTEVALUE,Integer.valueOf(-1)); Log.i(TAG, "checkIsVoted: datapos:"+ datapos.intValue() +" voteMode:"+ votemode.intValue()+ " val:"+val.intValue()); if(datapos.intValue() == voteInfo.dataPos && votemode.intValue() == voteInfo.mode && val.intValue()!=-1){ voteValue = val.intValue(); if (bill != null) { bill.voteResult = voteValue; } showResult(); showModifyOrDisable(); //tvTips.setText(getString(R.string.submited)); } } @Override public void onBack(VoteResult result) { // TODO Auto-generated method stub voteValue = result.selectResult; mMainActivity.multiContentFragment = null; showResult(); showModifyOrDisable(); } }