Android控件开发——RecycleView的使用总结(下)

文/程序员男神

前言

最近很是堕落,先来段鸡汤补补:你的时间有限,所以不要为别人而活。不要被教条所限,不要活在别人的观念里。不要让别人的意见左右自己内心的声音。最重要的是,勇敢的去追随自己的心灵和直觉,只有自己的心灵和直觉才知道你自己的真实想法,其他一切都是次要。


Android控件开发——RecycleView的使用总结(下)_第1张图片
aj

概述

先上需求图:Recycleview的分类显示。


Android控件开发——RecycleView的使用总结(下)_第2张图片
效果图

实现的功能细节:

  • 数据分两层显示
  • 第一层数据显示时间
  • 第二层数据显示这个时间段的项目

实现步骤

项目很简单,下面就是它的目录结构:


Android控件开发——RecycleView的使用总结(下)_第3张图片
目录结构

下面就是主要代码:activity-main.xml




    


MainActivity的代码:

package com.example.djj.retrofitdemos;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends Activity {

    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;

    private CountSectionAdapter mSectionAdapter;
    private RecyclerView.LayoutManager mLayoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        mRecyclerView.addItemDecoration(new DividerItemDecoration(RecyclerviewActivity.this, DividerItemDecoration.VERTICAL));
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mSectionAdapter = new CountSectionAdapter(RecyclerviewActivity.this);
        mRecyclerView.setAdapter(mSectionAdapter);
        mSectionAdapter.setOnItemClickListener(new CountSectionAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(RecyclerviewActivity.this, "你点击了第" + position + "项", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

接下来就是我们的CountSectionAdapter,主要分为三部分:头部,item,尾部。

package com.example.djj.retrofitdemos.adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.djj.retrofitdemos.R;
import com.truizlop.sectionedrecyclerview.SectionedRecyclerViewAdapter;

/**
 * desc: RecyclerView分段(section)显示的Adapter
 * author: dj
 * date: 2017/3/2 14:02
 */

public class CountSectionAdapter extends SectionedRecyclerViewAdapter {

    private Context context;

    public CountSectionAdapter(Context context) {
        this.context = context;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    /**
     * 分几段
     * @return
     */
    @Override
    protected int getSectionCount() {
        return 6;
    }

    /**
     * 段下面有多少item
     * @param section
     * @return
     */
    @Override
    protected int getItemCountForSection(int section) {
        return section + 1;
    }

    @Override
    protected boolean hasFooterInSection(int section) {
        return false;
    }

    protected LayoutInflater getLayoutInflater() {
        return LayoutInflater.from(context);
    }

    @Override
    protected CountHeaderViewHolder onCreateSectionHeaderViewHolder(ViewGroup parent, int viewType) {
        View view = getLayoutInflater().inflate(R.layout.item_title, parent, false);
        return new CountHeaderViewHolder(view);
    }

    @Override
    protected CountFooterViewHolder onCreateSectionFooterViewHolder(ViewGroup parent, int viewType) {
        return null;
    }

    @Override
    protected CountItemViewHolder onCreateItemViewHolder(ViewGroup parent, int viewType) {
        View view = getLayoutInflater().inflate(R.layout.item_list, parent, false);
        return new CountItemViewHolder(view);
    }

    @Override
    protected void onBindSectionHeaderViewHolder(CountHeaderViewHolder holder, int section) {
        holder.render((section + 9) + ":00");
    }

    @Override
    protected void onBindSectionFooterViewHolder(CountFooterViewHolder holder, int section) {

    }

    @Override
    protected void onBindItemViewHolder(CountItemViewHolder holder, int section, final int position) {
        holder.render(R.drawable.jian, "训练", "跑步,有氧训练;拳击,力量训练;起跳训练;拉伸训练");
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClick(v, position);
            }
        });
    }
}

下面就是各个对应的ViewHolder:CountHeaderViewHolder

package com.example.djj.retrofitdemos.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.example.djj.retrofitdemos.R;


/**
 * desc: CountHeaderViewHolder
 * author: dj
 * date: 2017/4/19 9:22
 */

public class CountHeaderViewHolder extends RecyclerView.ViewHolder {

    private TextView textView;

    public CountHeaderViewHolder(View itemView) {
        super(itemView);
        textView = (TextView) itemView.findViewById(R.id.tv_time);
    }

    public void render(String text) {
        textView.setText(text);
    }
}

CountItemViewHolder :

package com.example.djj.retrofitdemos.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.djj.retrofitdemos.R;

/**
 * desc: CountItemViewHolder
 * author: dj
 * date: 2017/4/19 9:22
 */

public class CountItemViewHolder extends RecyclerView.ViewHolder {

    public ImageView icon;
    public TextView title;
    public TextView desc;

    public CountItemViewHolder(View itemView) {
        super(itemView);
        //由于itemView是item的布局文件,我们需要的是里面的textView,因此利用itemView.findViewById获
        //取里面的textView实例,后面通过onBindViewHolder方法能直接填充数据到每一个textView了
        icon = (ImageView) itemView.findViewById(R.id.item_image);
        title = (TextView) itemView.findViewById(R.id.item_title);
        desc = (TextView) itemView.findViewById(R.id.item_desc);
    }

    public void render(int pic, String text, String text1) {
        icon.setImageResource(pic);
        title.setText(text);
        desc.setText(text1);
    }
}

CountFooterViewHolder :

package com.example.djj.retrofitdemos.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * desc: CountFooterViewHolder
 * author: dj
 * date: 2017/4/19 9:21
 */

public class CountFooterViewHolder extends RecyclerView.ViewHolder {
    public CountFooterViewHolder(View itemView) {
        super(itemView);
    }
}

下面就是对应item的布局文件:item_list.xml





    

        

            

                

                

                    

                    
                
            
        
    


item_title.xml:




    

所有代码都在这里,希望可以帮助到各位。
项目传送门:https://github.com/hellodonj/RetrofitDemo

参考文档
https://github.com/truizlop/SectionedRecyclerView

你可能感兴趣的:(Android控件开发——RecycleView的使用总结(下))