我有一个自定义视图消失的问题,我以编程方式创建并添加到项容器(RelativeLayout),当我向下和向上滚动时。它成功构建。
但是在滚动之后,我将此结果放在同一个项目中 - 一个空容器..
自定义视图
public class WaveFormView extends View {
Paint p;
Paint pTop;
Paint pBottom;
Rect rect;
Rect rect1;
int [] amplitudeArray;
int amplitudeArraySize;
int leftPointX;
int rightPointX;
int index;
boolean isPlay;
public static final int MAX_AMPLITUDE = 32;//was 1000
public static final int SIZE_OF_BAR_WAVE_FORM = 98;
public static final int BAR_SIZE_AND_INTERVAL = 5;
private static final float screenDensity = Utils.getDisplayDensity();
public WaveFormView(Context context, int [] amplitudeArray, boolean isPlay, int index) {
super(context);
p = new Paint();
p.setColor(getResources().getColor(R.color.color_wave_from_grey));
pTop = new Paint();
pTop.setColor(getResources().getColor(R.color.color_wave_form_play_top));
pBottom = new Paint();
pBottom.setColor(getResources().getColor(R.color.color_wave_form_play_bottom));
this.amplitudeArray = amplitudeArray;
amplitudeArraySize = amplitudeArray.length;
this.isPlay = isPlay;
this.index = index;
}
@Override
protected void onDraw(Canvas canvas) {
if(!isPlay){
buildWaveForm(canvas);
}
else{
buildWaveFormWhenPlay(canvas);
}
}
private void buildWaveForm(Canvas canvas){
int oneDp = (int) (1*screenDensity);
int fourDp = (int) (4*screenDensity);
int amplitudeDp = 0;
int startBottomY = (int) (MAX_AMPLITUDE*screenDensity) + oneDp;
p.setColor(getResources().getColor(R.color.color_wave_from_grey));
for (int i = 0; i < amplitudeArraySize; i++){
p.setStyle(Paint.Style.FILL_AND_STROKE);
leftPointX += oneDp;
rightPointX = leftPointX+fourDp;
amplitudeDp = (int) (amplitudeArray[i]*screenDensity);
rect = new Rect(leftPointX, startBottomY, rightPointX, startBottomY + amplitudeDp);
rect1 = new Rect(leftPointX, startBottomY-oneDp-amplitudeDp, rightPointX, startBottomY-oneDp);
canvas.drawRect(rect, p);
canvas.drawRect(rect1, p);
leftPointX+=fourDp;
}
}
private void buildWaveFormWhenPlay(Canvas canvas){
...}
适配器
public class PostTimeLineAdapter extends
RecyclerView.Adapter<PostTimeLineHolder> ...
@Override
public void onBindViewHolder(final PostTimeLineHolder holder, final int position) { ...
holder.homeItemContainerWaveForm.addView(new WaveFormView(context,bufferArray, false, -1));...}
持有人
public class PostTimeLineHolder extends RecyclerView.ViewHolder {
...
public RelativeLayout homeItemContainerWaveForm;
public PostTimeLineHolder(View itemView) {
super(itemView);
...
this.homeItemContainerWaveForm = (RelativeLayout) itemView.findViewById(R.id.home_item_sound_container_wave_forms);}}
感谢您的所有答案!
答案 0 :(得分:0)
这种奇怪行为的原因是在运算符之后没有将这些参数归零:leftPointX和rightPointX,在方法buildWaveForm中。如果在operator for之后为它们设置了零,则方法buildWaveForm会精确地工作。