转载请注明出处
作者:developerHaoz
Github 地址:developerHaoz
本文为 手把手教你从零开始做一个好看的 APP - Day five ,如果想看该系列的其他文章,请点击以下连接
手把手教你从零开始做一个好看的 APP - Day three
手把手教你从零开始做一个好看的 APP - Day four
手把手教你从零开始做一个好看的 APP - Day five
Day five
一、段子数据的获取
段子数据的获取其实跟妹子模块的方法基本一样
先编写实体类
public class DuanziBean {
@SerializedName("group")
private GroupBean groupBean;
private String type;
public GroupBean getGroupBean() {
return groupBean;
}
public void setGroupBean(GroupBean groupBean) {
this.groupBean = groupBean;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
public class GroupBean {
private String text;
private long id;
private UserBean user;
public String getText() {
return text;
}
public long getId() {
return id;
}
public UserBean getUser() {
return user;
}
public static class UserBean {
private long user_id;
private String name;
private String avatar_url;
public String getName() {
return name;
}
public String getAvatar_url() {
return avatar_url;
}
}
}
写好实体类之后,使用我们之前已经封装好的网络请求工具以及解析工具,便能将返回的数据,解析成一个包含段子实体类的 List。
二、段子的显示
老规矩,先写个 RecyclerView 的 Item
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:paddingLeft="8dp"
>
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/duanzi_civ_avatar"
android:layout_width="24dp"
android:layout_height="24dp"
android:src="@drawable/avatar"
android:layout_gravity="center"
/>
<TextView
android:id="@+id/duanzi_tv_author"
android:paddingLeft="8dp"
android:paddingStart="8dp"
android:layout_width="match_parent"
android:layout_height="16dp"
android:text="DeveloperHaoz"
android:layout_gravity="center_vertical"
/>
</LinearLayout>
<TextView
android:id="@+id/duanzi_tv_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingLeft="40dp"
android:paddingRight="10dp"
android:text=""
/>
<include layout="@layout/layout_app_divide"/>
</LinearLayout>
然后编写将数据和界面进行绑定的 Adapter
public class DuanziAdapter extends RecyclerView.Adapter<DuanziAdapter.DuanziViewHolder>{
private Fragment mFragment;
private List<DuanziBean> mDuanziBeanList;
public DuanziAdapter(Fragment fragment, List<DuanziBean> duanziBeanList){
this.mFragment = fragment;
this.mDuanziBeanList = duanziBeanList;
}
@Override
public DuanziViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_duanzi, null);
return new DuanziViewHolder(view);
}
@Override
public void onBindViewHolder(DuanziViewHolder holder, int position) {
try {
DuanziBean duanziBean = mDuanziBeanList.get(position);
Glide.with(mFragment).load(duanziBean.getGroupBean().getUser().getAvatar_url()).into(holder.mCivAvatar);
holder.mTvContent.setText(duanziBean.getGroupBean().getText());
holder.mTvAuthor.setText(duanziBean.getGroupBean().getUser().getName());
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public int getItemCount() {
return mDuanziBeanList.size();
}
public static class DuanziViewHolder extends RecyclerView.ViewHolder{
private CircleImageView mCivAvatar;
private TextView mTvAuthor;
private TextView mTvContent;
public DuanziViewHolder(View itemView) {
super(itemView);
mCivAvatar = (CircleImageView) itemView.findViewById(R.id.duanzi_civ_avatar);
mTvAuthor = (TextView) itemView.findViewById(R.id.duanzi_tv_author);
mTvContent = (TextView) itemView.findViewById(R.id.duanzi_tv_content);
}
}
}
最后段子页面中进行数据和获取以及界面的初始化
public class DuanziFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_duanzi, container, false);
ButterKnife.bind(this, view);
initView();
initRefresh();
return view;
}
private void initRefresh() {
mRefresh.setColorSchemeResources(R.color.colorPrimary);
mRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
initView();
mRefresh.setRefreshing(false);
}
});
}
private void initView() {
VolleyHelper.sendHttpGet(getActivity(), DuanziApi.GET_DUANZI, new VolleyResponseCallback() {
@Override
public void onSuccess(String response) {
List<DuanziBean> mDuanziBeanList = GsonHelper.getDuanziBeanList(response);
mDuanziBeanList.remove(3);
mRvShowDuanzi.setLayoutManager(new LinearLayoutManager(getActivity()));
mRvShowDuanzi.setAdapter(new DuanziAdapter(DuanziFragment.this, mDuanziBeanList));
}
@Override
public void onError(VolleyError error) {
Logger.d(error);
}
});
}
}