标签:dap scaletype 绑定 开发 height save contex context wrap
该控件用于在有限的窗口中展示大量数据集,其实这样功能的控件我们并不陌生,例如:ListView、GridView
通过设置它提供的不同LayoutManager,ItemDecoration , ItemAnimator实现令人瞠目的效果。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <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" > <android.support.v7.widget.RecyclerView android:id="@+id/id_recyclerview_horizontal" android:layout_width="match_parent" android:layout_height="120dp" android:layout_centerVertical="true" android:scrollbars="none" /> </RelativeLayout>
item.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="120dp" android:layout_height="120dp"> <ImageView android:id="@+id/iv" android:layout_width="80dp" android:layout_height="80dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_margin="5dp" android:scaleType="centerCrop"/> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/iv" android:layout_centerHorizontal="true" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" android:text="some info" android:textSize="12dp" /> </RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity { private RecyclerView mRV; private List<Integer> mDatas; private GalleryAdapter madapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initData(); mRV = (RecyclerView) findViewById(R.id.id_recyclerview_horizontal); LinearLayoutManager li = new LinearLayoutManager(this); li.setOrientation(LinearLayoutManager.HORIZONTAL); mRV.setLayoutManager(li);//设置布局管理器 madapter = new GalleryAdapter(this,mDatas); mRV.setAdapter(madapter);//设置adapter } private void initData(){ mDatas = new ArrayList<Integer>(Arrays.asList(R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.e, R.drawable.f, R.drawable.g)); } }
GalleryAdapter.java
public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.ViewHolder> { private LayoutInflater mInflter; private List<Integer> mdatas; public GalleryAdapter(Context context, List<Integer> datas){ mInflter = LayoutInflater.from(context); mdatas = datas; } /** * 我们创建的ViewHolder必须继承RecyclerView.ViewHolder,这个RecyclerView.ViewHolder的 * 构造时必须传入一个View,这个View相当于我们ListView getView中的convertView * (即:我们需要inflate的item布局需要传入)。 */ public class ViewHolder extends RecyclerView.ViewHolder{ public ViewHolder(View v){ super(v); } ImageView miv; TextView mtv; } //创建ViewHoler @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = mInflter.inflate(R.layout.item,parent,false); ViewHolder viewHolder = new ViewHolder(view); viewHolder.miv = (ImageView) view.findViewById(R.id.iv); return viewHolder; } //设置值;将数据绑定至viewholder @Override public void onBindViewHolder(ViewHolder holder, int position) { holder.miv.setImageResource(mdatas.get(position)); } //有多少条数据 @Override public int getItemCount() { return mdatas.size(); } }
标签:dap scaletype 绑定 开发 height save contex context wrap
原文地址:http://www.cnblogs.com/mengxiao/p/7152070.html