編輯:關於Android編程
public class MyActivity extends Activity implements AdapterView.OnItemClickListener { private GridView gvOperatingTable; private GridAdapter mGridAdapter; private ArrayListmList = new ArrayList<>(); private Context mContext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_operating_table); mContext = this; initGridItemData(); //設置Grid Item數據 gvOperatingTable = (GridView) findViewById(R.id.gv_operating_table); mGridAdapter = new GridAdapter(mContext, mList); gvOperatingTable.setAdapter(mGridAdapter); gvOperatingTable.setOnItemClickListener(this); } /** * descripyion:初始化GridView Item參數:圖片,描述,以及跳轉的Activity * param null * return null */ private void initGridItemData() { mList.clear(); mList.add(new GridContentItem(R.drawable.ic_alarm_major, "發電參數配置", MainActivity.class)); mList.add(new GridContentItem(R.drawable.ic_alarm_major, "網絡配置", MainActivity.class)); mList.add(new GridContentItem(R.drawable.ic_alarm_major, "管理系統配置", MainActivity.class)); mList.add(new GridContentItem(R.drawable.ic_alarm_major, "設備維護", MainActivity.class)); mList.add(new GridContentItem(R.drawable.ic_alarm_major, "日志管理", MainActivity.class)); mList.add(new GridContentItem(R.drawable.ic_alarm_major, "設備信息", MainActivity.class)); } @Override public void onItemClick(AdapterView parent, View view, int position, long id) { Intent mIntent = new Intent(); GridContentItem item = (GridContentItem) gvOperatingTable.getItemAtPosition(position); Class activityCls = item.getActivityClass(); //獲取跳轉的界面類 mIntent.setClass(mContext, activityCls); startActivity(mIntent); } }
/** * Description: add adapter for Operating Table */ public class GridAdapter extends BaseAdapter{ private Context mContext; /** 存儲item內容,圖片id、名稱、響應activity */ private ArrayListmList; private LayoutInflater inflater; public OperatingTableGridAdapter(Context context, ArrayList list){ mContext = context; mList = list; inflater = LayoutInflater.from(context); } @Override public int getCount() { return mList.size(); } @Override public Object getItem(int position) { return mList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View view, ViewGroup parent) { ViewHolder holder = null; if(view == null){ view = inflater.inflate(R.layout.operating_table_item, null); holder = new ViewHolder(); holder.logo = (ImageView) view.findViewById(R.id.iv_item_image); holder.name = (TextView)view.findViewById(R.id.tv_item_desc); view.setTag(holder); } else{ holder = (ViewHolder)view.getTag(); } //設置每個Item的寬度和高度 AbsListView.LayoutParams params = new AbsListView.LayoutParams( android.view.ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); view.setLayoutParams(params); GridContentItem item = mList.get(position); //獲取到該Item實例 //設置Item圖片和名字 holder.logo.setImageResource(item.getItemImgId()); holder.name.setText(item.getItemName()); return view; } private static class ViewHolder{ /** 圖標 */ ImageView logo; /** 名稱 */ TextView name; } }
/** * Description: add adapter for Operating Table */ public class GridAdapter extends BaseAdapter{ private Context mContext; /** 存儲item內容,圖片id、名稱、響應activity */ private ArrayListmList; private LayoutInflater inflater; public OperatingTableGridAdapter(Context context, ArrayList list){ mContext = context; mList = list; inflater = LayoutInflater.from(context); } @Override public int getCount() { return mList.size(); } @Override public Object getItem(int position) { return mList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View view, ViewGroup parent) { ViewHolder holder = null; if(view == null){ view = inflater.inflate(R.layout.my_gridview_item, null); holder = new ViewHolder(); holder.logo = (ImageView) view.findViewById(R.id.iv_item_image); holder.name = (TextView)view.findViewById(R.id.tv_item_desc); view.setTag(holder); } else{ holder = (ViewHolder)view.getTag(); } //設置每個Item的寬度和高度 AbsListView.LayoutParams params = new AbsListView.LayoutParams( android.view.ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); view.setLayoutParams(params); GridContentItem item = mList.get(position); //獲取到該Item實例 //設置Item圖片和名字 holder.logo.setImageResource(item.getItemImgId()); holder.name.setText(item.getItemName()); return view; } private static class ViewHolder{ /** 圖標 */ ImageView logo; /** 名稱 */ TextView name; } }
初次用到回調是在Fragment和Activity之間進行通信的時候,當時感覺很難理解,但又覺得這個方法使用起來很方便,所以對它進行仔細的研究。發現回調不僅僅是實現功能那
View的Draw時序圖前面幾篇通過對View樹的measure和layout過程分析事,接下來將結合前兩步得到的測量值及在視圖中的位位置,開始進行繪制操作,一步比一步復
今天這篇文章教給大家解決這幾個問題:1、如何通過AndroidStudio的git上傳項目到github2、如何通過AndroidStudio的git將存在的項目增加的類
Fragment 介紹: 每個Fragment都是一個獨立的模塊,並與它所綁定的Activity精密聯系在一起,不同的Actiivty也可以使用同一個Fragment,在