1、布局文件中使用
1 <android.support.v7.widget.RecyclerView
2 android:id="@+id/recycleview"
3 android:paddingLeft="10dp"
4 android:paddingRight="10dp"
5 android:layout_width="match_parent"
6 android:layout_height="match_parent"
7 android:gravity="center">
8 </android.support.v7.widget.RecyclerView>
2、代码中使用
1 recyclerView = (RecyclerView)findViewById(R.id.recycleview);
2 recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 4)) ; //gridView网格布局
3 recyclerView.setLayoutManager(new LinearLayoutManager(this)); //listView线性布局
3、适配器
1 package com.yiba.files.adapter; 2 3 import android.content.Context; 4 import android.graphics.drawable.BitmapDrawable; 5 import android.support.v7.widget.RecyclerView; 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.view.ViewGroup; 9 import android.widget.ImageView; 10 import android.widget.RelativeLayout; 11 import android.widget.TextView; 12 13 import com.yiba.files.R; 14 import com.yiba.files.model.DataItem; 15 import com.yiba.files.util.DensityUtils; 16 import com.yiba.files.util.DeviceUtils; 17 18 import java.util.List; 19 20 /** 21 * Created by ${zyj} on 2016/1/23. 22 */ 23 public class AppAdapter extends RecyclerView.Adapter<AppAdapter.MyViewHolder> { 24 private Context context; 25 private List<DataItem> list; 26 private RelativeLayout.LayoutParams params; 27 28 public interface OnItemClickListener { 29 void onItemClick(View view, int position); 30 } 31 32 private OnItemClickListener onItemClickListener; 33 34 public void setOnItemClickListener(OnItemClickListener listener) { 35 onItemClickListener = listener; 36 } 37 38 public AppAdapter(Context context, List<DataItem> list) { 39 this.context = context; 40 this.list = list; 41 } 42 43 @Override 44 public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 45 MyViewHolder holder = new MyViewHolder(LayoutInflater.from(context).inflate( 46 R.layout.fragment_app_item, null)); 47 return holder; 48 } 49 50 @Override 51 public void onBindViewHolder(final MyViewHolder holder, 52 final int position) { 53 DataItem info = list.get(position); 54 String tag = (String) holder.imageView.getTag(); 55 56 if (!info.getName().equals(tag)) { 57 holder.imageView.setImageBitmap(((BitmapDrawable) list.get(position) 58 .getImage()).getBitmap()); 59 holder.imageView.setTag(info.getName()); 60 } 61 62 if (info.isSelect()) { 63 holder.imageView.setAlpha(0.3f); 64 holder.appName.setAlpha(0.3f); 65 holder.appSize.setAlpha(0.3f); 66 holder.app_choice.setVisibility(View.VISIBLE); 67 } else { 68 holder.imageView.setAlpha(1.0f); 69 holder.appName.setAlpha(1.0f); 70 holder.appSize.setAlpha(1.0f); 71 holder.app_choice.setVisibility(View.GONE); 72 } 73 74 holder.appName.setText(info.getDisplayName()); 75 holder.appSize.setText("" + DeviceUtils.convertByte(info.getSize())); 76 77 holder.itemView.setOnClickListener(new View.OnClickListener() { 78 @Override 79 public void onClick(View v) { 80 if (onItemClickListener != null) { 81 int pos = holder.getLayoutPosition(); 82 onItemClickListener.onItemClick(holder.itemView, pos); 83 } 84 } 85 }); 86 } 87 88 @Override 89 public int getItemCount() { 90 if (list != null) { 91 return list.size(); 92 } else { 93 return 0; 94 } 95 } 96 97 public DataItem getItem(int position) { 98 return list.get(position); 99 } 100 101 class MyViewHolder extends RecyclerView.ViewHolder { 102 ImageView imageView; 103 ImageView app_choice; 104 TextView appName; 105 TextView appSize; 106 107 public MyViewHolder(View view) { 108 super(view); 109 imageView = (ImageView) view.findViewById(R.id.appImage); 110 111 params = (RelativeLayout.LayoutParams) imageView.getLayoutParams(); 112 params.width = DensityUtils.getScreenWidth(context) / 6; 113 params.height = params.width; 114 imageView.setLayoutParams(params); 115 116 appName = (TextView) view.findViewById(R.id.appName); 117 appSize = (TextView) view.findViewById(R.id.appSize); 118 app_choice = (ImageView) view.findViewById(R.id.appSelect_image); 119 } 120 } 121 }
更新数据
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/1118/2004.html