RecyclerView的基本使用

时间:2023-03-08 17:00:40

1、布局文件中使用

  <android.support.v7.widget.RecyclerView
android:id="@+id/recycleview"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
</android.support.v7.widget.RecyclerView>

2、代码中使用

   recyclerView = (RecyclerView)findViewById(R.id.recycleview);
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 4)) ; //gridView网格布局
recyclerView.setLayoutManager(new LinearLayoutManager(this)); //listView线性布局

3、适配器

 package com.yiba.files.adapter;

 import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
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.RelativeLayout;
import android.widget.TextView; import com.yiba.files.R;
import com.yiba.files.model.DataItem;
import com.yiba.files.util.DensityUtils;
import com.yiba.files.util.DeviceUtils; import java.util.List; /**
* Created by ${zyj} on 2016/1/23.
*/
public class AppAdapter extends RecyclerView.Adapter<AppAdapter.MyViewHolder> {
private Context context;
private List<DataItem> list;
private RelativeLayout.LayoutParams params; public interface OnItemClickListener {
void onItemClick(View view, int position);
} private OnItemClickListener onItemClickListener; public void setOnItemClickListener(OnItemClickListener listener) {
onItemClickListener = listener;
} public AppAdapter(Context context, List<DataItem> list) {
this.context = context;
this.list = list;
} @Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
MyViewHolder holder = new MyViewHolder(LayoutInflater.from(context).inflate(
R.layout.fragment_app_item, null));
return holder;
} @Override
public void onBindViewHolder(final MyViewHolder holder,
final int position) {
DataItem info = list.get(position);
String tag = (String) holder.imageView.getTag(); if (!info.getName().equals(tag)) {
holder.imageView.setImageBitmap(((BitmapDrawable) list.get(position)
.getImage()).getBitmap());
holder.imageView.setTag(info.getName());
} if (info.isSelect()) {
holder.imageView.setAlpha(0.3f);
holder.appName.setAlpha(0.3f);
holder.appSize.setAlpha(0.3f);
holder.app_choice.setVisibility(View.VISIBLE);
} else {
holder.imageView.setAlpha(1.0f);
holder.appName.setAlpha(1.0f);
holder.appSize.setAlpha(1.0f);
holder.app_choice.setVisibility(View.GONE);
} holder.appName.setText(info.getDisplayName());
holder.appSize.setText("" + DeviceUtils.convertByte(info.getSize())); holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onItemClickListener != null) {
int pos = holder.getLayoutPosition();
onItemClickListener.onItemClick(holder.itemView, pos);
}
}
});
} @Override
public int getItemCount() {
if (list != null) {
return list.size();
} else {
return 0;
}
} public DataItem getItem(int position) {
return list.get(position);
} class MyViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
ImageView app_choice;
TextView appName;
TextView appSize; public MyViewHolder(View view) {
super(view);
imageView = (ImageView) view.findViewById(R.id.appImage); params = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
params.width = DensityUtils.getScreenWidth(context) / 6;
params.height = params.width;
imageView.setLayoutParams(params); appName = (TextView) view.findViewById(R.id.appName);
appSize = (TextView) view.findViewById(R.id.appSize);
app_choice = (ImageView) view.findViewById(R.id.appSelect_image);
}
}
}

更新数据

http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/1118/2004.html