Reputation: 6912
Below is my layout.xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="30dp" >
<LinearLayout
android:id="@+id/ll1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/tv1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Medium Text"
android:background="#000000"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</LinearLayout>
I want let the TeextView tv1 can drag in LinearLayout ll1 only horizontal.
And I implement as below:
ll1 = (LinearLayout)findViewById(R.id.ll1);
tv1 = (TextView)findViewById(R.id.tv1);
Rect rectf = new Rect();
ll1.getGlobalVisibleRect(rectf);
LayoutLeft = rectf.left;
tv1.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
float TouchX = event.getX();
System.out.println("TouchX " + TouchX);
switch(event.getAction()) {
case MotionEvent.ACTION_MOVE:
if(TouchX < LayoutLeft) {
ll1.setPadding(0, 0, 0, 0);
}
else if(TouchX > (LayoutLeft + 100)) {
ll1.setPadding(100, 0, 0, 0);
}
else {
ll1.setPadding((int)(TouchX - LayoutLeft), 0, 0, 0);
}
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_DOWN:
break;
}
return true;
}
});
But I test drag from left to right, I get the TouchX value as below:
04-03 14:24:18.061: I/System.out(18468): TouchX 43.554024
04-03 14:24:18.631: I/System.out(18468): TouchX 92.101845
04-03 14:24:19.731: I/System.out(18468): TouchX 49.150528
04-03 14:24:20.371: I/System.out(18468): TouchX 100.82299
04-03 14:24:22.311: I/System.out(18468): TouchX 54.747032
04-03 14:24:22.781: I/System.out(18468): TouchX 74.458405
04-03 14:24:22.801: I/System.out(18468): TouchX 6.1168213
04-03 14:24:22.821: I/System.out(18468): TouchX 80.44317
04-03 14:24:22.851: I/System.out(18468): TouchX 21.571915
04-03 14:24:22.881: I/System.out(18468): TouchX 89.24452
Why the TouchX Value not in order?
How can I modify it?
Upvotes: 2
Views: 639
Reputation: 12239
ll1 is a linearlayout and Im guessing you have your textview inside your layout. If you set the padding to your linearlayout then the children in the view will also be added padding. You have to either have the touch event for your linearlayout or for your textview. you can also change margin instead of the textview. Find my solution for your problem
ll1 = (LinearLayout)findViewById(R.id.ll1);
tv1 = (TextView)findViewById(R.id.tv1);
LinearLayout.LayoutParams layout_param= new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.fill_parent,
height * 2);
/or whatever you want to be margins for
layout_param.setMargins(30, 20, 30, 0);
Rect rectf = new Rect();
ll1.getGlobalVisibleRect(rectf);
LayoutLeft = rectf.left;
ll1.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
float TouchX = event.getX();
System.out.println("TouchX " + TouchX);
switch(event.getAction()) {
case MotionEvent.ACTION_MOVE:
//ALTER your layoutparams as per your need.
//do calculations on your layout param
if(TouchX < LayoutLeft) {
ll1.setLayoutParams(layout_param);
}
else if(TouchX > (LayoutLeft + 100)) {
ll1.setLayoutParams(layout_param);
}
else {
ll1.setLayoutParams(layout_param);
}
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_DOWN:
break;
}
return true;
}
});
Hope this will solve your problem.
Upvotes: 1