Android进阶之路Android开发经验谈Android开发

RecycleView

2017-12-26  本文已影响20人  喂_balabala
mAdapter = new MoneyRecordFragmentAdapter(mContext, null);
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
    mRecyclerView.setAdapter(mAdapter);





public class MoneyRecordFragmentAdapter extends RecyclerView.Adapter {


private Context mContext;
private List<MoneyRecordBean.ResultBean.ListBean> mResult;

private static final int HEADER_VIEW = 0;
private static final int NORMAL_VIEW = 1;


public MoneyRecordFragmentAdapter(Context context, List<MoneyRecordBean.ResultBean.ListBean> result) {
    mContext = context;
    mResult = result;
}

@Override
public int getItemCount() {

    int count = 0;
    if (mResult != null) {
        for (int i = 0; i < mResult.size(); i++) {
            count++;
            count += mResult.get(i).getData().size();
        }
    }
    return count;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    switch (viewType) {
        case HEADER_VIEW:
            return new HeaderViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_money_record_fragment_header, parent, false));

        case NORMAL_VIEW:
            return new NormalViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_money_record_fragment_normal, parent, false));
    }
    return null;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    switch (getItemViewType(position)) {
        case HEADER_VIEW:
            ((HeaderViewHolder) holder).setData(mResult.get(getHeaderRelPosition(position)), getHeaderRelPosition(position));
            break;
        case NORMAL_VIEW:
            ((NormalViewHolder) holder).setData(mResult.get(getHeaderRelPosition(position)).getData().get(getNormalRelPosition(position)),position);
            break;
    }
}

private int getHeaderRelPosition(int position) {
    int count = 0;
    for (int i = 0; i < mResult.size(); i++) {
        count += mResult.get(i).getData().size();
        count++;
        if (position < count) {
            return i;
        }
    }
    return 0;
}

private int getNormalRelPosition(int position) {
    int headerRelPosition = getHeaderRelPosition(position);

    int count = 0;
    for (int i = 0; i < headerRelPosition; i++) {
        count += mResult.get(i).getData().size();
    }

    return position - count - 1 - headerRelPosition;
}

class HeaderViewHolder extends RecyclerView.ViewHolder {
    @BindView(R.id.tv_time)
    TextView mTvTime;

    public HeaderViewHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
    }

    public void setData(MoneyRecordBean.ResultBean.ListBean bean, int position) {
        if (position != 0) {
            if (bean.getTime().equals(mResult.get(position - 1).getTime())) {
                mTvTime.setVisibility(View.GONE);
            } else {
                mTvTime.setVisibility(View.VISIBLE);
                mTvTime.setText(bean.getTime());
            }
        } else {
            mTvTime.setVisibility(View.VISIBLE);
            mTvTime.setText(bean.getTime());
        }
    }
}

class NormalViewHolder extends RecyclerView.ViewHolder {
    @BindView(R.id.iv)
    ImageView mIv;
    @BindView(R.id.tv_name)
    TextView mTvName;
    @BindView(R.id.tv_use_money)
    TextView mTvUseMoney;
    @BindView(R.id.tv_time)
    TextView mTvTime;
    @BindView(R.id.tv_opt_money)
    TextView mTvOptMoney;
    @BindView(R.id.tv_remark)
    TextView mTvRemark;
    @BindView(R.id.bottom_line_moneyRecord)
    View mBottomLineMoneyRecord;

    public NormalViewHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
    }

    public void setData(MoneyRecordBean.ResultBean.ListBean.DataBean bean,int position) {
//            LogUtil.showLog("eeeee=img",bean.getImage()+"");
        Glide.with(mContext).load("https://www.hzjr.com" + bean.getImage()).apply(RequestOptions.placeholderOf(R.mipmap.hong_bao))
                .apply(RequestOptions.errorOf(R.mipmap.hong_bao))
                .into(mIv);
        mTvName.setText(bean.getName());
        mTvUseMoney.setText("可用余额:" + bean.getUse_money());
//            long l = Long.parseLong();
        mTvTime.setText(TimeUtil.format(bean.getAdd_time()));
        if ("1".equals(bean.getDirect())) {
            mTvOptMoney.setText("+" + bean.getOpt_money());
            mTvOptMoney.setTextColor(Color.parseColor("#f15150"));
        } else if ("0".equals(bean.getDirect()) || "2".equals(bean.getDirect())) {
            mTvOptMoney.setText("-" + bean.getOpt_money());
            mTvOptMoney.setTextColor(Color.parseColor("#2acb47"));
        }
        mTvRemark.setText(Html.fromHtml(bean.getRemark()));
//            mTvRemark.setTextColor(mContext.getResources().getColor(R.color.text_gray));
        /*int count=0;
        if (mResult != null) {
            for (int i = 0; i < mResult.size(); i++) {
                count++;
                count += mResult.get(i).getData().size();

                if (count==position+1){
                    mBottomLineMoneyRecord.setVisibility(View.GONE);
                }else {
                    mBottomLineMoneyRecord.setVisibility(View.VISIBLE);
                }
                if (count>position){
                    return;
                }
            }

        }*/
    }
}

@Override
public int getItemViewType(int position) {

    int count = 0;
    for (int i = 0; i < getHeaderRelPosition(position); i++) {
        count += mResult.get(i).getData().size();
        count++;
    }

    if (position == count) {
        return HEADER_VIEW;
    } else {
        return NORMAL_VIEW;
    }

}

public void setData(List<MoneyRecordBean.ResultBean.ListBean> list) {
    mResult = list;
    notifyDataSetChanged();
}
}

设置间隔

package com.gochequan.hzlc.widget;

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

/**
* Created by hesanwei on 2017/8/17.
*
* 自定义RecyclerView的item之间的间距
*/

public class RecyclerViewSpaceItemDecoration extends RecyclerView.ItemDecoration {
private int space;

public RecyclerViewSpaceItemDecoration(int space) {
    this.space = space;
}

@Override
public void getItemOffsets(Rect outRect, View view,
                           RecyclerView parent, RecyclerView.State state) {
    outRect.right = space;
    outRect.bottom = space;

}
}

  mRecyclerView.addItemDecoration(new RecyclerViewSpaceItemDecoration(0));

设置不要上滑效果

android:overScrollMode="never"
上一篇 下一篇

猜你喜欢

热点阅读