RecyclerView的簡單使用方法(一)
阿新 • • 發佈:2017-06-18
設置 lns hello ins sta size fin ack list
1、在build中引入compile ‘com.android.support:recyclerview-v7:25.3.1‘
2、在activity中布局如下:
<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="com.example.myapplication.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycylview"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
item.xml布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"
android:background="@color/colorPrimary"
android:gravity="center"
android:padding="20dp"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text"
android:layout_gravity="center"
android:gravity="center"
android:text="Hello"
android:textSize="20dp"
android:padding="20dp"
android:textColor="#fff"/>
</LinearLayout>
3.在MainActivity中代碼如下:
private RecyclerView mRecyclerView;4.適配器代碼:
private List<String> datas = new ArrayList();
private MyAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = (RecyclerView) findViewById(R.id.recycylview);
mAdapter = new MyAdapter(datas);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(4,StaggeredGridLayoutManager.HORIZONTAL));//可以設置列表模式
mRecyclerView.addItemDecoration(new MyItemDacoration(this));//加載列表樣式
initDatas();
}
private void initDatas(){
datas.add("New York");
datas.add("Boston");
datas.add("New York");
datas.add("New York");
datas.add("New York");
datas.add("New York");
datas.add("New York");
datas.add("New York");
datas.add("New York");
datas.add("New York");
datas.add("New York");
}
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {5、新建dimen.xml文件
private List<String> mDatas ;
private MyAdapter mAdapter;
private LayoutInflater inflater;
public MyAdapter(List<String> datas){
mDatas =datas;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.item,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {//綁定數據
holder.textView.setText(mDatas.get(position));
}
@Override
public int getItemCount() {
return mDatas.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
private TextView textView;
public ViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.text);
}
}
}<dimen name="divider">10dp</dimen> />
6.新建Java文件設置列表樣式:
public class MyItemDacoration extends RecyclerView.ItemDecoration {
private int divider;
public MyItemDacoration(Context context) {
//即你要設置的分割線的寬度 --這裏設為10dp
divider = context.getResources().getDimensionPixelSize(R.dimen.divider);
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
// outRect.left = divider; //相當於 設置 left padding
// outRect.top = divider; //相當於 設置 top padding
// outRect.right = divider; //相當於 設置 right padding
outRect.bottom = divider; //相當於 設置 bottom padding
}
RecyclerView的簡單使用方法(一)