我创建了只扩展View类的自定义视图。自定义视图完美工作,除非在RecyclerView中使用。这是自定义视图:如何制作与RecyclerView兼容的Android自定义视图
public class KdaBar extends View {
private int mKillCount, mDeathCount, mAssistCount;
private int mKillColor, mDeathColor, mAssistColor;
private int mViewWidth, mViewHeight;
private Paint mKillBarPaint, mDeathBarPaint, mAssistBarPaint, mBgPaint;
private float mKillPart, mDeathPart, mAssistPart;
public KdaBar(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.KdaBar,
0, 0);
try {
mKillCount = a.getInt(R.styleable.KdaBar_killCount, 0);
mDeathCount = a.getInt(R.styleable.KdaBar_deathCount, 0);
mAssistCount = a.getInt(R.styleable.KdaBar_assistCount, 0);
mKillColor = a.getColor(R.styleable.KdaBar_killBarColor, ContextCompat.getColor(getContext(), R.color.kill_score_color));
mDeathColor = a.getColor(R.styleable.KdaBar_deathBarColor, ContextCompat.getColor(getContext(), R.color.death_score_color));
mAssistColor = a.getColor(R.styleable.KdaBar_assistBarColor, ContextCompat.getColor(getContext(), R.color.assist_score_color));
} finally {
a.recycle();
}
init();
}
public void setValues(int killCount, int deathCount, int assistCount) {
mKillCount = killCount;
mDeathCount = deathCount;
mAssistCount = assistCount;
invalidate();
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawRect(0f, 0f, mViewWidth, mViewHeight, mBgPaint);
canvas.drawRect(mKillPart+mDeathPart, 0f, mKillPart+mDeathPart+mAssistPart, mViewHeight, mAssistBarPaint);
canvas.drawRect(mKillPart, 0f, mKillPart+mDeathPart, mViewHeight, mDeathBarPaint);
canvas.drawRect(0f, 0f, mKillPart, mViewHeight, mKillBarPaint);
}
@Override
protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld){
super.onSizeChanged(xNew, yNew, xOld, yOld);
mViewWidth = xNew;
mViewHeight = yNew;
float total = mKillCount + mDeathCount + mAssistCount;
mKillPart = (mKillCount/total) * mViewWidth;
mDeathPart = (mDeathCount/total) * mViewWidth;
mAssistPart = (mAssistCount/total) * mViewWidth;
}
private void init() {
mKillBarPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mKillBarPaint.setColor(mKillColor);
mDeathBarPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mDeathBarPaint.setColor(mDeathColor);
mAssistBarPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mAssistBarPaint.setColor(mAssistColor);
mBgPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mBgPaint.setColor(ContextCompat.getColor(getContext(), R.color.transparent));
}
}
链接的图像就是自定义视图现在看起来像(自定义视图为中心的数字上面的矩形)http://imgur.com/a/Ib5Yl
的数字低于条代表其价值(如果你没有注意到它们是颜色编码的)。很显然,第一个项目上的零值不应在自定义视图上显示蓝条。奇怪,我知道。
下面的方法是其中所述值被设置(它是RecyclerView.Adapter <>内):
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
MatchHistory.Match item = mDataset.get(position);
MatchHistory.MatchPlayer[] players = item.getPlayers();
for(MatchHistory.MatchPlayer player: players) {
int steamId32 = (int) Long.parseLong(mCurrentPlayer.getSteamId());
if (steamId32 == player.getAccountId()) {
mCurrentMatchPlayer = player;
}
}
...
holder.mKdaBar.setValues(mCurrentMatchPlayer.getKills(), mCurrentMatchPlayer.getDeaths(), mCurrentMatchPlayer.getAssists());
...
}
这是onCreateViewHolder:
@Override
public MatchesAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_match_item, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
和ViewHolder类:
public static class ViewHolder extends RecyclerView.ViewHolder {
KdaBar mKdaBar;
public ViewHolder(View v) {
super(v);
...
mKdaBar = (KdaBar) v.findViewById(R.id.kda_bar);
...
}
}
我认为值得注意的是数据集是适配器使用时不时地改变项目的位置(因为它正在同时被全部取出,但被插入以便数据集被排序)。我几乎忘记了我还测试了不改变数据集内项目的位置,但仍然没有任何好的结果。如果您检查了图片,您可以看到项目中还有其他信息,并且我100%确定这些信息都是正确的,但自定义视图中的数据除外。
我在想,我忘记了一些必须重写的方法,但我已经看了很多教程,并没有提到这个问题。期待解决这个问题。 TIA!
你能分享更多的代码吗?自定义视图只是画布绘图吗?你如何设置mCurrentMatchPlayer?什么是onCreateViewHolder工作 – napkinsterror
@napkinsterror是自定义视图只是画布绘图,对于mCurrentMatchPlayer和onCreateViewHolder,请检出编辑过的帖子。 –