到目前为止,我已经创建了一个RelativeLayout,如下所示,它包含6个文本视图(但在任何时候只能看到3个)。按下两个圆形图像视图中的任何一个时,这些文本视图将被适当地显示或不可见。
我还想发生什么,但是我还不知道是怎么回事:因为当使用滑动时,在拿着文本视图的RelativeLayout上的任何地方,就好像第一个圆形的图像视图被按下了一样用户在图像视图的任何位置滑动,就像用户选择了第二个圆形图像视图一样。
之前我没有在RelativeLayouts上以这种方式使用手势,所以非常感谢帮助。感谢
到目前为止我的代码:
MainActivity.Java
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView firstCircle = (ImageView) findViewById(R.id.firstCircle);
final ImageView secondCircle = (ImageView) findViewById(R.id.secondCircle);
final TextView textView1 = (TextView) findViewById(R.id.textView1);
final TextView textView2 = (TextView) findViewById(R.id.textView2);
final TextView textView3 = (TextView) findViewById(R.id.textView3);
final TextView textView4 = (TextView) findViewById(R.id.textView4);
final TextView textView5 = (TextView) findViewById(R.id.textView5);
final TextView textView6 = (TextView) findViewById(R.id.textView6);
firstCircle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
textView1.setVisibility(View.VISIBLE);
textView2.setVisibility(View.VISIBLE);
textView3.setVisibility(View.VISIBLE);
textView4.setVisibility(View.INVISIBLE);
textView5.setVisibility(View.INVISIBLE);
textView6.setVisibility(View.INVISIBLE);
}
});
secondCircle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
textView1.setVisibility(View.INVISIBLE);
textView2.setVisibility(View.INVISIBLE);
textView3.setVisibility(View.INVISIBLE);
textView4.setVisibility(View.VISIBLE);
textView5.setVisibility(View.VISIBLE);
textView6.setVisibility(View.VISIBLE);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<RelativeLayout
android:id="@+id/resultsBox"
android:layout_width="fill_parent"
android:layout_height="500dp"
android:layout_alignParentLeft="false"
android:layout_alignParentStart="false"
android:layout_alignParentTop="true"
android:background="#2afd24">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tables"
android:visibility="visible"
android:textSize="40dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tables"
android:visibility="visible"
android:layout_marginTop="40dp"
android:textSize="40dp" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tables"
android:visibility="visible"
android:layout_marginTop="80dp"
android:textSize="40dp" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="chairs"
android:visibility="invisible"
android:layout_marginTop="120dp"
android:textSize="40dp" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="chairs"
android:visibility="invisible"
android:layout_marginTop="160dp"
android:textSize="40dp" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="chairs"
android:visibility="invisible"
android:layout_marginTop="200dp"
android:textSize="40dp" />
<ImageView
android:id="@+id/firstCircle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/circle"
android:layout_marginTop="310dp"
android:layout_marginLeft="170dp"/>
<ImageView
android:id="@+id/secondCircle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/circle"
android:layout_marginTop="310dp"
android:layout_marginLeft="200dp"/>
</RelativeLayout>
</RelativeLayout>
circle.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#fff"/>
<size
android:width="18dp"
android:height="18dp"/>
</shape>
到目前为止应用程序的内容:
答案 0 :(得分:0)
试试这段代码: 声明
private float lastX;
作为成员变量。
添加此方法以处理onTouch
/***********************************************
* TouchEventListener
***********************************************/
@Override
public boolean onTouchEvent(MotionEvent touchevent) {
if (!isForcedSleep) {
switch (touchevent.getAction()) {
case MotionEvent.ACTION_LEFT: {
lastY = touchevent.getX();
break;
}
case MotionEvent.ACTION_RIGHT: {
float currentY = touchevent.getX());
if (lastX < currentX - 15) {
//Do your stuff
} else if (lastX > currentX + 15) {
//Do your stuff
}
break;
}
}
return false;
} else {
return false;
}
}
答案 1 :(得分:0)
这应该可以胜任: - )
public class MainActivity extends ActionBarActivity {
private int viewLeftMargin;
private int viewTopMargin;
private int viewBottomMargin;
private int viewRightMargin;
private int lastMarginLeft;
private int lastMarginRight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView firstCircle = (ImageView) findViewById(R.id.firstCircle);
final ImageView secondCircle = (ImageView) findViewById(R.id.secondCircle);
final TextView textView1 = (TextView) findViewById(R.id.textView1);
final TextView textView2 = (TextView) findViewById(R.id.textView2);
final TextView textView3 = (TextView) findViewById(R.id.textView3);
final TextView textView4 = (TextView) findViewById(R.id.textView4);
final TextView textView5 = (TextView) findViewById(R.id.textView5);
final TextView textView6 = (TextView) findViewById(R.id.textView6);
firstCircle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
firstCircleClick();
}
});
secondCircle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
secondCircleClick();
}
});
RelativeLayout resultsBox = (RelativeLayout) findViewById(R.id.resultsBox);
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) resultsBox
.getLayoutParams();
lastMarginLeft = lParams.leftMargin;
lastMarginRight = lParams.rightMargin;
resultsBox.setOnTouchListener(
new RelativeLayout.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) v
.getLayoutParams();
viewLeftMargin = X - lParams.leftMargin;
viewTopMargin = lParams.topMargin;
viewBottomMargin = lParams.bottomMargin;
viewRightMargin = X - lParams.rightMargin;
break;
case MotionEvent.ACTION_MOVE:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) v
.getLayoutParams();
layoutParams.rightMargin = X - viewRightMargin;
layoutParams.leftMargin = X - viewLeftMargin;
layoutParams.topMargin = viewTopMargin;
layoutParams.bottomMargin = viewBottomMargin;
if (layoutParams.leftMargin <= lastMarginLeft) {
Log.d("ON_TOUCH", "Moving to left!");
secondCircleClick();
} else {
Log.d("ON_TOUCH", "Moving to right!");
firstCircleClick();
}
break;
}
return true;
}
}
);
}
public void firstCircleClick(){
textView1.setVisibility(View.VISIBLE);
textView2.setVisibility(View.VISIBLE);
textView3.setVisibility(View.VISIBLE);
textView4.setVisibility(View.INVISIBLE);
textView5.setVisibility(View.INVISIBLE);
textView6.setVisibility(View.INVISIBLE);
}
public void secondCircleClick(){
textView1.setVisibility(View.INVISIBLE);
textView2.setVisibility(View.INVISIBLE);
textView3.setVisibility(View.INVISIBLE);
textView4.setVisibility(View.VISIBLE);
textView5.setVisibility(View.VISIBLE);
textView6.setVisibility(View.VISIBLE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}