1. 项目中涉及技术点
- activity之间跳转传值
- RecyclerView+adapter获取用户影评列表(第九集)
2. 具体代码实现
- activity_movie_detail.xml布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MovieDetailActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/purple_200"
app:navigationIcon="@drawable/ic_baseline_arrow_back_ios_24"
app:title="详情"
app:titleTextColor="@color/white" />
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="210dp"
android:layout_below="@id/toolbar">
<ImageView
android:layout_width="match_parent"
android:layout_height="188dp"
android:src="@drawable/movie_details_bg" />
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="150dp"
android:layout_height="match_parent"
android:background="@color/white"
android:padding="6dp">
<ImageView
android:id="@+id/img_cover"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@mipmap/img_1" />
</androidx.appcompat.widget.LinearLayoutCompat>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="抓娃娃"
android:textColor="@color/white"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_types"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:singleLine="true"
android:text="喜剧 剧情"
android:textColor="@color/white"
android:textSize="13sp" />
<TextView
android:id="@+id/duration_region"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:singleLine="true"
android:text="中国大陆 / 133分钟"
android:textColor="@color/white"
android:textSize="13sp" />
<TextView
android:id="@+id/tv_actors"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:maxLines="2"
android:text="沈腾/马丽/常远/艾伦"
android:textColor="@color/white"
android:textSize="13sp" />
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp">
<TextView
android:id="@+id/tv_rate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9.5"
android:textColor="#ffb400"
android:textSize="30sp"
android:textStyle="italic" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" 分"
android:textColor="@color/white" />
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.appcompat.widget.LinearLayoutCompat>
</FrameLayout>
<View
android:id="@+id/line"
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_below="@id/frameLayout"
android:layout_marginTop="10dp"
android:background="#f5f5f5" />
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/line"
android:padding="10dp"
android:text="全部评论"
android:textColor="#333333"
android:textSize="17sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/submit"
android:layout_below="@id/title">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="@layout/comments_list_item" />
<androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/ll_empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/img_empty" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="空空如也~~"
android:textColor="#999999" />
</androidx.appcompat.widget.LinearLayoutCompat>
</RelativeLayout>
<TextView
android:id="@+id/submit"
android:layout_width="match_parent"
android:layout_height="44dp"
android:layout_alignParentBottom="true"
android:layout_margin="16dp"
android:background="@color/purple_200"
android:gravity="center"
android:text="立即购票"
android:textColor="@color/white" />
</RelativeLayout>
- MovieDetailActivity.java
public class MovieDetailActivity extends AppCompatActivity {
private static final String URL = "https://movie.douban.com/j/subject_abstract";
private MovieInfo.SubjectsDTO subjectsDTO;
private ImageView img_cover;
private TextView tv_rate;
private TextView tv_title;
private TextView tv_types;
private TextView duration_region;
private TextView tv_actors;
private LinearLayoutCompat ll_empty;
private RecyclerView recyclerView;
private CommentListAdapter mCommentListAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_movie_detail);
//获取跳转传值
subjectsDTO = (MovieInfo.SubjectsDTO) getIntent().getSerializableExtra("subjectsDTO");
//初始化控件
img_cover = findViewById(R.id.img_cover);
tv_title = findViewById(R.id.tv_title);
tv_rate = findViewById(R.id.tv_rate);
tv_types = findViewById(R.id.tv_types);
duration_region = findViewById(R.id.duration_region);
tv_actors = findViewById(R.id.tv_actors);
recyclerView = findViewById(R.id.recyclerView);
ll_empty = findViewById(R.id.ll_empty);
//初始化适配器
mCommentListAdapter = new CommentListAdapter();
//设置适配器
recyclerView.setAdapter(mCommentListAdapter);
//加载评论数据
loadCommentData();
if (null != subjectsDTO) {
Glide.with(this).load(subjectsDTO.getCover()).into(img_cover);
tv_rate.setText(subjectsDTO.getRate());
tv_title.setText(subjectsDTO.getTitle());
getMovieDetail();
}
//返回
findViewById(R.id.toolbar).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
findViewById(R.id.submit).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MovieDetailActivity.this, CinemaListActivity.class);
intent.putExtra("subjectsDTO", subjectsDTO);
startActivity(intent);
}
});
}
private void loadCommentData() {
List<CommentInfo> commentInfoList = CommentDbHelper.getInstance(this).queryCommentByMovieName(subjectsDTO.getTitle());
mCommentListAdapter.setCommentInfoList(commentInfoList);
//是否显示空布局
ll_empty.setVisibility(commentInfoList.size() > 0 ? View.GONE : View.VISIBLE);
}
private void getMovieDetail() {
OkGo.<String>get(URL)
.params("subject_id", subjectsDTO.getId())
.execute(new StringCallback() {
@Override
public void onStart(Request<String, ? extends Request> request) {
ProgressDialogUtils.showProgressDialog(MovieDetailActivity.this);
}
@Override
public void onSuccess(Response<String> response) {
MovieDetailsInfo movieDetailsInfo = new Gson().fromJson(response.body(), MovieDetailsInfo.class);
if (null != movieDetailsInfo) {
duration_region.setText(movieDetailsInfo.getSubject().getRegion() + " / " + movieDetailsInfo.getSubject().getDuration());
List<String> types = movieDetailsInfo.getSubject().getTypes();
String result = "";
for (String str : types) {
result += str + "/";
}
result = result.substring(0, result.length() - 1);
tv_types.setText(result);
List<String> actors = movieDetailsInfo.getSubject().getActors();
String result2 = "";
for (String str : actors) {
result2 += str + "/";
}
result2 = result2.substring(0, result2.length() - 1);
tv_actors.setText("主演 " + result2);
}
}
@Override
public void onFinish() {
ProgressDialogUtils.hideProgressDialog();
}
});
}
}
3. 实现效果图
温馨提示:影片详情中包含了用户评论列表,第九集具体说明