首先导航activity_main.XML
<?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="match_parent" android:orientation="vertical"> <!-- app bar --> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:layout_width="130dp" android:layout_height="35dp" android:drawableLeft="@drawable/ic_location_on_black_24dp" android:drawableRight="@drawable/ic_keyboard_arrow_down_black_24dp" android:gravity="center" android:singleLine="true" android:text="北京八维教育中心" /> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" android:layout_weight="1" android:background="@drawable/search_b" android:paddingLeft="40dp" android:paddingRight="40dp"> <EditText android:layout_width="match_parent" android:layout_height="match_parent" android:background="@null" android:drawableLeft="@drawable/ic_search_black_24dp" android:hint="搜索附近的吃喝玩乐" android:singleLine="true" android:textSize="13sp" /> </LinearLayout> <ImageView android:id="@+id/iv_head" android:layout_width="50dp" android:layout_height="50dp" android:padding="3dp" android:src="@drawable/ic_search_black_24dp" /> </LinearLayout> <!-- 列表 --> <android.support.v7.widget.RecyclerView android:layout_margin="15dp" android:id="@+id/recycle_view" android:layout_width="match_parent" android:layout_height="match_parent" ></android.support.v7.widget.RecyclerView> </LinearLayout>
之后是列表main_item
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:layout_marginBottom="20dp"> <ImageView android:id="@+id/iv_icon" android:layout_width="140dp" android:layout_height="100dp" android:scaleType="fitXY" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toRightOf="@id/iv_icon" android:orientation="horizontal"> <TextView android:id="@+id/tv_title" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_toRightOf="@id/iv_icon" android:layout_weight="1" android:paddingLeft="10dp" android:singleLine="true" android:text="兰州拉面" android:textAppearance="@style/TextAppearance.AppCompat.Title" /> <FrameLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/distance_bg"> <TextView android:id="@+id/tv_distance" android:textSize="10sp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:padding="2dp" android:text="85m" /> </FrameLayout> </LinearLayout> </RelativeLayout>
之后是java代码,里面有5个包分别是baes,contract,model,presenter,ui
baes里面有IModel,Ipresenter,IView
package com.bwie.xiaqin.zhoukao03lx.mvp.base; /** * M层规范 * Created by lenovo on 2018/9/15. */ public interface IModel { }
第二个
package com.bwie.xiaqin.zhoukao03lx.mvp.base; /** * P层规范 * Created by lenovo on 2018/9/15. */ public interface IPresenter<V extends IView> { void attach(V v); void detach(); }
第三个
package com.bwie.xiaqin.zhoukao03lx.mvp.base; /** * V层规范 * Created by lenovo on 2018/9/15. */ public interface IView { }
之后是contract里面的,有App,MainContract
package com.bwie.xiaqin.zhoukao03lx.mvp.contract; import android.app.Application; import org.xutils.x; /** * Created by lenovo on 2018/9/16. */ public class App extends Application { @Override public void onCreate() { super.onCreate(); x.Ext.init(this); } }
第二个
package com.bwie.xiaqin.zhoukao03lx.mvp.contract; import com.bwie.xiaqin.zhoukao03lx.mvp.base.IModel; import com.bwie.xiaqin.zhoukao03lx.mvp.base.IPresenter; import com.bwie.xiaqin.zhoukao03lx.mvp.base.IView; /** * Created by lenovo on 2018/9/15. */ public interface MainContract { interface MainView extends IView{ void showList(String result); } interface MainModel extends IModel{ interface NetCallback{ void onSuccess(String result); void onError(String meg); } void loadData(String url,NetCallback callback); } interface MainPresenter extends IPresenter<MainView>{ void loadData(String url); } }
之后是model里面的,里面有一个包叫net,里面是OKHttpUtils,然后是model里面的MainModel
package com.bwie.xiaqin.zhoukao03lx.mvp.model.net; import android.os.Handler; import android.os.Looper; import java.io.IOException; import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import okhttp3.logging.HttpLoggingInterceptor; /** * Created by lenovo on 2018/9/15. */ public class OKHttpUtils { public interface NetCallback{ void success(String result); void error(String errorMsg); } private Handler mHandler; private OkHttpClient mOkHttpClient; { HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); mOkHttpClient = new OkHttpClient.Builder().addInterceptor(interceptor).build(); mHandler = new Handler(Looper.getMainLooper()); } private static final OKHttpUtils ourInstance = new OKHttpUtils(); public static OKHttpUtils getInstance(){ return ourInstance; } private OKHttpUtils(){ } public void get(String url,final NetCallback netCallback){ Request request = new Request.Builder().url(url).build(); mOkHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, final IOException e) { mHandler.post(new Runnable() { @Override public void run() { if (netCallback !=null){ netCallback.error(e.getMessage()); } } }); } @Override public void onResponse(Call call, final Response response) throws IOException { mHandler.post(new Runnable() { @Override public void run() { if (netCallback!=null){ try { netCallback.success(response.body().string()); } catch (IOException e) { e.printStackTrace(); } } } }); } }); } public void post(){ } }
第二个
package com.bwie.xiaqin.zhoukao03lx.mvp.model; import com.bwie.xiaqin.zhoukao03lx.mvp.contract.MainContract; import com.bwie.xiaqin.zhoukao03lx.mvp.model.net.OKHttpUtils; /** * Created by lenovo on 2018/9/15. */ public class MainModel implements MainContract.MainModel { private OKHttpUtils utils; public MainModel(){ utils = OKHttpUtils.getInstance(); } @Override public void loadData(String url, final NetCallback callback) { utils.get(url, new OKHttpUtils.NetCallback() { @Override public void success(String result) { callback.onSuccess(result); } @Override public void error(String errorMsg) { callback.onError(errorMsg); } }); } }
之后是presenter里面是MainPresenter
package com.bwie.xiaqin.zhoukao03lx.mvp.presenter; import com.bwie.xiaqin.zhoukao03lx.mvp.contract.MainContract; import com.bwie.xiaqin.zhoukao03lx.mvp.model.MainModel; import java.lang.ref.WeakReference; /** * Created by lenovo on 2018/9/15. */ public class MainPresenter implements MainContract.MainPresenter { private WeakReference<MainContract.MainView> viewWeakReference; private WeakReference<MainModel>modelWeakReference; @Override public void attach(MainContract.MainView mainView) { viewWeakReference = new WeakReference(mainView); modelWeakReference = new WeakReference(new MainModel()); } @Override public void detach() { if (viewWeakReference!=null){ viewWeakReference.clear(); viewWeakReference=null; modelWeakReference.clear(); modelWeakReference=null; } } @Override public void loadData(String url) { modelWeakReference.get().loadData(url, new MainContract.MainModel.NetCallback() { @Override public void onSuccess(String result) { viewWeakReference.get().showList(result); } @Override public void onError(String meg) { } }); } }
然后是ui里面的里面有四个包分别是activity,adapter,bean,holder
MainActivity
package com.bwie.xiaqin.zhoukao03lx.mvp.ui.activity; import android.Manifest; import android.content.Intent; import android.os.Build; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.View; import android.widget.ImageView; import com.bumptech.glide.Glide; import com.bumptech.glide.request.RequestOptions; import com.bwie.xiaqin.zhoukao03lx.R; import com.bwie.xiaqin.zhoukao03lx.mvp.contract.MainContract; import com.bwie.xiaqin.zhoukao03lx.mvp.presenter.MainPresenter; import com.bwie.xiaqin.zhoukao03lx.mvp.ui.adapter.MainAdapter; import com.bwie.xiaqin.zhoukao03lx.mvp.ui.bean.MainBean; import com.google.gson.Gson; import org.xutils.view.annotation.ContentView; import org.xutils.x; import java.util.ArrayList; import java.util.List; @ContentView(R.layout.activity_main) public class MainActivity extends AppCompatActivity implements MainContract.MainView, View.OnClickListener { private static final int CODE_REQUEST_LOGIN = 10; private static final String TAG = MainActivity.class.getSimpleName(); private ImageView ivHead; private RecyclerView recyclerView; private MainPresenter presenter; private List<MainBean.Pois> data = new ArrayList<>(); private MainAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); x.view().inject(this); if (Build.VERSION.SDK_INT >=Build.VERSION_CODES.M){ requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},200); } ivHead = findViewById(R.id.iv_head); ivHead.setOnClickListener(this); Glide.with(this).load(getResources().getDrawable(R.drawable.hdpi)).apply(new RequestOptions().circleCrop()).into(ivHead); recyclerView = findViewById(R.id.recycle_view); recyclerView.setLayoutManager(new LinearLayoutManager(this)); LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this); layoutManager.setOrientation(LinearLayoutManager.VERTICAL); recyclerView.setLayoutManager(layoutManager); adapter = new MainAdapter(this, data); recyclerView.setAdapter(adapter); // recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL)); presenter = new MainPresenter(); presenter.attach(this); presenter.loadData("https://restapi.amap.com/v3/place/around?key=d78f39012867929dc6ad174dd498f51f&location=116.473168,39.993015&keywords=%E7%BE%8E%E9%A3%9F&types=&radius=1000&offset=20&page=1&extensions=all"); } @Override protected void onResume() { super.onResume(); } @Override protected void onPause() { super.onPause(); } @Override public void onClick(View v) { switch (v.getId()){ // case R.id.iv_head: // toLogin(); // break; } } private void toLogin() { startActivityForResult(new Intent(this,LoginActivity.class),CODE_REQUEST_LOGIN); } @Override public void showList(String result) { Log.i(TAG,"result:"+result); Gson gson = new Gson(); MainBean mainBean = gson.fromJson(result, MainBean.class); data.addAll(mainBean.getPois()); adapter.notifyDataSetChanged(); } }
adapter里面的
MainAdapter
package com.bwie.xiaqin.zhoukao03lx.mvp.ui.adapter; import android.content.Context; import android.support.annotation.NonNull; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.ViewGroup; import com.bumptech.glide.Glide; import com.bwie.xiaqin.zhoukao03lx.R; import com.bwie.xiaqin.zhoukao03lx.mvp.ui.bean.MainBean; import com.bwie.xiaqin.zhoukao03lx.mvp.ui.holder.MainHodler; import java.util.List; /** * Created by lenovo on 2018/9/16. */ public class MainAdapter extends RecyclerView.Adapter<MainHodler>{ private Context context; private List<MainBean.Pois> poisBean; public MainAdapter(Context context, List<MainBean.Pois> poisBean) { this.context = context; this.poisBean = poisBean; } @NonNull @Override public MainHodler onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { return new MainHodler(LayoutInflater.from(context).inflate(R.layout.main_item,parent,false)); } @Override public void onBindViewHolder(@NonNull MainHodler holder, int position) { holder.tvTitle.setText(poisBean.get(position).getName()); holder.tvDestance.setText(poisBean.get(position).getDistance() + "m"); List<MainBean.Pois.Photo> photos = poisBean.get(position).getPhotos(); if (photos!=null&&photos.size()>0){ Glide.with(context).load(photos.get(0).getUrl()).into(holder.ivIcon); } } @Override public int getItemCount() { return poisBean.size(); } }
MainBean...省略
MainHolder
package com.bwie.xiaqin.zhoukao03lx.mvp.ui.holder; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.ImageView; import android.widget.TextView; import com.bwie.xiaqin.zhoukao03lx.R; /** * Created by lenovo on 2018/9/15. */ public class MainHodler extends RecyclerView.ViewHolder { public ImageView ivIcon; public TextView tvTitle; public TextView tvDestance; public MainHodler(View itemView) { super(itemView); ivIcon = itemView.findViewById(R.id.iv_icon); tvTitle = itemView.findViewById(R.id.tv_title); tvDestance = itemView.findViewById(R.id.tv_distance); } }
权限AndroidMainfest.xml
<uses-permission android:name="android.permission.INTERNET"/>
android:name=".mvp.contract.App"
build.gradle里面
implementation 'com.android.support:recyclerview-v7:27.1.1' implementation 'com.squareup.okhttp3:okhttp:3.11.0' implementation 'com.squareup.okhttp3:logging-interceptor:3.11.0' implementation 'com.github.bumptech.glide:glide:4.8.0' implementation 'com.google.code.gson:gson:2.2.4' /* Xutils */ implementation 'org.xutils:xutils:3.5.0'
res里面创建drawable
distance_bg.xml里面有:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <corners android:bottomLeftRadius="12dp"/> <solid android:color="#ccc"/> </shape>
ic_keyboard_arrow_down_black_24dp.xml
ic_launcher_background.xml
ic_location_on_black_24dp.xml
ic_search_black_24dp.xml
search_b.xml里面有
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <corners android:radius="30dp"/> <solid android:color="#5ccc"/> </shape>
res里面还创建了一个图片
drawable-hdpi包名 hdpi hdpi.jpg