android复习路之RecyclerView简单的应用

之前接触了RecyclerView学写了一个简单的demo效果就是实现美团顶部的侧滑效果,今天就是只是简单的实现效果,不深入理解,明天深入理解RecyclerView,各种变换和操作。

MainActivity代码:

public class MainActivity extends AppCompatActivity {
    private RecyclerView rv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rv = (RecyclerView) findViewById(R.id.rv);
        initdata();
    }

    private void initdata() {


        /*
          初始化数据
         */
        List<Integer> datas = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            Resources rs = getResources();
            datas.add(rs.getIdentifier("ic_launcher", "mipmap", getPackageName()));
        }

        /*
         用来确定每一个item如何进行排列摆放
         LinearLayoutManager 相当于ListView的效果
         GridLayoutManager相当于GridView的效果
         StaggeredGridLayoutManager 瀑布流
         */
        rv.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.HORIZONTAL));
        rv.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                //outRect.left = 10;
                //outRect.top = 10;
                //outRect.top = 10;
            }
        });
        rv.setAdapter(new LinearRecyclerView(this,datas));
    }
}
 
LinearRecyclerView类代码:
 
public  class LinearRecyclerView extends RecyclerView.Adapter<LinearRecyclerView.MyViewHolder>{
    private Context context;
    private List<Integer> datas;
    public  LinearRecyclerView(Context context,List<Integer>datas){
        this.context=context;
        this.datas=datas;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View contentView =LayoutInflater.from(context).inflate(R.layout.item_layout,parent,false);
        MyViewHolder viewHolder=new MyViewHolder(contentView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.imageView.setImageResource(datas.get(position));
        holder.textView.setText("分类"+position);
    }

    @Override
    public int getItemCount() {

        return datas==null?0:datas.size();
    }


    public class MyViewHolder extends RecyclerView.ViewHolder{
        private ImageView imageView;
        private TextView textView;
        public MyViewHolder(View itemView) {
            super(itemView);
            imageView= (ImageView) itemView.findViewById(R.id.img);
            textView= (TextView) itemView.findViewById(R.id.tv);
        }
    }
}
主布局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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.zobject.linearrecyclerview.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:layout_height="200dp"
        android:layout_width="match_parent"
        android:id="@+id/rv"
        >

    </android.support.v7.widget.RecyclerView>

</RelativeLayout>
item布局xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="70dp"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <ImageView
        android:id="@+id/img"
        android:layout_gravity="center"
        android:layout_width="60dp"
        android:layout_height="75dp"

        >
    </ImageView>>
    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:textColor="#000000"
        android:text="nicaicai"
        >

    </TextView>
</LinearLayout>
  代码没什么说的都是基本的模版代码,方便以后查看,下面是效果图:


 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值