底层build.gradle
buildscript {
repositories {
google()
jcenter()
mavenCentral() // add repository
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2'
本app的build.gradle
apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao'
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
compile 'com.android.support:design:27.+'
implementation 'com.squareup.retrofit2:converter-gson:2.0.2'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
implementation 'io.reactivex.rxjava2:rxjava:2.0.2'
implementation 'com.squareup.retrofit2:retrofit:2.0.2'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
implementation 'org.greenrobot:greendao:3.2.2'
compile 'com.facebook.fresco:fresco:0.12.0'
compile 'com.facebook.fresco:animated-base-support:0.12.0'
// 支持 GIF 动图,需要添加
compile 'com.facebook.fresco:animated-gif:0.12.0'
// 支持 WebP (静态图+动图),需要添加
compile 'com.facebook.fresco:animated-webp:0.12.0'
compile 'com.facebook.fresco:webpsupport:0.12.0'
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name=".app.App"
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"
android:orientation="vertical"
tools:context="com.example.kson.twolianxi01.MainActivity">
<FrameLayout
android:id="@+id/fragm"
android:layout_width="match_parent"
android:layout_weight="9"
android:layout_height="0dp" />
<RadioGroup
android:id="@+id/rap"
android:layout_weight="1"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="0dp">
<RadioButton
android:id="@+id/rap1"
android:layout_weight="1"
android:textSize="20sp"
android:text="首页"
android:button="@null"
android:gravity="center"
android:layout_width="0dp"
android:layout_height="match_parent" />
<RadioButton
android:id="@+id/rap2"
android:layout_weight="1"
android:textSize="20sp"
android:text="附近"
android:button="@null"
android:gravity="center"
android:layout_width="0dp"
android:layout_height="match_parent" />
<RadioButton
android:id="@+id/rap3"
android:layout_weight="1"
android:textSize="20sp"
android:text="我的"
android:button="@null"
android:gravity="center"
android:layout_width="0dp"
android:layout_height="match_parent" />
</RadioGroup>
</LinearLayout>
frag1.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"
android:orientation="vertical"
tools:context="com.example.kson.twolianxi01.fragment.Frag1">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycleview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
include1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_height="match_parent">
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/my_image_view"
android:layout_width="130dp"
android:layout_height="130dp"
fresco:placeholderImage="@mipmap/ic_launcher"
/>
<TextView
android:id="@+id/title1"
android:textSize="20sp"
android:text="title1"
android:layout_width="130dp"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity
public class MainActivity extends AppCompatActivity {
@BindView(R.id.fragm)
FrameLayout fragm;
@BindView(R.id.rap)
RadioGroup rap;
private FragmentManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化
ButterKnife.bind(this);
//创建对象
final Frag1 frag1 = new Frag1();
final Frag2 frag2 = new Frag2();
final Frag3 frag3 = new Frag3();
// 事务提交
manager = getSupportFragmentManager();
//默认使用第一个
manager.beginTransaction().replace(R.id.fragm,frag1).commit();
//条目的监听事件
rap.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
//获取事务
FragmentTransaction transaction = manager.beginTransaction();
switch (i){
case R.id.rap1:
transaction.replace(R.id.fragm,frag1);
break;
case R.id.rap2:
transaction.replace(R.id.fragm,frag2);
break;
case R.id.rap3:
transaction.replace(R.id.fragm,frag3);
break;
}
//提交事务
transaction.commit();
}
});
}
}
HttpUtils
public class HttpUtils {
private static HttpUtils httpUtils;
private OkHttpClient okHttpClient;
//有参
private HttpUtils(){
okHttpClient = new OkHttpClient.Builder()
.addInterceptor(new longinterceptor())
.build();
}
//创建拦截器
class longinterceptor implements Interceptor{
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
//创建方法
String method = request.method();
//请求地址
HttpUrl url = request.url();
//打印日志信息
Log.i("xxx",method+"===="+url);
//响应
Response response = chain.proceed(request);
return response;
}
}
//单例
public static HttpUtils getinstance(){
if (httpUtils==null){
synchronized (HttpUtils.class){
if (httpUtils==null){
httpUtils = new HttpUtils();
}
}
}
return httpUtils;
}
//get方法
public Api getpost(String base){
Retrofit retrofit = new Retrofit.Builder()
.client(okHttpClient)
//拼接地址
.baseUrl(base)
//添加retrofit对rxjava的支持
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
//添加一个gson对象
.addConverterFactory(GsonConverterFactory.create())
.build();
//获取接口
Api api = retrofit.create(Api.class);
return api;
}
}
Api
public interface Api {
@GET("content_lists&version=1.7&token=&type=1&cat=&catid2=&page=1")
Observable<news> getresponse();
}
app
public class app extends Application {
private static app app;
private DaoSession daoSession;
@Override
public void onCreate() {
super.onCreate();
Fresco.initialize(this);
app =app.this;
//获取数据库
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "beiw");
//获取对象
SQLiteDatabase db = helper.getWritableDatabase();
//获取数据
DaoMaster daoMaster = new DaoMaster(db);
//数据库的增删改查
daoSession = daoMaster.newSession();
}
public static app getinstance(){
if (app==null){
app =new app();
}
return app;
}
public DaoSession getdoasession(){
return daoSession;
}
}
news
format
work
@Entity
public class work {
@Id(autoincrement = true)
Long id;
@Property
String imgageurl;
@Property
String title1;
build--make projeck
constant
public class constant {
//定义接口地址
public static final String URL="https://app.tuozhe8.com/api.php/api/Lists/content_lists&version=1.7&token=&type=1&cat=&catid2=&page=1";
//定义根地址
public static final String BASE_URL="https://app.tuozhe8.com/api.php/api/Lists/";
}
icontract
public interface icontract {
/**
* iview层
*/
public interface iview{
//显示方法
void showdata(List<work> works);
}
/**
* ipresenter
*/
public interface ipresenter<iview>{
//关联
void attachview(iview iview);
//取消
void detachview(iview iview);
//逻辑
void requestinfo();
}
/**
* imoudle
*
*/
public interface imoudle{
//接口回调
public interface callisten{
//信息回显
void responsemsg(List<work> works);
}
void requestdata(callisten callisten);
}
}
moudleimp
public class moudleimp implements icontract.imoudle {
@Override
public void requestdata(final callisten callisten) {
//获取数据库表
final workDao workDao = app.getinstance().getdoasession().getWorkDao();
//进行全查
final List<work> works = workDao.loadAll();
//判断
if (works.size()>0){
//进行接口回传
callisten.responsemsg(works);
return;
}
//查不到进行网络请求
HttpUtils httpUtils = HttpUtils.getinstance();
//获取接口
Api api = httpUtils.getpost(constant.BASE_URL);
//获取接口中的方法
Observable<news> observable = api.getresponse();
//获取具体的参数
observable
//运行在io线程
.subscribeOn(Schedulers.io())
//回调在主线程
.observeOn(AndroidSchedulers.mainThread())
//进行不完全回调
.subscribe(new Consumer<news>() {
//成功
@Override
public void accept(news news) throws Exception {
//获取具体的对象
List<com.example.kson.twolianxi01.bean.news.DataBean> data = news.getData();
//创建新的集合进行回传
ArrayList<work> works1 = new ArrayList<>();
//进行遍历
for (int i = 0; i <data.size() ; i++) {
String title1 = data.get(i).getTitle();
String logo = data.get(i).getLogo();
//创建对象
work work = new work();
work.setTitle1(title1);
work.setImgageurl(logo);
works1.add(work);
}
callisten.responsemsg(works1);
workDao.insertInTx(works1);
}
}, new Consumer<Throwable>() {
//失败
@Override
public void accept(Throwable throwable) throws Exception {
callisten.responsemsg(null);
}
});
}
}
presenterimp
public class presenterimp implements icontract.ipresenter<icontract.iview> {
private icontract.iview iview;
private com.example.kson.twolianxi01.model.moudleimp moudleimp;
private WeakReference<icontract.iview> iviewWeakReference;
private WeakReference<icontract.imoudle> weakReference;
@Override
public void attachview(icontract.iview iview) {
this.iview = iview;
moudleimp = new moudleimp();
//弱引用解绑
iviewWeakReference = new WeakReference<>(iview);
weakReference = new WeakReference<icontract.imoudle>(moudleimp);
}
@Override
public void detachview(icontract.iview iview) {
iviewWeakReference.clear();
weakReference.clear();
}
@Override
public void requestinfo() {
//调用方法
moudleimp.requestdata(new icontract.imoudle.callisten() {
@Override
public void responsemsg(List<work> works) {
//调用view方法
iview.showdata(works);
}
});
}
}
myadapter
public class myadapter extends RecyclerView.Adapter<myadapter.oneholder> { private Context context; private List<work> list; public myadapter(Context context, List<work> list) { this.context = context; this.list = list; } @Override public oneholder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.include1, null); oneholder oneholder = new oneholder(view); return oneholder; } @Override public void onBindViewHolder(oneholder holder, int position) { holder.title1.setText(list.get(position).getTitle1()); Uri uri = Uri.parse(list.get(position).getImgageurl()); holder.my_image_view.setImageURI(uri); } @Override public int getItemCount() { return list.size(); } //创建视图 class oneholder extends RecyclerView.ViewHolder{ private final SimpleDraweeView my_image_view; private final TextView title1; public oneholder(View itemView) { super(itemView); //初始化 my_image_view = itemView.findViewById(R.id.my_image_view); title1 = itemView.findViewById(R.id.title1); } } }
Frag1
public class Frag1 extends Fragment implements icontract.iview {
@BindView(R.id.recycleview)
RecyclerView recycleview;
Unbinder unbinder;
private presenterimp presenterimp;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag1, container, false);
unbinder = ButterKnife.bind(this, view);
presenterimp = new presenterimp();
//关联
presenterimp.attachview(this);
//请求
presenterimp.requestinfo();
return view;
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
//取消关联
presenterimp.detachview(this);
}
//具体操作
@Override
public void showdata(List<work> works) {
//创建布局管理器
GridLayoutManager manager = new GridLayoutManager(getActivity(), 2, GridLayoutManager.VERTICAL, false);
//添加布局管理器
recycleview.setLayoutManager(manager);
//创建适配器
myadapter myadapter = new myadapter(getActivity(),works);
//设置适配器
recycleview.setAdapter(myadapter);
}
}