今天,我试图创建一个具有4个ImageViews和一个ListView的布局,并根据ImageView的位置更新listview的位置。
一个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;
}
});
如您所见,我向“灰色手柄”添加了一个侦听器,并在移动时更新了“布局”规则并计算了边距。有一个更好的方法吗?
提前致谢,以帮助我。
EDIT: I forgot to add that the bigger the listView the bigger the stutter and I think it's caused by the listView rebuilding all views while changing its position.
EDIT2: I did some testing, added logs and while moving the listview, all views get rebuilt everytime the listview moves. Is there a way to stop this? Maybe make it rebuild once the user stops moving the images?
Solved the problem by optimizing "getView" in my ListView.
For everyone that has problems with listViews:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句