Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> xUtils之view注入--Android

xUtils之view注入--Android

編輯:關於Android編程

關於xUtils的使用有各種各樣的說明和帖子,今天主要結合自己的項目總結一下view注入的方面。

添加xUtils庫後

 

@ViewInject(R.id.textView)

TextView textView;

 

//@ViewInject(vale=R.id.textView, parentId=R.id.parentView)

//TextView textView;

 

 

1.在Activity中注入:

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

ViewUtils.inject(this); //注入view和事件

...

textView.setText(some text...);

...

}

2.在Fragment中注入:

@Override

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

View view = inflater.inflate(R.layout.bitmap_fragment, container, false); // 加載fragment布局

ViewUtils.inject(this, view); //注入view和事件

...

}

3.在PreferenceFragment中注入:

public void onActivityCreated(Bundle savedInstanceState) {

super.onActivityCreated(savedInstanceState);

ViewUtils.inject(this, getPreferenceScreen()); //注入view和事件

...

}

4.在Adapter中注入

public class MxnGridViewAdapter extends BaseAdapter {


class ViewHolder {
@ViewInject(R.id.home_grid_image)
public ImageView image;
@ViewInject(R.id.home_grid_text)
public TextView title;
}

...

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.fhome_grid_item, null);
viewHolder = new ViewHolder();
ViewUtils.inject(viewHolder,convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.title.setText(titles[position]);
viewHolder.image.setImageResource(images[position]);
return convertView;
 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved