今天我正在尝试使用4个ImageViews和ListView创建一个布局,并根据ImageView位置更新列表视图位置。
SS向您展示我正在尝试做的事情:
正如您所看到的,当我们拉出“灰色手柄”时,ListView位置会更新。 一切都运作良好,但移动手柄时图形不平滑但口吃。
我使用的代码就像这样;
btn.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent me) {
if (me.getAction() == MotionEvent.ACTION_DOWN) {
status = START_DRAGGING;
}
if (me.getAction() == MotionEvent.ACTION_UP) {
status = STOP_DRAGGING;
Log.i("Drag", "Stopped Dragging");
} else if (me.getAction() == MotionEvent.ACTION_MOVE) {
if (status == START_DRAGGING) {
System.out.println("Dragging");
GestureImageView image1 = (GestureImageView) findViewById(R.id.image1);
GestureImageView image2 = (GestureImageView) findViewById(R.id.image2);
ImageView header = (ImageView) findViewById(R.id.imageHeader);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
margin1 = (int) me.getRawY() - 100;
if (margin1 < header.getHeight() - btn.getHeight() / 3)
margin1 = header.getHeight() - btn.getHeight() / 3;
else if (margin1 > layout.getHeight() - 1.5
* btn.getHeight() - btn3.getHeight())
margin1 = (int) (layout.getHeight() - 1.5
* btn.getHeight() - btn3.getHeight());
lp.topMargin = margin1;
btn.setLayoutParams(lp);
if (margin1 >= margin2 - 35) {
RelativeLayout.LayoutParams newlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
newlp.addRule(RelativeLayout.CENTER_HORIZONTAL);
margin2 = margin1 + 35;
newlp.topMargin = margin2;
btn2.setLayoutParams(newlp);
RelativeLayout.LayoutParams newlp2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.FILL_PARENT);
newlp2.addRule(RelativeLayout.ABOVE, btn2.getId());
newlp2.addRule(RelativeLayout.BELOW, header.getId());
newlp2.bottomMargin = -(15 + margin2);
image2.setLayoutParams(newlp2);
image2.setAdditionalY(image2.getHeight(),
margin2 - 85, image2.getScaledHeight()
- margin2 - 85);
}
RelativeLayout.LayoutParams lp3 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.FILL_PARENT);
lp3.addRule(RelativeLayout.ABOVE, btn.getId());
lp3.addRule(RelativeLayout.BELOW, header.getId());
lp3.bottomMargin = -(15 + margin1);
image1.setLayoutParams(lp3);
image1.setAdditionalY(image1.getHeight(), margin1 - 50,
image1.getScaledHeight() - margin1 - 50);
}
}
return false;
}
});
正如您所看到的,我向“灰色句柄”添加了一个监听器,并在移动时更新了布局规则并计算了边距。有一个更好的方法吗?
提前帮助我。
修改 我忘了添加listView越大,口吃越大,我认为这是由listView在改变位置时重建所有视图引起的。
EDIT2: 我做了一些测试,添加了日志,在移动列表视图时,每次listview移动时都会重建所有视图。有办法阻止这个吗?也许在用户停止移动图像后重建它?
答案 0 :(得分:0)
通过优化&#34; getView&#34;解决了这个问题。在我的ListView。
对于每个遇到listViews问题的人: