RecyclerView的基本用法

2017-11-28  本文已影响0人  啧啧啧ZzzZ

打开app/build.gradle文件,在dependencies闭包中添加相应依赖库

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support:recyclerview-v7:26.1.0' 
    testImplementation 'junit:junit:4.12'
}

点击Sync Now进行同步,在xml布局文件中加入RecyclerView控件

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

为RecyclerView准备一个适配器,新建一个NewsAdapter类,并让它继承RecyclerView.Adapter,并将泛型指定为NewsAdapter.Viewholder;
其中Viewholder是自己在NewsAdapter中定义的一个内部类,继承自RecyclerView.Viewholder,然后给Viewholder的构造函数中传入一个View参数(通常是RecycleView子项的最外层布局),那么就可以通过findViewById()的方法来获取布局中的实例了

public class NewsAdapter extends RecyclerView.Adapter {

    private List<News> newsList;

    public NewsAdapter(List<News> newsList) {
        this.newsList = newsList;
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        TextView newTitleText;
        public ViewHolder(View view){
            super(view);
            newTitleText = (TextView) view.findViewById(R.id.news_title);
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.new_item, parent, false);
        final ViewHolder holder = new ViewHolder(view);
        view.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        News news= newsList.get(position);
        holder.newsTitleText,setText(news.getTitle());
    }

    @Override
    public int getItemCount() {
        return newsList.size();
    }

}

onCreateViewHolder负责为Item创建视图
onBindViewHolder负责将数据绑定到Item视图

适配器准备好后,就可以使用RecyclerView了

public class NewsTitleFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.news_title_frag,container,false);
        RecyclerView newsTitleRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        newsTitleRecyclerView.setLayoutManager(linearLayoutManager);
        NewsAdapter newsAdapter = new NewsAdapter(getNews());
        newsTitleRecyclerView.setAdapter(newsAdapter);
        return view;
    }

    private List<News> getNews(){
        List<News> newsList = new ArrayList<News>();
        for (int i=1; i<20; i++){
            News news = new News();
            news.setTitle("This is title" + i);
            news.setContent("Hi,here is content" + i);
            newsList.add(news);
        }
        return newsList;
    }

}
上一篇下一篇

猜你喜欢

热点阅读