1.参考博客地址
http://blog.csdn.net/lmj623565791/article/details/45059587 Android RecyclerView 使用完全解析 体验艺术般的控件
http://blog.csdn.net/skykingf/article/details/50827141 Android开发之RecyclerView的使用全解
http://www.cnblogs.com/tianzhijiexian/p/4066589.htmlAndroid5.0新控件RecyclerVIew的介绍和兼容使用的方法
2. 学习实践
1)UI设计,主布局采用relativelayout,actionbar + recycleview,recycleview子布局采用linerlayout
<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.npsmaster.learnrecyclerview.MainActivity">
<android.support.v7.widget.RecyclerView
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#ffff0000"
android:dividerHeight="10dp"
android:id="@+id/myrcv" />
</RelativeLayout>
itemview的设计
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="10dp" android:orientation="horizontal" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <ImageView android:layout_width="80dp" android:layout_height="80dp" android:layout_gravity="bottom" app:srcCompat="@mipmap/icon" android:id="@+id/imageView"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="20dp" android:orientation="vertical" > <TextView android:id="@+id/tvTitle" android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center_vertical" android:text="Title" /> <TextView android:id="@+id/tvDetail" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginTop="5dp" android:text="2015-7-29 14:12:21" /> </LinearLayout> </LinearLayout> </LinearLayout>
2)代码设计,主程序基于appcompatActivity
package com.npsmaster.learnrecyclerview; import java.util.ArrayList; import java.util.List; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.DefaultItemAnimator; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.OrientationHelper; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private List<MyListData> itemsdata; private MyRecycleViewAdapter myRecycleViewAdapter; private RecyclerView recyclerView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initData(); recyclerView = (RecyclerView)findViewById(R.id.myrcv); //线式布局类似 listview LinearLayoutManager layoutManager = new LinearLayoutManager(this); layoutManager.setOrientation(OrientationHelper.VERTICAL); recyclerView.setLayoutManager(layoutManager); //Gride布局 类似Gridview //recyclerView .setLayoutManager(new GridLayoutManager( this,4)); myRecycleViewAdapter = new MyRecycleViewAdapter(MainActivity.this,itemsdata); myRecycleViewAdapter.setOnItemClickLitener(new MyRecycleViewAdapter.OnItemClickLitener() { @Override public void onItemClick(View view, int position) { Toast.makeText(MainActivity.this, position+"", Toast.LENGTH_SHORT) .show(); } }); recyclerView.setAdapter(myRecycleViewAdapter); recyclerView.setItemAnimator(new DefaultItemAnimator()); } protected void initData() { itemsdata = new ArrayList<MyListData>(); itemsdata.add(new MyListData(R.mipmap.icon,"PhoneInfo","Show the current phone information")); itemsdata.add(new MyListData(R.mipmap.icon_file,"FieldTest","Save the test track for analysis")); itemsdata.add(new MyListData(R.mipmap.icon_music,"SiteInfo","Show the surrounding sites information")); itemsdata.add(new MyListData(R.mipmap.icon,"Longitude&Latitude","Record the longitude and latitude")); itemsdata.add(new MyListData(R.mipmap.ic_launcher,"About","Software & license information")); } }
数据类
package com.npsmaster.learnrecyclerview; import java.io.Serializable; public class MyListData implements Serializable { private static final long serialVersionID =1L; private int imageId; private String title; private String detail; public MyListData(int imageId,String title,String detail) { super(); this.detail = detail; this.imageId = imageId; this.title = title; } public int getImageId() { return imageId; } public void setImageId(int imageid) { this.imageId = imageId; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getDetail() { return detail; } public void setDetail(String detail) { this.detail = detail; } }
recycleview适配器
package com.npsmaster.learnrecyclerview; 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 java.util.List; class MyRecycleViewAdapter extends RecyclerView.Adapter<MyRecycleViewAdapter.MyViewHolder> { private List<MyListData> itemsData; private Context mContext; private LayoutInflater inflater; private OnItemClickLitener mOnItemClickLitener; public interface OnItemClickLitener { void onItemClick(View view, int position); } public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) { this.mOnItemClickLitener = mOnItemClickLitener; } public MyRecycleViewAdapter(Context context,List<MyListData> itemsData) { this.itemsData = itemsData; this.mContext=context; inflater=LayoutInflater.from(mContext); } public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = inflater.inflate(R.layout.item_main, parent, false); MyViewHolder holder = new MyViewHolder(view); return holder; } @Override public void onBindViewHolder(final MyViewHolder holder, final int position) { holder.tvTitle.setText(itemsData.get(position).getTitle()); holder.tvDetail.setText(itemsData.get(position).getDetail()); holder.imageView.setImageResource(itemsData.get(position).getImageId()); if (mOnItemClickLitener != null) { holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mOnItemClickLitener.onItemClick(holder.itemView, position); } }); } } @Override public int getItemCount() { return itemsData.size(); } public static class MyViewHolder extends RecyclerView.ViewHolder { public TextView tvTitle; public TextView tvDetail; public ImageView imageView; public MyViewHolder(View view) { super(view); tvTitle = (TextView)view.findViewById(R.id.tvTitle); tvDetail = (TextView)view.findViewById(R.id.tvDetail); imageView = (ImageView) view.findViewById(R.id.imageView); } } }