I can not add the swipe right, I want to open a new activity after swipe right
public class MainActivity extends AppCompatActivity implements View.OnTouchListener{
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
*/
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
/* FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});*/
}
@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);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
return false;
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}
@Override
public int getCount() {
// Show 3 total pages.
return 5;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "SECTION 1";
case 1:
return "SECTION 2";
case 2:
return "SECTION 3";
}
return null;
}
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
// TextView textView = (TextView) rootView.findViewById(R.id.section_label);
//textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
}
答案 0 :(得分:1)
You need to add a page change listener to your ViewPager and launch the new activity when detect the swipe right:
$request->setViewportSize(1280, 1024);
答案 1 :(得分:0)
You should add a listener to your ViewPager
. Add this line to your code
mViewPager.setOnTouchListener(this);
答案 2 :(得分:0)
如果有人在2019年遇到此问题,我在youtube上发现了一个不错的方法。我在项目的空活动中实现了代码。
public class RSA extends AppCompatActivity implements GestureDetector.OnGestureListener {
public static final int SWIPE_THRESHOLD = 100;
public static final int SWIPE_VELOCITY_THRESHOLD = 100;
private GestureDetector gestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rsa);
this.setTitle("RSA"); // edit the action bar text
gestureDetector = new GestureDetector(this);
}
@Override
public boolean onDown(MotionEvent motionEvent) {
return false;
}
@Override
public void onShowPress(MotionEvent motionEvent) {
}
@Override
public boolean onSingleTapUp(MotionEvent motionEvent) {
return false;
}
@Override
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
return false;
}
@Override
public void onLongPress(MotionEvent motionEvent) {
}
@Override
public boolean onFling(MotionEvent downEvent, MotionEvent moveEvent, float velocityX, float velocityY) {
boolean result = false;
float diffY = moveEvent.getY() - downEvent.getY();
float diffX = moveEvent.getX() - downEvent.getX();
//which movement was greater, across x or y
if(Math.abs(diffX) > Math.abs(diffY)){
//swipe left or right
if(Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD){
if(diffX > 0){
//swipe right
} else {
//swipe left
}
result = true;
}
} else {
//up or down swipe
if(Math.abs(diffY)> SWIPE_THRESHOLD && Math.abs(diffY)> SWIPE_VELOCITY_THRESHOLD){
if(diffY > 0){
//bottom swipe
onSwipeBottom();
} else {
//topswipe
onSwipeTop();
}
result = true;
}
}
return result;
}
private void onSwipeBottom() {
//here we start the bottom activity
Intent intent = new Intent(this, rsaAlgorithm.class);
startActivity(intent);
}
private void onSwipeTop() {
//here we start the info activity
Intent intent = new Intent(this, rsaInformation.class);
startActivity(intent);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
gestureDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
}
代码中的许多部分都是自解释性的,但是如果您有任何疑问,请随时提出。 您将需要导入一些内容,只需在出现的红色文本上按alt +并输入。 帮助我的youtube视频:https://www.youtube.com/watch?v=32rSs4tE-mc&list=LLgb_VsN-nX-URXVVHvn4b4g&index=4&t=0s
在类中实现Alt + enter进入GestureDetector.OnGestureListener时很重要,以便默认情况下添加所需的方法。
我实现了顶部和底部的滑动,如果要完成向右滑动,只需将onSwipebottom / top方法中的代码添加到onSwipeRight方法中。该意图将打开您的新活动。您需要对其进行编辑以使其适合您的情况。