01 效果图
RecyclerView+CardView.png
02 Add library dependency
compile 'com.android.support:recyclerview-v7:25.0.1'
compile 'com.android.support:cardview-v7:25.0.1'
03 layout
rv_item.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@android:color/transparent">
<!--此处使用了CardView-->
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="#669900"
app:cardCornerRadius="20dp">
<TextView
android:id="@+id/tv_item"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="Text"
android:textColor="#ffffff"/>
</android.support.v7.widget.CardView>
</FrameLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
04 MainActivity
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private Context mContext;
private ArrayList<String> mDatas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
intiData();
intiUI();
}
private void intiData() {
mDatas = new ArrayList<String>();
for(int i = 'A'; i <= 'Z'; i++){
mDatas.add("" + (char) i);
}
}
private void intiUI() {
RecyclerView rv_recycler_view = (RecyclerView) findViewById(R.id.rv_recycler_view);
rv_recycler_view.setLayoutManager(new LinearLayoutManager(mContext));
rv_recycler_view.setAdapter(new HomeAdapter());
rv_recycler_view.addItemDecoration(new DividerItemDecoration(mContext, LinearLayout.VERTICAL));
}
class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder>{
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//MyViewHolder holder = new MyViewHolder(View.inflate(mContext, R.layout.rv_item, parent));
MyViewHolder holder = new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.rv_item, parent, false));
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.tv_item.setText(mDatas.get(position));
}
@Override
public int getItemCount() {
return mDatas.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView tv_item;
public MyViewHolder(View view){
super(view);
tv_item = (TextView) view.findViewById(R.id.tv_item);
}
}
}
}
网友评论