我創建了只擴展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,請檢出編輯過的帖子。 –