今天我们来使用RecyclerView显示最新上映的电影,显示最新上映的电影顾名思义我们需要去请求服务器接口然后解析显示。
效果图如下:
新建一个项目名为RecyclerViewTest然后在build.gradle中添加以下依赖库:
compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
compile 'com.squareup.okhttp3:okhttp:3.8.0'
compile 'com.github.bumptech.glide:glide:3.7.0'
接着在AndroidManifest.xml中添加访问网络权限
<uses-permission android:name="android.permission.INTERNET"/>
然后开始编写我们的布局文件activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="com.ranlegeran.recyclerviewtest.MainActivity"
android:background="#FFFFFF">
<android.support.v7.widget.RecyclerView
android:id="@+id/mRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
film_item.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="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="10dp">
<ImageView
android:id="@+id/img_item_pic"
android:layout_width="120dp"
android:layout_height="120dp"
android:src="@drawable/bg"/>
<TextView
android:id="@+id/text_item_film_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_toRightOf="@id/img_item_pic"
android:text="后来的我们"
android:textSize="17sp"/>
<TextView
android:id="@+id/text_item_released_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_below="@+id/text_item_film_name"
android:layout_toRightOf="@id/img_item_pic"
android:text="2018-04-28上映"
android:textSize="16sp"/>
<TextView
android:id="@+id/text_item_score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="15dp"
android:text="8.0分"
android:textSize="15sp"/>
<TextView
android:id="@+id/text_item_starring"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_toRightOf="@id/img_item_pic"
android:layout_below="@+id/text_item_released_time"
android:text="主演:"
android:textSize="16sp"/>
</RelativeLayout>
</LinearLayout>
接着我们需要创建一个适配器代码如下:
package com.ranlegeran.recyclerviewtest;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* Created by RANLEGERAN on 2018/4/30.
*/
public class FilmAdapter extends RecyclerView.Adapter<FilmAdapter.ViewHolder> {
public List<Map<String,Object>> mList = new ArrayList<>();
private Context mContext;
private LayoutInflater mLayoutInflater;
public FilmAdapter(List<Map<String,Object>> list, Context context) {
this.mList = list;
this.mContext = context;
mLayoutInflater = LayoutInflater.from(context);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mLayoutInflater.inflate(R.layout.film_item, null);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Glide.with(mContext).load(mList.get(position).get("img")).into(holder.mImgPic);
holder.mTextFilmName.setText(mList.get(position).get("nm").toString());
holder.mTextReleasedTime.setText(mList.get(position).get("rt").toString());
holder.mTextScore.setText(mList.get(position).get("sc").toString());
holder.mTextStarring.setText(mList.get(position).get("star").toString());
}
@Override
public int getItemCount() {
return mList.size(); //返回的数量
}
class ViewHolder extends RecyclerView.ViewHolder {
ImageView mImgPic;
TextView mTextFilmName;
TextView mTextReleasedTime;
TextView mTextScore;
TextView mTextStarring;
public ViewHolder(View itemView) {
super(itemView);
mImgPic = itemView.findViewById(R.id.img_item_pic);
mTextFilmName = itemView.findViewById(R.id.text_item_film_name);
mTextReleasedTime = itemView.findViewById(R.id.text_item_released_time);
mTextScore = itemView.findViewById(R.id.text_item_score);
mTextStarring = itemView.findViewById(R.id.text_item_starring);
}
}
}
用到的网络请求工具类如下:
package com.ranlegeran.recyclerviewtest;
import okhttp3.OkHttpClient;
import okhttp3.Request;
/**
* Created by RANLEGERAN on 2018/4/30.
*/
public class OkHttpUtils {
public static void sendOkHttpRequest(String address, okhttp3.Callback callback) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(address).build();
client.newCall(request).enqueue(callback);
}
}
最后就是我们的MainActivity部分如下:
package com.ranlegeran.recyclerviewtest;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity {
private static final String URL = "http://m.maoyan.com/movie/list.json?type=hot&offset=0&limit=1000";
private RecyclerView mRecyclerView;
private String mResult;
public List<Map<String,Object>> mList = new ArrayList<>();
public Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
mRecyclerView.addItemDecoration(new DividerItemDecoration(MainActivity.this, DividerItemDecoration.VERTICAL));
FilmAdapter mAdapter = new FilmAdapter(mList, MainActivity.this);
mRecyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
mRecyclerView.setAdapter(mAdapter);
break;
default:
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = (RecyclerView) findViewById(R.id.mRecyclerView);
GetData();
}
private void GetData() {
new Thread(new Runnable() {
@Override
public void run() {
OkHttpUtils.sendOkHttpRequest(URL, new Callback() {
@Override
public void onResponse(Call call, Response response) throws IOException {
mResult = response.body().string();
ToShowData(mResult);
}
@Override
public void onFailure(Call call, IOException e) {
}
});
}
}).start();
}
private void ToShowData(String result) {
try {
JSONObject obj = new JSONObject(result);
JSONObject jsonData = obj.getJSONObject("data");
JSONArray jsonArray = jsonData.getJSONArray("movies");
for (int i = 0; i < jsonArray.length(); i++) {
obj = jsonArray.getJSONObject(i);
Map<String,Object> map = new HashMap<>();
String img = obj.getString("img");
String nm = obj.getString("nm");
String sc = obj.getString("sc");
String rt = obj.getString("rt");
String star = obj.getString("star");
map.put("img", img);
map.put("nm", nm);
map.put("sc", sc+"分");
map.put("rt", rt);
map.put("star", "主演:"+star);
mList.add(map);
}
Message msg = new Message();
msg.what = 1;
handler.sendMessage(msg);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
好了,运行一下看看吧。