美文网首页Android开发知识集Android知识Android开发
Android控件开发——RecycleView的使用总结(下)

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

作者: 程序猿男神 | 来源:发表于2017-04-20 17:42 被阅读133次

文/程序员男神

前言

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


aj

概述

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


效果图

实现的功能细节:

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

实现步骤

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


目录结构

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:orientation="vertical"
    tools:context="com.example.djj.retrofitdemos.activity.MainActivity">

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

</LinearLayout>

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<CountHeaderViewHolder,
        CountItemViewHolder,
        CountFooterViewHolder> {

    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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:orientation="horizontal">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:id="@+id/layout_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <ImageView
                    android:id="@+id/item_image"
                    android:layout_width="100dp"
                    android:layout_height="70dp"
                    android:background="@drawable/jian"/>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:orientation="vertical"
                    android:padding="6dp">

                    <TextView
                        android:id="@+id/item_title"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="我是标题"
                        android:textAppearance="@android:style/TextAppearance.Large"/>

                    <TextView
                        android:id="@+id/item_desc"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:ellipsize="end"
                        android:maxLines="2"
                        android:text="我是大体内容"/>
                </LinearLayout>
            </LinearLayout>
        </RelativeLayout>
    </RelativeLayout>

</LinearLayout>

item_title.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_time"
        android:padding="6dp"
        android:text="3:00"
        android:layout_marginLeft="8dp"
        android:textSize="24sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

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

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

相关文章

网友评论

    本文标题:Android控件开发——RecycleView的使用总结(下)

    本文链接:https://www.haomeiwen.com/subject/jdvlzttx.html