标签:
RecyclerView 出来已经挺久了,今天下午刚接触一下。
RecyclerView已经逐步取代lsitView,别的不多说,上代码:
我们使用的开发工具是Android Studio。
想要使用RecyclerView,首先我们要在build.gradle中添加:
dependencies {
compile fileTree(dir: ‘libs‘, include: [‘*.jar‘])
testCompile ‘junit:junit:4.12‘
compile ‘com.android.support:appcompat-v7:23.2.1‘
compile ‘com.android.support:design:23.2.1‘
/*使用RecyclerView*/
compile ‘com.android.support:recyclerview-v7:23.0.1‘
compile ‘com.google.android.gms:play-services-appindexing:8.1.0‘
}
XML中的代码:
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
JAVA代码:
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private List<String> datas = new ArrayList<>();
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找到控件
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
adapter = new MyAdapter(datas);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//设置分割线
recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
//GridVIew与listVIew自由切换
// recyclerView.setLayoutManager( new GridLayoutManager(this,4));
//瀑布流
// recyclerView.setLayoutManager( new StaggeredGridLayoutManager(4,StaggeredGridLayoutManager.HORIZONTAL));
initDatas();
adapter.setOnClickListener(new MyAdapter.OnTestClickListener() {
@Override
public void OnCcc(View v, int position, String city) {
Toast.makeText(MainActivity.this, city, Toast.LENGTH_SHORT).show();
}
});
}
private void initDatas() {
datas.add("山东");
datas.add("上海");
datas.add("北京");
datas.add("河南");
datas.add("古巴");
datas.add("莱阳");
datas.add("烟台");
datas.add("青岛");
datas.add("济南");
datas.add("菏泽");
datas.add("淄博");
datas.add("莱西");
datas.add("栖霞");
datas.add("桃村");
datas.add("鱼台");
datas.add("青州");
datas.add("台东");
datas.add("高新");
datas.add("长清");
datas.add("即墨"); datas.add("姜疃"); datas.add("穴坊"); datas.add("乔家泊"); datas.add("聊城"); datas.add("菏泽"); datas.add("哈尔滨"); datas.add("齐齐哈尔"); datas.add("美国"); datas.add("中国"); datas.add("伊拉克"); }}
Adapter:
//继承RecyclerView.Adapter<>,泛型必须是ViewHolder
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<String> mDatas;
private LayoutInflater inflater;
private OnTestClickListener listener;
public void setOnClickListener(OnTestClickListener listener){
this.listener = listener;
}
//创建一个构造方法,把数据源传递进来
public MyAdapter(List<String> datas) {
mDatas = datas;
}
//添加数据
public void addData(int position,String city){
mDatas.add(position,city);
notifyItemInserted(position);
}
//删除数据
public void deleteData(int position){
mDatas.remove(position);
notifyItemRemoved(position);
}
//创建一个ViewHolder,相当于以前个getView方法;
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.the_item, parent, false);
return new ViewHolder(view);
}
//相当于以前在getView方法中绑定数据
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
//把数据和View进行绑定
holder.textView.setText(mDatas.get(position));
}
@Override
public int getItemCount() {
return mDatas.size();
}
// ViewHolder必须继承RecyclerView.ViewHolder
class ViewHolder extends RecyclerView.ViewHolder {
private TextView textView;
public ViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.textView);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(listener != null){
listener.OnCcc(view,getLayoutPosition(),mDatas.get(getLayoutPosition()));
}
}
});
}
}
//RecyclerView 的监听事件完全交给开发者;
interface OnTestClickListener{
//自定义我们需要的参数;
void OnCcc(View v, int position ,String city);
}
}
标签:
原文地址:http://www.cnblogs.com/mrlongwt/p/5324697.html