Android RecyclerView加载不同布局简单实现

来源:爱站网时间:2020-03-16编辑:网友分享
我们在工作中都会遇到各种各样的列表,然而随着项目的需求,我们会发现当初开发布局的列表也会有所变动,下面就由爱站技术频道小编给大家介绍Android RecyclerView加载不同布局简单实现。

我们在工作中都会遇到各种各样的列表,然而随着项目的需求,我们会发现当初开发布局的列表也会有所变动,下面就由爱站技术频道小编给大家介绍Android RecyclerView加载不同布局简单实现。

前言

关于RecyclerView的使用这里就不在赘述了,相信网上一搜一大把(本人之前的文章也有简单的使用介绍),这次我们讲的是RecyclerView在使用的过程中,有时候会根据不同的位置加载不同的布局的简单实现,这里只是起到抛砖引玉的作用

效果图

设计思想
 •重写RecyclerView.Adapter的getItemViewType(int position),
在此方法中根据不同的position,设置不同的ViewType
 •编写具体的RecyclerView.ViewHolder子类(不同子类对应不同View或Layout)
 •重写RecyclerView.Adapter的onCreateViewHolder(ViewGroup parent,int viewType)
在此方法中根据我们之前设置的ViewType来返回不同的RecyclerView.ViewHolder的子类 

代码

使用RecyclerView需要添加依赖

compile 'com.android.support:recyclerview-v7:24.1.1'

首先是MainActivity布局,很简单的布局




  


然后是MainActivity代码

 

package com.example.hfs.recyclerviewloadlayout;

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

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

  private RecyclerView mRecyclerView;

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

    initView();

    initData();
  }

  private void initData() {
    List descs = new ArrayList();
    descs.clear();

    List images = new ArrayList();
    images.clear();

    for (int i = 1; i 

然后是我们的适配器

package com.example.hfs.recyclerviewloadlayout;

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

import java.util.ArrayList;
import java.util.List;

/**
 * Created by HFS on 2016/8/24.
 */
public class MyAdapter extends RecyclerView.Adapter {

  private Context mContext;
  private List descs;
  private List images;
  private List allList;

  private static final int TEXTVIEW_VIEWTYPE = 1;
  private static final int IMAGEVIEW_VIEWTYPE = 2;

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

  private OnItemClickListener mOnItemClickListener;

  public OnItemClickListener getmOnItemClickListener() {
    return mOnItemClickListener;
  }

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

  public MyAdapter(Context mContext, List descs, List images) {
    this.mContext = mContext;
    this.descs = descs;
    this.images = images;

    allList = new ArrayList();
    allList.clear();
    for (int i = 0; i 

接着就是我们自定义的DividerItemDecoration

package com.example.hfs.recyclerviewloadlayout;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * Created by HFS on 2016/8/24.
 */
public class DividerItemDecoration extends RecyclerView.ItemDecoration {
  private static final int[] ATTRS = new int[]{
      android.R.attr.listDivider
  };

  public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;

  public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;

  private Drawable mDivider;

  private int mOrientation;

  public DividerItemDecoration(Context context, int orientation) {
    final TypedArray a = context.obtainStyledAttributes(ATTRS);
    mDivider = a.getDrawable(0);
    a.recycle();
    setOrientation(orientation);
  }

  public void setOrientation(int orientation) {
    if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
      throw new IllegalArgumentException("invalid orientation");
    }
    mOrientation = orientation;
  }

  @Override
  public void onDraw(Canvas c, RecyclerView parent) {


    if (mOrientation == VERTICAL_LIST) {
      drawVertical(c, parent);
    } else {
      drawHorizontal(c, parent);
    }

  }


  public void drawVertical(Canvas c, RecyclerView parent) {
    final int left = parent.getPaddingLeft();
    final int right = parent.getWidth() - parent.getPaddingRight();

    final int childCount = parent.getChildCount();
    for (int i = 0; i 

 

上一篇:android接收到蓝牙配对请求时如何点亮屏幕具体实现

下一篇:PHP autoload 机制详解

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载