recyleView使用笔记

直接上代码:

package com.test.recycleview;

import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast; import java.util.ArrayList; /**
* test recyleView
*/
public class MainActivity extends Activity{
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
recyclerView=new RecyclerView(this);
ViewGroup.LayoutParams params=new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
setContentView(recyclerView,params);
initRecycleView();
initData();
}
private void initRecycleView(){
LinearLayoutManager manager=new LinearLayoutManager(this);
recyclerView.setLayoutManager(manager);
recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
@Override
public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent event) {
return false;
} @Override
public void onTouchEvent(RecyclerView recyclerView, MotionEvent event) { }
}); recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDraw(c, parent, state);
Paint paint=new Paint();
paint.setColor(Color.GRAY);
paint.setStrokeWidth(3);
for (int i = 0, size = parent.getChildCount(); i < size; i++) {
View child = parent.getChildAt(i);
c.drawLine(0, child.getBottom(), parent.getRight(), child.getBottom(), paint);
}
}
}); }
public void initData(){
ArrayList<String> data=new ArrayList<String>(50);
for (int i=0;i<100;i++){
data.add(String.valueOf(i));
}
recyclerView.setAdapter(new MyAdapter(data));
}
public class MyAdapter extends RecyclerView.Adapter{
ArrayList<String> data; public MyAdapter(ArrayList<String> data) {
this.data = data;
} @Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view=View.inflate(MainActivity.this, android.R.layout.simple_list_item_1,null);
ViewGroup.LayoutParams params=new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
view.setLayoutParams(params);
return new ViewHolder(view);
} @Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
((ViewHolder)viewHolder).textView.setText(String.valueOf(i));
} @Override
public int getItemCount() {
return data.size();
} private class ViewHolder extends RecyclerView.ViewHolder{
public TextView textView;
public ViewHolder(View itemView) {
super(itemView);
textView= (TextView) itemView;
textView.setGravity(Gravity.CENTER);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"click postion:"+getPosition()+" content: "+textView.getText(),Toast.LENGTH_LONG).show();
}
});
}
}
} }

  

上一篇:Centos6 源代码部署MySQL5.6


下一篇:【Kafka源码】ReplicaManager启动过程