Android Material风格的应用(二)--RecyclerView
添加RecyclerView
Android Material风格的应用(一)--AppBar TabLayout
Android Material风格的应用(二)--RecyclerView
Android Material风格的应用(三)--DrawerLayout
Android Material风格的应用(四)--FloatActionButton
Android Material风格的应用(五)--CollapsingToolbar

RecyclerView
工程源码
添加对应的支持库
compile 'com.android.support:recyclerview-v7:24.2.1'
compile 'com.android.support:cardview-v7:24.2.1'
添加 res/layout/recycler_view.xml
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recycler_view"
android:clipToPadding="false"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:scrollbars="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
</android.support.v7.widget.RecyclerView>
在ListContentFragment.java
TileContentFragment.java
和 CardContentFragment.java
中
分别创建各自的ContentAdapter,在onCreateView方法中初始化RecyclerView相关的内容
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
RecyclerView recyclerView = (RecyclerView)inflater.inflate(R.layout.recycler_view,container,false);
ContentAdapter adapter = new ContentAdapter(recyclerView.getContext());
recyclerView.setAdapter(adapter);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),2));
return recyclerView;
}
public static class ViewHolder extends RecyclerView.ViewHolder{
public ImageView imageView;
public TextView textView;
public ViewHolder(LayoutInflater inflater,ViewGroup parent) {
super(inflater.inflate(R.layout.item_tile,parent,false));
imageView = (ImageView)itemView.findViewById(R.id.title_picture);
textView = (TextView)itemView.findViewById(R.id.title_title);
}
}
public static class ContentAdapter extends RecyclerView.Adapter<ViewHolder>{
private static final int LENGTH = 18;
private String[] names;
private Drawable[] avatars;
public ContentAdapter(Context context) {
Resources resources = context.getResources();
names = resources.getStringArray(R.array.places);
TypedArray a = resources.obtainTypedArray(R.array.places_picture);
avatars = new Drawable[a.length()];
for(int i = 0;i<a.length();i++){
avatars[i] = a.getDrawable(i);
}
a.recycle();
}
@Override
public int getItemCount() {
return LENGTH;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.imageView.setImageDrawable(avatars[position % avatars.length]);
holder.textView.setText(names[position % names.length]);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()),parent);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!