xUtils

导入依赖

 implementation  'org.xutils:xutils:3.5.0'

//初始化

public class MyAppliaction extends Application {
    @Override
    public void onCreate() {
        x.Ext.init(this);//开启xuiils
        x.Ext.setDebug(BuildConfig.DEBUG);//开启debug模式
        super.onCreate();
    }
}

//activity

@ContentView(R.layout.activity_main)//绑定布局
public class MainActivity extends AppCompatActivity {
    /**
     * @ViewInject(控件id)
     * 绑定控件
     *
     * @ContentView(布局id)
     * 绑定布局
     *
     * @Event(value=控件id,type=View.监听.class
     * 关联监听事件
     *
     * 备注:控件一定要放在注解后面 否者会报错
     *
     * */
    @ViewInject(R.id.iamge1)
    ImageView imageView;
    @ViewInject(R.id.b1)
    Button button;
    @ViewInject(R.id.b2)
    Button button2;
    @ViewInject(R.id.t1)
    TextView textView;
    @ViewInject(R.id.t2)
    TextView textView2;
    @ViewInject(R.id.list1)
    ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);//加载布局控件

        //加载网络图片
//        x.image().bind(imageView,"http://pic.baike.soso.com/p/20090711/20090711101754-314944703.jpg");
        //自定义加载网络图片
        ImageOptions imageOptions=new ImageOptions.Builder()
                .setSize(200,300)//图片大小
                .setRadius(20)//圆角半径
                .setCrop(true)//设置imageview的大小不是定义为 wrap_content 修改设置的参数
                .setImageScaleType(ImageView.ScaleType.CENTER_CROP)
                .setLoadingDrawableId(R.mipmap.ic_launcher)//设置加载中默认显示图片
                .setFailureDrawableId(R.mipmap.ic_launcher)//设置加载失败显示图片
                .build();
               x.image().bind(imageView,"http://pic.baike.soso.com/p/20090711/20090711101754-314944703.jpg",imageOptions);

        List<String> list=new ArrayList<>();
               for(int i=0;i<10;i++){
                   list.add("列表数据"+i);
               }
              MyAdapter myAdapter=new MyAdapter(list,this);
               listView.setAdapter(myAdapter);
    }
    @Event(value = R.id.b1)
    private void click(View view){
        Toast.makeText(MainActivity.this,"您点击了按钮1",Toast.LENGTH_SHORT).show();
    }
    @Event(value = R.id.b2)
    private void click2(View view){
        Toast.makeText(MainActivity.this,"您点击了按钮2",Toast.LENGTH_SHORT).show();
    }
}

//xml布局

 <ImageView
        android:id="@+id/iamge1"
        android:layout_width="50dp"
        android:layout_height="50dp" />
    <Button
        android:id="@+id/b1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮1"/>
    <Button
        android:id="@+id/b2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮2"/>
    <TextView
        android:id="@+id/t1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="文本1"/>
    <TextView
        android:id="@+id/t2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="---------------------------------------"
        android:textSize="25dp"/>
    <ListView
        android:id="@+id/list1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>

//listItme布局

  <TextView
        android:id="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="19dp"/>

//适配器

public class MyAdapter extends BaseAdapter {
    List list;
    Context context;
    public MyAdapter( List list,Context context){
        this.list=list;
        this.context=context;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView==null){
         convertView=View.inflate(parent.getContext(),R.layout.list_item,null);
         viewHolder=new ViewHolder();
         //viewHolder注解
            x.view().inject(viewHolder,convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder=(ViewHolder) convertView.getTag();
        }
        viewHolder.textView.setText(list.get(position));
        return convertView;
    }
    private class ViewHolder{
        @ViewInject(value =R.id.text1)
        TextView textView;
    }
    @Event(value = R.id.text1)
    private void onclick(View view){
        Toast.makeText(context,"您点击了列表按钮",Toast.LENGTH_SHORT).show();
    }
}

//效果图
xUtils_第1张图片

你可能感兴趣的:(xUtils)