1、添加recycleview依赖
compile('com.android.support:recyclerview-v7:25.1.1') {force = true
}
2、item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:melove="http://schemas.android.com/apk/res-auto"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><RelativeLayoutandroid:id="@+id/item_company_member_layout"android:layout_width="80dp"android:layout_height="90dp"><ImageViewandroid:id="@+id/item_information_choose_image"android:layout_width="15dp"android:layout_height="15dp"android:layout_alignParentRight="true"android:layout_marginRight="12dp"android:src="@drawable/ic_launcher_background"android:visibility="invisible"/><ImageViewandroid:id="@+id/item_company_member_head_image"android:layout_width="44dp"android:layout_height="44dp"android:layout_below="@+id/item_information_choose_image"android:layout_centerHorizontal="true"android:scaleType="centerCrop"android:src="@drawable/ic_launcher_background"/><TextViewandroid:id="@+id/item_company_member_name"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="8dp"android:layout_below="@+id/item_company_member_head_image"android:layout_centerHorizontal="true"android:text="林晓"android:textSize="12sp"/></RelativeLayout></LinearLayout>
3、entity.class
/*** 作者:created by meixi* 邮箱:13164716840@163.com* 日期:2018/8/30 10*/
public class TestEntity {private String title;private String https;private boolean ifshow;public boolean isIfshow() {return ifshow;}public void setIfshow(boolean ifshow) {this.ifshow = ifshow;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getHttps() {return https;}public void setHttps(String https) {this.https = https;}
}
4、adapter.class
public class NewgroupsAdapter extends RecyclerView.Adapter<NewgroupsAdapter.ViewHolder> {private List<TestEntity> entityList;private Context context;private OnItemClickListener mOnItemClickListener;private OnItemClickListenerupd mOnItemClickListenerupd;private Boolean is_edit = false;public NewgroupsAdapter(Context context, List<TestEntity> list) {this.context = context;this.entityList = list;}public void setEdit() {//activity刷新adapter方法is_edit = !is_edit;notifyDataSetChanged();}@Overridepublic ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {View view = LayoutInflater.from(context).inflate(R.layout.item_add_groups_number, null);ViewHolder holder = new ViewHolder(view);holder.imageView = (ImageView)view.findViewById(R.id.item_information_choose_image);holder.textView = (TextView) view.findViewById(R.id.item_company_member_name);return holder;}@Overridepublic void onBindViewHolder(final ViewHolder holder, final int position) {final TestEntity entity = entityList.get(position);if (is_edit) {holder.imageView.setVisibility(View.VISIBLE);} else {holder.imageView.setVisibility(View.GONE);}holder.textView.setText(entity.getTitle());//标题holder.itemView.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {mOnItemClickListenerupd.onItemClick(position);}});holder.textView.setOnClickListener(new View.OnClickListener() {//点击删除@Overridepublic void onClick(View view) {if (is_edit) {}else {mOnItemClickListener.onItemClick(holder.itemView, position); // 2}}});}@Overridepublic int getItemCount() {return entityList.size();}public class ViewHolder extends RecyclerView.ViewHolder {public ImageView imageView;public TextView textView;public ViewHolder(View view) {super(view);}}public interface OnItemClickListener {void onItemClick(View view, int position);}public void setOnItemminiClickListener(OnItemClickListener mOnItemClickListener) {//元素点击this.mOnItemClickListener = mOnItemClickListener;}public interface OnItemClickListenerupd {void onItemClick( int position);}public void setOnItemClickListener(OnItemClickListenerupd mOnItemClickListener) {//item点击this.mOnItemClickListenerupd = mOnItemClickListener;}}
5、activity调用详解
private List<TestEntity> testEntities = new ArrayList<>();
private RecyclerView recyclerView;
private NewgroupsAdapter newgroupsAdapter;
private void initRecyclerView() {recyclerView = (RecyclerView)findViewById(R.id.myrecyclerview);newgroupsAdapter = new NewgroupsAdapter(MainActivity.this, testEntities);newgroupsAdapter.setOnItemminiClickListener(new NewgroupsAdapter.OnItemClickListener() {@Overridepublic void onItemClick( View view,int posi) {Toast.makeText(MainActivity.this,posi+"",Toast.LENGTH_LONG).show();}});newgroupsAdapter.setOnItemClickListener(new NewgroupsAdapter.OnItemClickListenerupd() {@Overridepublic void onItemClick(int position) {Toast.makeText(MainActivity.this,position+"max",Toast.LENGTH_LONG).show();}});
// recyclerView.setLayoutManager(new GridLayoutManager(getApplicationContext(), 5));//横向布局,九宫格recyclerView.setLayoutManager(new LinearLayoutManager(this));//竖向布局,默认recyclerView.setAdapter(newgroupsAdapter);}
横向线性布局
LinearLayoutManager linearLayoutManagerHor = new LinearLayoutManager(getContext()); linearLayoutManagerHor.setOrientation(LinearLayoutManager.HORIZONTAL);mRecycleviewHorlayout.setLayoutManager(linearLayoutManagerHor);
给recycleview 赋值
for (int i=0;i<29;i++){TestEntity testEntity = new TestEntity();testEntity.setHttps("baidu.com");testEntity.setTitle("百度。com");testEntities.add(testEntity);if (i==28){newgroupsAdapter.notifyDataSetChanged();}
}
recycleview设置item占位数量
GridLayoutManager layoutManage = new GridLayoutManager(this, 2);
layoutManage.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {@Overridepublic int getSpanSize(int position) {if (tagBeanList.get(position).getTag_name().length()>MAX)//判断条件,如大号item或者小号itemreturn 2;return 1;}
});
tagRecyclerView.setLayoutManager(layoutManage);