recyclerView

public class MainActivity extends AppCompatActivity implements View.OnClickListener,MainContract.MainView {
    private static final int CODE_REQUEST_LOGIN = 1000;
    private ImageView ivHead;
    private RecyclerView recyclerView;

    private MainPresenter presenter;

    private List<MainBean.Pois>datas=new ArrayList<>();
    private MainAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

     
        ivHead=  findViewById(R.id.iv_head);
        ivHead.setOnClickListener(this );
        recyclerView=findViewById(R.id.recycle_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new MainAdapter(this,datas);
        recyclerView.setAdapter(adapter);
  /
        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
    public void showList(String result) {
        Log.i("sss",toString());
        Gson gson=new Gson();
         MainBean mainBean=gson.fromJson(result,MainBean.class);
         datas.addAll(mainBean.getPois());
         adapter.notifyDataSetChanged();

    }
}
--------------------------------
public class MainAdapter extends RecyclerView.Adapter<MainHodel> {
private Context context;
private List<MainBean.Pois> poisBeans;

    public MainAdapter(Context context, List<MainBean.Pois> poisBeans) {
        this.context = context;
        this.poisBeans = poisBeans;
    }

    @NonNull
    @Override
    public MainHodel onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return  new MainHodel(LayoutInflater.from(context).inflate(R.layout.main_title, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MainHodel holder, int position) {
        holder.tv_title.setText(poisBeans.get(position).getName());
        holder.tvDestance.setText(poisBeans.get(position).getDistance() + "m");
        List<MainBean.Pois.Photo> photos = poisBeans.get(position).getPhotos();
        if (photos != null && photos.size() > 0) {
            Glide.with(context)
                    .load(photos.get(0).getUrl())
                    .into(holder.iv_icon);
        }
    }

    @Override
    public int getItemCount() {
        return poisBeans.size();
    }
}

-*************----------------------------------------

public class MainHodel extends RecyclerView.ViewHolder {
    public ImageView iv_icon;
    public TextView tv_title;
     public TextView tvDestance;
    public MainHodel(View itemView) {
        super(itemView);
        iv_icon = itemView.findViewById(R.id.iv_icon);
        tv_title = itemView.findViewById(R.id.tv_title);
        tvDestance = itemView.findViewById(R.id.tv_distance);
    }
}
------------------------------------------------------

public class MainBean {
    private List<Pois> pois;

    public List<Pois> getPois() {
        return pois;
    }

    public void setPois(List<Pois> pois) {
        this.pois = pois;
    }

    public class Pois {
        private String name;

        private String distance;

        public String getDistance() {
            return distance;
        }

        public void setDistance(String distance) {
            this.distance = distance;
        }

        private List<Photo> photos;

        public List<Photo> getPhotos() {
            return photos;
        }

        public void setPhotos(List<Photo> photos) {
            this.photos = photos;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public class Photo {
            private String url;

            public String getUrl() {
                return url;
            }

            public void setUrl(String url) {
                this.url = url;
            }
        }
    }
}
------------------------------------------------

public class MainHodel extends RecyclerView.ViewHolder {
    public MainHodel(View itemView) {
        super(itemView);
    }
}
----------------------------------------

public interface IPresenter <V extends IView> {
    void  attach( V v);
    void   datach();
}
-------------------------------------------

public interface MainContract {
    interface MainView extends IView{
         void  showList(String result);
    }
    interface MainPresenter extends IPresenter <MainView>{
   void  loadData(String url);
    }
    interface MainMode extends IMode {

        interface  NetCallback {
 void  chenggong(String result);
 void  shibai (String meg);
        }
        void  loadData(String url,NetCallback callback);
    }

}
---------------------------------------

public class OKHttpUtils {
    public  interface  NetCallback{
        void  chenggong(String result);
        void  shibai (String meg);
    }
    private Handler mhandler;
    private OkHttpClient mokHttpUtils;
    {
        /* 日志拦截器 */
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        /* OKHttpClient */
        mokHttpUtils=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();
        mokHttpUtils.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.shibai(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.chenggong(response.body().string());
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
            }
        });
    }

    public void post() {

    }

}
---------------------------------------------

public class MainMode implements MainContract.MainMode {
    private OKHttpUtils utils;

    public MainMode() {
     utils=OKHttpUtils.getInstance();
    }

    @Override
    public void loadData(String url, final NetCallback callback) {
  utils.get(url, new OKHttpUtils.NetCallback() {
      @Override
      public void chenggong(String result) {
          callback.chenggong(result);
      }

      @Override
      public void shibai(String meg) {
   callback.shibai(meg);
      }
  });
    }
}
----------------------------------------------

public class MainPresenter implements MainContract.MainPresenter {
  private WeakReference<MainContract.MainView> viewWeakReference;
  private  WeakReference<MainMode> modeWeakReference;

    @Override
    public void loadData(String url) {
      modeWeakReference.get().loadData(url, new MainContract.MainMode.NetCallback() {
          @Override
          public void chenggong(String result) {
               viewWeakReference.get().showList(result);
          }

          @Override
          public void shibai(String meg) {

          }
      });
    }

    @Override
    public void attach(MainContract.MainView mainView) {
    viewWeakReference =new WeakReference(mainView);
     modeWeakReference=new WeakReference(new MainMode());
    }

    @Override
    public void datach() {
    if (viewWeakReference !=null){
         viewWeakReference.clear();
         viewWeakReference=null;
         modeWeakReference.clear();
         modeWeakReference=null;
    }
    }
}
---------------------布局---------------------------
主界面布局<android.support.v7.widget.RecyclerView
    android:layout_margin="15dp"
    android:id="@+id/recycle_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
---------
title界面布局
<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"

            >

            <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>
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值