GreenDao+ButterKnife+Retrofit+Fresco展示网络数据
来源:互联网 发布:amarra for mac 编辑:程序博客网 时间:2024/05/21 15:48
activity_main
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.exam.20171202.MainActivity"> <FrameLayout android:id="@+id/frag" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" /> <RadioGroup android:id="@+id/rg" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:layout_weight="9"> <RadioButton android:id="@+id/rb1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:button="@null" android:gravity="center" android:background="@drawable/ac0"/> <RadioButton android:id="@+id/rb2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:button="@null" android:gravity="center" android:background="@drawable/abw"/> <RadioButton android:id="@+id/rb3" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:button="@null" android:gravity="center" android:background="@drawable/aby"/> <RadioButton android:id="@+id/rb4" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:button="@null" android:gravity="center" android:background="@drawable/abu"/> <RadioButton android:id="@+id/rb5" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:button="@null" android:gravity="center" android:background="@drawable/ac2"/> </RadioGroup></LinearLayout>
fragment1
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v7.widget.RecyclerView android:id="@+id/rv" android:layout_width="match_parent" android:layout_height="match_parent"/></LinearLayout>item布局<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="150dp" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="horizontal" android:padding="20dp"> <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/sdv" android:layout_width="120dp" android:layout_height="120dp" app:placeholderImage="@mipmap/ic_launcher_round"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:paddingLeft="20dp" android:layout_marginTop="20dp"> <TextView android:id="@+id/tv1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp"/> </LinearLayout></LinearLayout>MainActivitypublic class MainActivity extends AppCompatActivity { @BindView(R.id.rb1) RadioButton rb1; @BindView(R.id.rb2) RadioButton rb2; @BindView(R.id.rb3) RadioButton rb3; @BindView(R.id.rb4) RadioButton rb4; @BindView(R.id.rb5) RadioButton rb5; @BindView(R.id.rg) RadioGroup rg; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); Fragment1 fragment1 = new Fragment1(); getSupportFragmentManager().beginTransaction().replace(R.id.frag,fragment1).commit(); rb1.setBackgroundResource(R.drawable.ac1); } @OnClick({R.id.rb1,R.id.rb2,R.id.rb3,R.id.rb4,R.id.rb5}) public void onClick(View v) { rb1.setBackgroundResource(R.drawable.ac0); rb2.setBackgroundResource(R.drawable.abw); rb3.setBackgroundResource(R.drawable.aby); rb4.setBackgroundResource(R.drawable.abu); rb5.setBackgroundResource(R.drawable.ac2); switch (v.getId()) { default: break; case R.id.rb1: Fragment1 fragment1 = new Fragment1(); getSupportFragmentManager().beginTransaction().replace(R.id.frag,fragment1).commit(); rb1.setBackgroundResource(R.drawable.ac1); break; case R.id.rb2: Fragment2 fragment2 = new Fragment2(); getSupportFragmentManager().beginTransaction().replace(R.id.frag,fragment2).commit(); rb2.setBackgroundResource(R.drawable.abx); break; case R.id.rb3: Fragment3 fragment3 = new Fragment3(); getSupportFragmentManager().beginTransaction().replace(R.id.frag,fragment3).commit(); rb3.setBackgroundResource(R.drawable.abz); break; case R.id.rb4: Fragment4 fragment4 = new Fragment4(); getSupportFragmentManager().beginTransaction().replace(R.id.frag,fragment4).commit(); rb4.setBackgroundResource(R.drawable.abv); break; case R.id.rb5: Fragment5 fragment5 = new Fragment5(); getSupportFragmentManager().beginTransaction().replace(R.id.frag,fragment5).commit(); rb5.setBackgroundResource(R.drawable.ac3); break; } }}bean类ClassBeanspublic class ClassBeans<T> { private boolean error; private List<T> results; public boolean isError() { return error; } public void setError(boolean error) { this.error = error; } public List<T> getResults() { return results; } public void setResults(List<T> results) { this.results = results; } @Override public String toString() { return "ClassBeans{" + "error=" + error + ", results=" + results + '}'; }}DaoBean@Entitypublic class DaoBean {// @Id(autoincrement = true)// private Long id; private String _id; private String createdAt; private String desc; private String publishedAt; private String source; private String type; private String url; private boolean used; private String who; @Convert(converter = ImageUtil.class,columnType = String.class) public List<String> images;}GreenDaoManagerpublic class GreenDaoManager { private static GreenDaoManager managers; private static DaoMaster daoMaster; private static DaoSession daoSession; private GreenDaoManager(Context context){ DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(context, "contacts-db",null); SQLiteDatabase sqLiteDatabase = devOpenHelper.getWritableDatabase(); daoMaster = new DaoMaster(sqLiteDatabase); daoSession = daoMaster.newSession(); } public static GreenDaoManager getGreenDaoManager(Context context){ if (managers == null){ synchronized (GreenDaoManager.class){ if (managers == null){ managers = new GreenDaoManager(context); } } } return managers; } public DaoSession getDaoSession(){ return daoSession; } public DaoMaster getDaoMaster(){ return daoMaster; }}MyApp--去清单文件注册public class MyApp extends Application { @Override public void onCreate() { super.onCreate(); Fresco.initialize(this); GreenDaoManager.getGreenDaoManager(this); }}ServerApipublic interface ServerApi { @GET("api/data/Android/{size}/{page}") Call<ClassBeans<DaoBean>> list(@Path("size") int size,@Path("page") int page);}RetrofitHelperpublic class RetrofitHelper { public static OkHttpClient client; public static ServerApi serverApi; static { getClient(); } public static OkHttpClient getClient() { if (client == null){ synchronized (RetrofitHelper.class){ if (client == null){ client = new OkHttpClient(); } } } return client; } public static ServerApi getServerApi(){ if (serverApi == null){ synchronized (ServerApi.class){ if (serverApi == null){ serverApi = OnCreatApi(ServerApi.class,"http://gank.io/"); } } } return serverApi; } public static <T> T OnCreatApi(Class<T> tClass,String url){ Retrofit retrofit = new Retrofit.Builder() .baseUrl(url) .addConverterFactory(GsonConverterFactory.create()) .build(); return retrofit.create(tClass); }}ImageUtil--加载图片public class ImageUtil implements PropertyConverter<List<String>, String> { @Override public List<String> convertToEntityProperty(String databaseValue) { List<String> list = new ArrayList<>(); list.add(databaseValue); return null; } @Override public String convertToDatabaseValue(List<String> entityProperty) { return (entityProperty == null && !entityProperty.isEmpty())?"" : entityProperty.get(0); }}Fragment1public class Fragment1 extends Fragment { private DaoBeanDao dao; private RecyclerView rv; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { View view = View.inflate(getActivity(), R.layout.fragment1, null); rv = view.findViewById(R.id.rv); LinearLayoutManager manager = new LinearLayoutManager(getActivity()); rv.setLayoutManager(manager); //GreenDao dao = GreenDaoManager.getGreenDaoManager(getActivity()).getDaoSession().getDaoBeanDao(); //Retrofit ServerApi serverApi = RetrofitHelper.getServerApi(); final Call<ClassBeans<DaoBean>> list = serverApi.list(10, 1); list.enqueue(new Callback<ClassBeans<DaoBean>>() { @Override public void onResponse(Call<ClassBeans<DaoBean>> call, Response<ClassBeans<DaoBean>> response) { ClassBeans<DaoBean> classBeans=response.body(); for (DaoBean bean:response.body().getResults()) { dao.insert(bean); } List<DaoBean> results = classBeans.getResults(); Log.e("TAG",results+""); rv.setAdapter(new RvAdapter(getContext(),results)); } @Override public void onFailure(Call<ClassBeans<DaoBean>> call, Throwable t) { } }); return view; }}RecyclerView的适配器public class RvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private Context context; private List<DaoBean> list; public RvAdapter(Context context, List<DaoBean> list) { this.context = context; this.list = list; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = View.inflate(context, R.layout.item, null); return new MyViewHolder(view); } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { MyViewHolder holder1 = (MyViewHolder) holder; DaoBean daoBean = list.get(position); List<String> images = daoBean.getImages(); if (images != null){ holder1.sdv.setImageURI(images.get(0)); } holder1.tv1.setText(daoBean.getDesc()); holder1.tv2.setText(daoBean.getWho()); } @Override public int getItemCount() { return list.size(); } class MyViewHolder extends RecyclerView.ViewHolder{ SimpleDraweeView sdv; TextView tv1; TextView tv2; public MyViewHolder(View itemView) { super(itemView); sdv = itemView.findViewById(R.id.sdv); tv1 = itemView.findViewById(R.id.tv1); tv2 = itemView.findViewById(R.id.tv2); } }}依赖包compile 'com.facebook.fresco:fresco:1.5.0'compile 'com.android.support:recyclerview-v7:26.1.0'compile 'com.squareup.okhttp3:okhttp:3.9.0'compile 'com.google.code.gson:gson:2.8.2'
compile 'org.greenrobot:greendao:3.2.0'annotationProcessor 'org.greenrobot:eventbus-annotation-processor:3.1.1'compile 'com.jakewharton:butterknife:8.8.1'annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'//Retrofit2所需要的包compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'//ConverterFactory的Gson依赖包compile 'com.squareup.retrofit2:converter-scalars:2.0.0-beta4'//ConverterFactory的String依赖包apply plugin: 'org.greenrobot.greendao'classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2'
阅读全文
0 0
- GreenDao+ButterKnife+Retrofit+Fresco展示网络数据
- 展示数据使用:recyclerview,retrofit,greendao,butterknife,eventbus,fresco。实现效果图列表。MVP模式。
- fresco加载图片+EventBus Activity之间跳转传值+GreenDAo数据库+retrofit请求数据+recyclerview展示数据+ButterKnife找控件
- Retrofit网络请求+Recyclerview展示数据+Fresco显示图片
- Retrofit网络请求+Recyclerview展示数据+Fresco显示图片
- android_网络判断,Greendao,retrofit,eventbus结合展示数据
- MVP+Retrofit+Rxjava+ButterKnife+Fresco+Recyclerview+GreenDao+泛型+完整Demo
- Android——Fresco、EventBus、GreenDao、Butterknife、Retrofit综合使用
- Fresco,EventBus,GreenDao,Butterknife,Retrofit 的结合使用
- Retrofit+RxJava+Fresco简单实现数据展示
- 使用MVP+Retrofit+RxJava 图片加载使用Fresco 做网络请求并展示数据
- GreenDao--retrofit--fresco--eventbus--recyclerview
- MVP+Retrofit+fresco+greendao+eventbus
- Fresco图片加载+Eventbus跳转传值+Retrofit请求数据+GreenDao操作数据+Mvp分层+RecyclerView展示数据+ButtonKnife生成控件
- MVP+Rxjava+Retrofit+Fresco网络请求数据
- Retrofit网络请求+GreenDao数据缓存
- Retrofit+OkHttpClient+Fresco+XRecyclerView+RxJava实现数据展示
- Android 用Retrofit进行网络获取数据,recyclerview进行展示在Fragment里,并存入到GreenDao数据库中
- 数据结构之树的操作
- Atitit 人工智能目前的进展与未来 包含的技术 v3
- python 统计序列中元素的出现频度\统计英文文章的词频
- codeforces #422 C. Hacker, pack your bags!(贪心dp)
- 利用ScriptEngineManager实现字符串公式灵活计算
- GreenDao+ButterKnife+Retrofit+Fresco展示网络数据
- yii2 Unable to find debug data tagged with
- 运算符重载
- AIDL的基本使用
- Java之等边三角形
- SVN中如何给项目打基线
- Redis 分布式锁的正确实现方式( Java 版 )
- 【运维】堡垒机的安装和使用
- 数字数组去重