本文主要是介绍xUtils 获取网络数据 + RecycleView 显示数据+ F resco 加载图片 + ButterKnife 初始化控件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
导入 xUtils 和 gson 的jar包
导入依赖
compile 'com.android.support:recyclerview-v7:25.3.1' compile 'com.jakewharton:butterknife:8.8.1' compile 'com.jakewharton:butterknife-compiler:8.8.1' compile 'com.facebook.fresco:fresco:1.5.0'
页面
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="bw.com.week2_test.MainActivity"> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/rv_id" /> </LinearLayout>item_rv.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:fresco="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <!-- fresco:placeholderImage="@mipmap/ic_launcher" 占位图片 fresco:roundAsCircle="false" //设置圆形图片 fresco:roundedCornerRadius="1dp" 设置圆角的度数 fresco:roundTopLeft="true" 左上角是否为圆形 fresco:roundTopRight="false" fresco:roundBottomLeft="false" fresco:roundBottomRight="true" fresco:roundWithOverlayColor="@color/colorAccent" 填充颜色 fresco:roundingBorderWidth="20dp" 边界线的宽度 fresco:roundingBorderColor="@color/colorPrimary" 边界线的颜色 --> <com.facebook.drawee.view.SimpleDraweeView android:layout_width="300dp" android:layout_height="200dp" android:id="@+id/iv_id" fresco:placeholderImage="@mipmap/ic_launcher" fresco:roundAsCircle="false" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv_id" android:text="标题" android:textSize="20sp" /> <View android:layout_width="match_parent" android:layout_height="2dp" android:background="@color/colorAccent" android:layout_margin="10dp" /> </LinearLayout>代码
MyApp.java
public class MyApp extends Application {@Override public void onCreate() {super.onCreate(); //初始化xUtils x.Ext.init(this); x.Ext.setDebug(true); //初始化Fresco Fresco.initialize(this); } }清单文件中:AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <application android:name=".MyApp"
MainActivity.java
public class MainActivity extends AppCompatActivity {@BindView(value = R.id.rv_id)RecyclerView mRv; private List<VMoiver.DataBean> data; private RecycleAdapter adapter; private Unbinder unbinder; private String path = "http://app.vmoiver.com/apiv3/post/getPostInCate?cateid=0&p=1"; @Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //绑定ButterKnife unbinder = ButterKnife.bind(this); //设置RecycleView展示的方式 LinearLayoutManager manager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false); mRv.setLayoutManager(manager); //通过xUtils获取数据 RequestParams params = new RequestParams(path); x.http().get(params, new Callback.CommonCallback<String>() {@Override public void onSuccess(String s) {//解析数据, 得到数据源 VMoiver vMoiver = new Gson().fromJson(s,VMoiver.class); data = vMoiver.getData(); //初始化适配器 adapter = new RecycleAdapter(MainActivity.this,data); mRv.setAdapter(adapter); }@Override public void onError(Throwable throwable, boolean b) {}@Override public void onCancelled(CancelledException e) {}@Override public void onFinished() {}}); }@Override protected void onDestroy() {super.onDestroy(); //解绑 unbinder.unbind(); } }
RecycleActivity.java
public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.ViewHolder> {private Context context; private List<VMoiver.DataBean> data; public RecycleAdapter(Context context,List<VMoiver.DataBean> data){this.context = context; this.data = data; }@Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {//TODO 绑定页面 View view = LayoutInflater.from(context).inflate(R.layout.item_rv,parent,false); ViewHolder viewHolder = new ViewHolder(view); return viewHolder; }@Override public void onBindViewHolder(ViewHolder holder, int position) {holder.tv.setText(data.get(position).getTitle()); //Fresco 加载图片 Uri uri = Uri.parse(data.get(position).getImage()); holder.sdv.setImageURI(uri); }@Override public int getItemCount() {return data.size(); }class ViewHolder extends RecyclerView.ViewHolder{//使用ButterKnife注解 @BindView(R.id.iv_id)SimpleDraweeView sdv; @BindView(R.id.tv_id)TextView tv; public ViewHolder(View itemView) {super(itemView); //绑定 ButterKnife.bind(this,itemView); }} }VMoiver.java ------ GsonFormat 生成的实体类
这篇关于xUtils 获取网络数据 + RecycleView 显示数据+ F resco 加载图片 + ButterKnife 初始化控件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!