xUtils3.0使用介绍:注解模块

之前介绍了xUtils3.0的导入和权限配置,现在再来逐一介绍xUtils3的4大模块:注解模块,网络模块,图片加载模块,数据库模块。

注解模块

xml布局注解:

1,Activity中的xml布局加载:

@ContentView(R.layout.activity_main)//加载的xml文件
public class ActivityTest extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);//注解绑定
    }
}

2, Fragment中的xml布局加载:

@ContentView(R.layout.activity_main)//加载的xml文件
public class FragmentTest extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return x.view().inject(this, inflater, container);//绑定fragment注解
    }
}

3,ViewHolder的注解加载:

public class AdapterTest extends BaseAdapter {
    private Context context;
    private Holder holder;
    public ActivityTest(Context context){
        this.context = context;
    }

    ...

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView==null){  
            convertView=View.inflate(context,R.layout.item, null);  
            holder=new Holder();  
            x.view().inject(holder,convertView);//注解绑定 
            convertView.setTag(holder);  
        }  
        else{  
            holder=(Holder) convertView.getTag();  
        }  

        return convertView;  
    }
    private class Holder{
        @ViewInject(R.id.item_name)private TextView tv_name;//id声明 
        @ViewInject(R.id.item_image)private ImageView iv_image;  
    }

}

以上几种的布局文件的绑定都是大同小异,来来去去都是x.view().inject()的用法

响应事件注解:

/** *等同于@Event(value={R.id.id_but1,R.id.id_but2},type=View.OnClickListener.class) */
@Event(value={R.id.id_but1,R.id.id_but2}) //type默认为OnClickListener
 private void getEvent(View view){//必须用private进行修饰,否则无效 
     switch(view.getId()){  
     case R.id.id_but1: 
         break;  
     case R.id.id_but2:  
        break;  
     }  
 }  

你可能感兴趣的:(框架工具)