标签:list padding ica insert 复制 public bool ams efault
在sdk中找到v7包
\sdk\extras\android\support\v7\recyclerview
导入工程
Import\Android\Existing Android Code Into Workspace
将导入的recycleview作为依赖库Library
工程上右键properties,勾选is Library,Apply,ok
将recycleview\libs\android_support_v7_recycleview.jar包复制粘贴到主工程的libs文件夹下
在layout中使用<android.support.v7.widget.RecyclerView/>标签
MainActivity代码
1 package com.jiatu.recyclerviewtest; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 import android.app.Activity; 6 import android.os.Bundle; 7 import android.support.v7.widget.DefaultItemAnimator; 8 import android.support.v7.widget.GridLayoutManager; 9 import android.support.v7.widget.LinearLayoutManager; 10 import android.support.v7.widget.RecyclerView; 11 import android.support.v7.widget.RecyclerView.ViewHolder; 12 import android.view.LayoutInflater; 13 import android.view.View; 14 import android.view.ViewGroup; 15 import android.widget.LinearLayout; 16 import android.widget.TextView; 17 import android.widget.Toast; 18 19 public class MainActivity extends Activity { 20 private RecyclerView recyclerView; 21 private List<Person> list = new ArrayList<Person>(); 22 private LinearLayoutManager mLayoutManager; 23 // private GridLayoutManager mGridLayoutManager; 24 25 @Override 26 protected void onCreate(Bundle savedInstanceState) { 27 super.onCreate(savedInstanceState); 28 setContentView(R.layout.activity_main); 29 recyclerView = (RecyclerView) findViewById(R.id.recycler_view_test_rv); 30 recyclerView.setHasFixedSize(true); // 固定item的高或者宽,提高性能 31 recyclerView.setItemAnimator(new DefaultItemAnimator()); 32 33 mLayoutManager = new LinearLayoutManager(this); 34 // 设置list的方向,默认是垂直 35 // mLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); 36 37 // 5,spanCount:列数 38 // mGridLayoutManager = new GridLayoutManager(this, 5); 39 40 recyclerView.setLayoutManager(mLayoutManager); 41 42 initData(); 43 setPersonAdapter(); 44 45 } 46 47 /** 48 * 设置适配器 49 */ 50 private void setPersonAdapter() { 51 final PersonAdapter adapter = new PersonAdapter(list); 52 adapter.setOnRecyclerViewListener(new OnRecyclerViewListener() { 53 54 @Override 55 public boolean onItemLongClick(int position) { 56 adapter.addData(position); 57 Toast.makeText(MainActivity.this, "吼吼吼吼吼", Toast.LENGTH_SHORT).show(); 58 return true; 59 } 60 61 @Override 62 public void onItemClick(int position) { 63 adapter.removeData(position); 64 Toast.makeText(MainActivity.this, "哈哈哈哈哈", Toast.LENGTH_SHORT).show(); 65 } 66 }); 67 68 recyclerView.setAdapter(adapter); 69 } 70 71 /** 72 * RecyclerView的单击和长按事件监听 73 * 74 * @author fans created on 2016年11月3日 75 * 76 */ 77 public interface OnRecyclerViewListener { 78 void onItemClick(int position); 79 80 boolean onItemLongClick(int position); 81 } 82 83 /** 84 * 适配器 85 * 86 * @author fans created on 2016年11月3日 87 * 88 */ 89 class PersonAdapter extends RecyclerView.Adapter<ViewHolder> { 90 91 private OnRecyclerViewListener onRecyclerViewListener; 92 93 public void setOnRecyclerViewListener(OnRecyclerViewListener onRecyclerViewListener) { 94 this.onRecyclerViewListener = onRecyclerViewListener; 95 } 96 97 private List<Person> list = new ArrayList<Person>(); 98 99 public PersonAdapter(List<Person> list) { 100 this.list = list; 101 } 102 103 /** 104 * 增加一个item 105 * 106 * @param position 107 */ 108 public void addData(int position) { 109 list.add(position, new Person("fans+", 23)); 110 notifyItemInserted(position); 111 notifyItemRangeChanged(position, list.size()); 112 } 113 114 /** 115 * 删减一个item 116 * 117 * @param position 118 */ 119 public void removeData(int position) { 120 list.remove(position); 121 notifyDataSetChanged(); 122 notifyItemRemoved(position); 123 notifyItemRangeChanged(position, list.size()); 124 } 125 126 @Override 127 public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { 128 129 View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, null); 130 LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 131 ViewGroup.LayoutParams.WRAP_CONTENT); 132 view.setLayoutParams(lp); 133 return new PersonViewHolder(view); 134 } 135 136 @Override 137 public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) { 138 PersonViewHolder holder = (PersonViewHolder) viewHolder; 139 holder.position = i; 140 Person person = list.get(i); 141 holder.nameTv.setText(person.name); 142 holder.ageTv.setText(person.age + "岁"); 143 } 144 145 @Override 146 public int getItemCount() { 147 return list.size(); 148 } 149 150 class PersonViewHolder extends RecyclerView.ViewHolder 151 implements View.OnClickListener, View.OnLongClickListener { 152 public View rootView; 153 public TextView nameTv; 154 public TextView ageTv; 155 public int position; 156 157 public PersonViewHolder(View itemView) { 158 super(itemView); 159 nameTv = (TextView) itemView.findViewById(R.id.recycler_view_test_item_person_name_tv); 160 ageTv = (TextView) itemView.findViewById(R.id.recycler_view_test_item_person_age_tv); 161 rootView = itemView.findViewById(R.id.recycler_view_test_item_person_view); 162 rootView.setOnClickListener(this); 163 rootView.setOnLongClickListener(this); 164 } 165 166 @Override 167 public void onClick(View v) { 168 if (null != onRecyclerViewListener) { 169 onRecyclerViewListener.onItemClick(position); 170 } 171 } 172 173 @Override 174 public boolean onLongClick(View v) { 175 if (null != onRecyclerViewListener) { 176 return onRecyclerViewListener.onItemLongClick(position); 177 } 178 return false; 179 } 180 } 181 182 } 183 184 /** 185 * 填充数据 186 */ 187 private void initData() { 188 for (int i = 0; i < 10; i++) { 189 list.add(new Person("fans" + i, 10 + i)); 190 } 191 } 192 193 class Person { 194 public String name; 195 public int age; 196 197 public Person(String name, int age) { 198 super(); 199 this.name = name; 200 this.age = age; 201 } 202 203 } 204 }
layout代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view_test_rv" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#bbccaa" android:scrollbars="vertical" /> </RelativeLayout>
item代码
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:id="@+id/recycler_view_test_item_person_view" 4 android:layout_width="match_parent" 5 android:layout_height="wrap_content" 6 android:background="#aabbcc" 7 android:orientation="vertical" 8 android:padding="15dp" > 9 10 <TextView 11 android:id="@+id/recycler_view_test_item_person_name_tv" 12 android:layout_width="match_parent" 13 android:layout_height="wrap_content" 14 android:background="#ccbbaa" 15 android:textSize="18sp" /> 16 17 <TextView 18 android:id="@+id/recycler_view_test_item_person_age_tv" 19 android:layout_width="match_parent" 20 android:layout_height="wrap_content" 21 android:background="#aaccbb" 22 android:paddingLeft="5dp" 23 android:textSize="15sp" /> 24 25 </LinearLayout>
标签:list padding ica insert 复制 public bool ams efault
原文地址:http://www.cnblogs.com/fans4-334/p/6027547.html