现在我开发了一个关于联系人的应用程序
我想,当用户在列表视图中向左移动项目时,他可以使用该号码电话拨打电话,当向右滑动发送信息时。
一切都好! ,但我希望当刷到左侧背景的项目更改为绿色时,请按照用户手指,向右滑动时更改为黄色。
或
我在OnTouchListener中的代码是:
public class OnSwipeTouchListener implements OnTouchListener {
private final GestureDetector gestureDetector;
public OnSwipeTouchListener (android.content.Context ctx){
gestureDetector = new GestureDetector(ctx, new GestureListener());
}
private final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
}
} else {
if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
onSwipeBottom();
} else {
onSwipeTop();
}
}
}
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}
public void onSwipeRight() {
}
public void onSwipeLeft() {
}
public void onSwipeTop() {
}
public void onSwipeBottom() {
}
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
return gestureDetector.onTouchEvent(arg1);
}
}
用法在自定义数组适配器中:
Row.setOnTouchListener(new OnSwipeTouchListener(context)
{
@Override
public void onSwipeLeft()
{
CallAndMsgManager call = new CallAndMsgManager();
call.CallHandler(context, "123456789");
super.onSwipeLeft();
}
});
答案 0 :(得分:0)
我必须使用here中的选择器创建选择器并在其中添加下面的代码行
<item android:state_hovered="true" android:drawable="@drawable/vertical_line"></item>
谢谢。
答案 1 :(得分:0)
最后我找到了解决这个问题的好方法:
我使用布局(0 - 1 - 2),布局1是默认布局,当向左滑动布局2时,向右滑动布局0显示
首先我们需要一个像这样的viewflipper:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ViewFlipper
android:id="@+id/vfContactInfo"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- Child 1 -->
<LinearLayout
android:id="@+id/llcallContact"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/msg">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="Message"
android:textSize="20sp"
android:textColor="#FFFFFF"/>
</LinearLayout>
<!-- Child 2 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#44ffffff"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imgPhoneEmailAddressType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp" />
<TextView
android:id="@+id/lblPhoneEmailAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_marginTop="12dp"
android:layout_marginLeft="7dp"
android:layout_marginRight="10dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<!-- Child 3 -->
<LinearLayout
android:id="@+id/llMsgContact"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/call">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="Call"
android:textSize="20sp"
android:textColor="#FFFFFF"/>
</LinearLayout>
</ViewFlipper>
</LinearLayout>
现在你必须创建适配器来为listview
扩充行public android.view.View getView(final int position,
android.view.View convertView, android.view.ViewGroup parent) {
android.view.LayoutInflater inflater = (LayoutInflater) context.getSystemService(android.content.Context.LAYOUT_INFLATER_SERVICE);
final android.view.View Row =inflater.inflate(resId, parent , false);
viewFlipper = (ViewFlipper) Row.findViewById(R.id.vfLogRow);
viewFlipper.showNext(); // to show loayot 2 by default
Row.setOnTouchListener(new OnSwipeTouchListener(context)
{
@Override
public void onSwipeLeft()
{
CallAndMsgManager call = new CallAndMsgManager();
call.CallHandler(context, arrData.get(position).GetNumber(0).GetNumber());
super.onSwipeLeft();
}
@Override
public void onClicked() {
android.content.Intent intent = new android.content.Intent(context, ShowLogNumber.class);
intent.putExtra("index", position);
context.startActivity(intent);
super.onClicked();
}
});
return Row;
}
现在你必须创建OnSwipeTouchListener类来监听触摸事件,在这个类中你必须覆盖ontouch函数:
public boolean onTouch(View arg0, MotionEvent touchevent) {
try {
viewFlipper = (ViewFlipper) arg0.findViewById(R.id.vfLogRow);
int width = viewFlipper.getWidth();
switch (touchevent.getAction())
{
// when user first touches the screen to swap
case MotionEvent.ACTION_DOWN:
{
lastX = touchevent.getX();
if (viewFlipper.getDisplayedChild() > 0)
{
View leftChild = viewFlipper.getChildAt(viewFlipper.getDisplayedChild() - 1);
leftChild.setVisibility(View.INVISIBLE);
leftChild.layout(-width,
leftChild.getTop(), 0,
leftChild.getBottom());
}
if (viewFlipper.getDisplayedChild() < 2)
{
View rightChild = viewFlipper.getChildAt(viewFlipper.getDisplayedChild() + 1);
rightChild.setVisibility(View.INVISIBLE);
rightChild.layout(width,
rightChild.getTop(), width * 2,
rightChild.getBottom());
}
break;
}
case MotionEvent.ACTION_MOVE:
{
View currentView = viewFlipper.getCurrentView();
currentView.layout((int)(touchevent.getRawX() - lastX),
currentView.getTop(), (int)(touchevent.getRawX() - lastX) + width,
currentView.getBottom());
if (viewFlipper.getDisplayedChild() > 0)
{
View leftChild = viewFlipper.getChildAt(viewFlipper.getDisplayedChild() - 1);
leftChild.layout((int)(touchevent.getRawX() - lastX - width),
leftChild.getTop(), (int)(touchevent.getRawX() - lastX),
leftChild.getBottom());
//Sets the left view to visible so it shows
if (leftChild.getVisibility() == View.INVISIBLE)
{
leftChild.setVisibility(View.VISIBLE);
}
}
if (viewFlipper.getDisplayedChild() < 2)
{
View rightChild = viewFlipper.getChildAt(viewFlipper.getDisplayedChild() + 1);
rightChild.layout((int)(touchevent.getRawX() - lastX + width),
rightChild.getTop(), (int)(touchevent.getRawX() - lastX + (width * 2)),
rightChild.getBottom());
if (rightChild.getVisibility() == View.INVISIBLE)
{
rightChild.setVisibility(View.VISIBLE);
}
}
break;
}
case MotionEvent.ACTION_UP:
{
float releasePoint = touchevent.getRawX();
float mid = MainActivity.densityDpi / 2;
int x1 = 3, x2 = 3 ;
if(lastX < mid)
x1 = 0;
else
x1 = 1;
if(releasePoint < mid)
x2 = 0;
else
x2 = 1;
if(x1 == 0 && x2 == 1)
{
viewFlipper.setInAnimation(MainActivity.animCallMsg);
viewFlipper.showPrevious();
onSwipeRight();
}
if(x1 == 1 && x2 == 0)
{
viewFlipper.setInAnimation(MainActivity.animCallMsg);
viewFlipper.showNext();
onSwipeLeft();
}
viewFlipper.setInAnimation(MainActivity.animCallMsg);
viewFlipper.setDisplayedChild(1);
break;
}
case MotionEvent.ACTION_CANCEL:
{
viewFlipper.setInAnimation(MainActivity.animCallMsg);
viewFlipper.setDisplayedChild(1);
break;
}
}
} catch (Exception e) {
}
finally
{
return gestureDetector.onTouchEvent(touchevent);
}
}
}