将活动更改为碎片,以便“滑动活动”成为可能

时间:2013-11-25 05:38:57

标签: java android eclipse android-activity android-animation

我需要创建一个“滑动活动”,当用户滑动它时它会滑动。当用户向左“滑动”时,活动应向左滑动,新活动将从右侧进入屏幕。你不能把它放在中间。雅虎邮箱应用程序也是如此。您在阅读邮件时可以看到此信息,然后向左滑动邮件以转到下一封邮件。就像下面一样。

enter image description here

在图像中,您可以看到下一封邮件(标题为“谢谢”)从侧面打开。就我而言,它们是两种不同的活动。我们还可以在当前活动的右侧显示下一个活动的一小部分(例如在Windows手机中),这样用户就知道他必须刷卡。无论如何,这部分现在都是强制性的。

我尝试用this answer来做,但这不是我想要的。好吗?

注意

如果我选择片段,很多课程将被更改或完全重新编码。

更新

请查看以下代码。在这里,当我在SalesInquiry.java上滑动时,NewLead.java应该是开放的,具有上述效果。

DropDownMenu.java

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class DropDownMenu extends Activity {

    private TextView addInquiry, addEvent, additionalInfo, addToContacts;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drop_down_menu);

        //Intializing instance variables
        addInquiry = (TextView)findViewById(R.id.menu_add_inquiry);
        addEvent = (TextView)findViewById(R.id.menu_add_event);
        additionalInfo = (TextView)findViewById(R.id.menu_additional_info);
        addToContacts = (TextView)findViewById(R.id.menu_add_to_contacts);

        //Register the Listeners
        addInquiry.setOnClickListener(new AddInquiryEvent());


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.drop_down_menu, menu);
        return true;
    }

    //Test Button
    private class AddInquiryEvent implements OnClickListener
    {

        @Override
        public void onClick(View arg0) 
        {
            // TODO Auto-generated method stub
            Intent intent = new Intent(DropDownMenu.this,NewLead.class);
            startActivity(intent);

        }

    }

    public void onMenuItemClicked(View view) {
        switch (view.getId()) {
        case R.id.menu_add_inquiry:
            Intent intent = new Intent(DropDownMenu.this,NewLead.class);
            startActivity(intent);
            break;
        default:;
        }
    }

}

SalesInquery.java

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class SalesInqury extends DropDownMenu {

    private ImageView addNewSalesInqury;
    private RelativeLayout salesInquryMainLayout;
    private TextView testEditSales;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);     
        setContentView(R.layout.activity_sales_inqury);

        //Intializing instance variables
        addNewSalesInqury = (ImageView)findViewById(R.id.add_new_sales_inqury_btn);
        salesInquryMainLayout = (RelativeLayout)findViewById(R.id.sales_inqury_main_layout);
        testEditSales = (TextView)findViewById(R.id.testWord);


        //Registering Event handlers
        addNewSalesInqury.setOnClickListener(new addNewSalesInquryEvent());
        salesInquryMainLayout.setOnTouchListener(mainLayoutSwiped);
        testEditSales.setOnClickListener(new OpenSalesDemo()); //TESTING DEMO!!!!!
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.sales_inqury, menu);
        return true;
    }

    //Event Handler for Add New Sales Inquery Button
    private class addNewSalesInquryEvent implements OnClickListener
    {

        @Override
        public void onClick(View v) 
        {
            // TODO Auto-generated method stub
            Intent intent = new Intent(SalesInqury.this,NewLead.class);
            startActivity(intent);

        }

    }

    //SWIP LISTENER
    private  OnTouchListener mainLayoutSwiped = new OnSwipeTouchListener()
    {
        public boolean onSwipeLeft() 
         {
             Intent intent = new Intent(SalesInqury.this,NewLead.class);
             startActivity(intent);
            return true;
         }
    };


    //
    //
    // TESTING FUNCTION!!
    //
    //
    //Testing function for sales lead opening
    private class OpenSalesDemo implements OnClickListener
    {

        @Override
        public void onClick(View arg0) 
        {
            // TODO Auto-generated method stub
            Intent intent = new Intent(SalesInqury.this,EditLeads.class);
            startActivity(intent);

        }

    }

}

3 个答案:

答案 0 :(得分:3)

            User GestureDetector and in onFling() method check for the direction of swipe and start the activity accordingly  

            Edited:
    Try this:
            GestureDetector gestureDetector = new GestureDetector(
                    simpleOnGestureListener);

        SimpleOnGestureListener simpleOnGestureListener = new SimpleOnGestureListener() {

                // Move The Background Theme To Select The Theme
                @Override
                public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                        float velocityY) {

                    float sensitvity = 100;

                    if ((e1.getX() - e2.getX()) > sensitvity) {

    //Move right ,start new Activity here
return true;
        }else if ((e2.getX() - e1.getX()) > sensitvity) {
    //Move left ,start new Activity here
return true;
        }else{
return false ;
}
}

@Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        return (gestureDetector.onTouchEvent(event) || super
                .onTouchEvent(event));
    }

答案 1 :(得分:0)

您可以使用片段代替活动

尝试使用viewpager库,参见

http://viewpagerindicator.com/

答案 2 :(得分:0)

您可以简单地使用Fragments精确支持片段和ViewPager,它们可以让您浏览不同的片段。它在android.support.v4库中可用。