package com.mainview;
import java.util.ArrayList;
import java.util.List;
import com.example.dianwang.R;
import com.tool.FileUtil;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView.ScaleType;
public class MainFragment extends Fragment {
// 声明属性
public GridView gridView; // 用来放图片
private LayoutInflater inflater;
MainActivity guide;
ImageView maintenanceList, maintenanceApply, maintenanceRecord;
TextView txtbarcode;
Button barcode01;
private GridView mGridView;
private List<OpenClass> mAllApps;
private Context mContext;
Class[] parameterTypes = { Activity_RepairList.class, Activity_RepairApply.class, Activity_repairRecord.class };
String[] names = { "维修任务", "维修申请", "维修登记" };
int[] drawnables = new int[] { R.drawable.around_czk_icon, R.drawable.around_xbk_icon, R.drawable.around_bank_icon };
View view;
@Override
public ViewonCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated methodstub
this.inflater = inflater;
view= inflater.inflate(R.layout.main_fragment, container, false);
setupViews();
TextView title_text = (TextView) view.findViewById(R.id.title_text);
title_text.setText("维修单");
ImageView prompt_image = (ImageView) view
.findViewById(R.id.prompt_image);
prompt_image.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
getActivity().finish();
// TODO Auto-generated methodstub
}
});
// txtbarcode = (TextView)view.findViewById(R.id.txtbarcode);
// 设置gridView的adapter
return view;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// 可以根据多个请求代码来作相应的操作
if (1 == requestCode) {
Bundle b = data.getExtras(); // data为B中回传的Intent
String str = b.getString("barcode");// str即为回传的值"Hello,this is B speaking"
txtbarcode.setText(str);
// bundle.putString("barcode",obj.getText());
}
super.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (guide == null) {
guide = (MainActivity) this.getActivity();
}
}
public MainFragment() {
super();
}
public void setupViews() {
mContext = getActivity();
mGridView = (GridView) view.findViewById(R.id.gridview1);
mAllApps=new ArrayList<OpenClass>();
for (int i = 0; i < parameterTypes.length; i++) {
OpenClassopenClass = new OpenClass();
openClass.drawnableid = drawnables[i];
openClass.textName = names[i];
openClass.openClass = parameterTypes[i];
mAllApps.add(openClass);
}
mGridView.setAdapter(new GridItemAdapter(mContext, mAllApps));
mGridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public voidonItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated methodstub
OpenClassres = mAllApps.get(position);
Intentitt = new Intent();
itt.setClass(getActivity(), res.openClass); // 设置需要转换的Activity
startActivity(itt); // 开始新的Activity
}
});
}
private class GridItemAdapter extends BaseAdapter {
private List<OpenClass> resInfo;
private Context context;
public GridItemAdapter(Context context, List<OpenClass> resInfo) {
this.resInfo = resInfo;
this.context = context;
}
@Override
public int getCount() {
// TODO Auto-generated methodstub
return mAllApps.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated methodstub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated methodstub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated methodstub
if (convertView == null) {
LayoutInflaterinflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.grid_view, null);
}
ImageViewiv = (ImageView) convertView.findViewById(R.id.imageView1);
// iv.setAdjustViewBounds(true);
// iv.setMaxHeight(100);
// iv.setMinimumHeight(100);
// iv.setMaxWidth(100);
// iv.setMinimumWidth(100);
iv.setScaleType(ScaleType.FIT_CENTER);
// 修改图标大小
ViewGroup.LayoutParamspara = (ViewGroup.LayoutParams) iv.getLayoutParams();
para.height = 120;
para.width = 90;
iv.setLayoutParams(para);
TextViewtv = (TextView) convertView.findViewById(R.id.textView1);
OpenClassres = resInfo.get(position);
iv.setImageDrawable(setimg(res.drawnableid));
tv.setText(res.textName);
return convertView;
}
}
public Drawablesetimg(int resid) {
Bitmap tmpBm = new FileUtil().loadImage(getActivity(), resid);
@SuppressWarnings("deprecation")
Drawable dr = new BitmapDrawable(tmpBm);
return dr;
}
// 横竖屏切换
@Override
public voidonConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated methodstub
super.onConfigurationChanged(newConfig);
//this.getResources().getConfiguration().orientation =
//Configuration.ORIENTATION_LANDSCAPE;
}
class OpenClass {
public int drawnableid;
public String textName;
@SuppressWarnings("rawtypes")
public Class openClass;
}
}