Android-RecycleView

RecycleView使用:

实现横向滚动:

LinearLayoutManager layoutManager = new LinearLayoutManager(this);

//设置横向滚动

layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);

实现瀑布流布局:

设置瀑布流布局

//参数1:布局分为3列

//参数2:布局纵向排列

StaggeredGridLayoutManager layoutManager = new         
            
                    StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);

recyclerView.setLayoutManager(layoutManager);

RecycleView的点击事件:

ViewHolder中添加view变量来保存子项最外层布局的实例,然后在onCreateViewHolder方法中注册点击事件。

利用int position = holder.getAdapterPosition();

获取用户点击的position,然后可以获取单个item实例,然后实现点击逻辑。

final ViewHolder holder = new ViewHolder(view);

holder.appView.setOnClickListener(new View.OnClickListener() {

    @Override

    public void onClick(View view) {

        int position = holder.getAdapterPosition();

        Intent intent = appList.get(position).getLaunch();

        view.getContext().startActivity(intent);

        //Toast.makeText(view.getContext(),""+intent,Toast.LENGTH_SHORT).show();

    }

});

关于LayoutInflater类的inflate方法:

LayoutInflater类的作用是,将xml布局文件实例化为它对应的View对象。

这个类不能直接使用,也就是不能直接调用其中的成员。

扫描二维码关注公众号,回复: 3656435 查看本文章

一般,我们通过getLayoutInflater()方法或者 getSystemService(String)方法来获得该类的实例,通过以上两个方法获得的LayoutInflater类实例,已经和当前的上下文关联起来,并且已经正确配置在当前程序运行的设备上。

public View inflate (int resource, ViewGroup root, boolean attachToRoot)

参数分析

第一个参数,就是我们要填充的xml文件

第二个参数,这个要和第三个参数有关系。若是第三个参数为true,那么第二个参数的意义是,从第一个参数填充成的view对象的父控件;若是第三个参数为false,那么第二个参数的意义是,可以为第一个参数生成的view对象的根布局提供一系LayoutParams参数的控件。

第三个参数,从第一个参数填充成的view对象是否要附着到第二个参数指定的空间上作为子控件。

【问题】

Error::Failed to resolve: com.android.support:recyclerview-v7:26.1.0

解决:

在build.gradle文件末尾加上:

allprojects {

    repositories {

        maven { url "https://maven.google.com" }

        jcenter()

    }

}

猜你喜欢

转载自blog.csdn.net/qq_34149526/article/details/82949354