我下载了一个名为SimpleGestureListener的Java类,该类是由名为CoXier的用户创建的。
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
public class SimpleGestureListener extends GestureDetector.SimpleOnGestureListener {
private static final String TAG = "SimpleGestureListener";
private Listener mListener;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
Log.i(TAG,e1.toString()+"\n"+e2.toString());
Log.d(TAG,"distanceX = "+distanceX+",distanceY = "+distanceY);
if (mListener == null)
return true;
if (distanceX == 0 && Math.abs(distanceY) > 1){
mListener.onScrollVertical(distanceY);
}
if (distanceY == 0 && Math.abs(distanceX) > 1){
mListener.onScrollHorizontal(distanceX);
}
return true;
}
public void setListener(Listener mListener) {
this.mListener = mListener;
}
interface Listener{
/**
* left scroll dx >0
* right scroll dx <0
* @param dx
*/
void onScrollHorizontal(float dx);
/**
* upward scroll dy > 0
* downward scroll dy < 0
* @param dy
*/
void onScrollVertical(float dy);
}
}
在仅包含1个活动的应用程序中使用它时,它可以完美运行。 例如,在这里我可以使用水平或垂直滑动来更改布局的背景颜色: (此后,我禁用了水平滑动功能)
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
public class MainActivity extends AppCompatActivity
{
private static final String TAG = "MainActivity";
private GestureDetector mDetector;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SimpleGestureListener simpleGestureListener = new SimpleGestureListener();
simpleGestureListener.setListener(new SimpleGestureListener.Listener()
{
@Override
public void onScrollHorizontal(float dx)
{
}
@Override
public void onScrollVertical(float dy)
{
ConstraintLayout constraintLayout = findViewById(R.id.constraint_layout);
if (dy > 0)
{
constraintLayout.setBackgroundColor(getResources().getColor(R.color.blue));
}
if (dy < 0)
{
constraintLayout.setBackgroundColor(getResources().getColor(R.color.yellow));
}
}
});
mDetector = new GestureDetector(this, simpleGestureListener);
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
mDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
}
现在我的问题是我想在我的原始应用程序中使用它,该应用程序由片段和ViewPager组成。 我想在1个片段中添加此SwipeUp / Down功能。 (其他片段我不需要。)
我必须在片段类中包含什么?
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Page_2 extends Fragment
{
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View PageTwo = inflater.inflate(R.layout.page2, container, false);
return PageTwo;
}
}
我是否必须使MainActivity.java与代码块2相同? (示例应用) 目前,我的MainActivity.java如下所示:
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity
{
ViewPager vp;
private static final String TAG = "MainActivity";
private static GestureDetector mDetector;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SimpleGestureListener simpleGestureListener = new SimpleGestureListener();
simpleGestureListener.setListener(new SimpleGestureListener.Listener()
{
@Override
public void onScrollHorizontal(float dx)
{
Log.wtf("swipe", "horizontal = " + dx);
}
@Override
public void onScrollVertical(float dy)
{
Log.wtf("swipe", "vertical = " + dy);
}
});
mDetector = new GestureDetector(this, simpleGestureListener);
ViewPager vp = findViewById(R.id.vp);
SetUpViewPager(vp);
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
mDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
public void SetUpViewPager(ViewPager viewpage)
{
MyViewPagerAdapter Adapter = new MyViewPagerAdapter(getSupportFragmentManager());
Adapter.AddPageFragment(new Page_1(), "Page 1"); // Load pages inside of ViewPager
Adapter.AddPageFragment(new Page_2(), "Page 2");
Adapter.AddPageFragment(new Page_3(), "Page 3");
Adapter.AddPageFragment(new Page_5(), "Page 5");
viewpage.setAdapter(Adapter);
}
public class MyViewPagerAdapter extends FragmentPagerAdapter
{
private List<Fragment> MyFragment = new ArrayList<>();
private List<String> MyPageTitle = new ArrayList<>();
public MyViewPagerAdapter(FragmentManager manager)
{
super(manager);
}
public void AddPageFragment(Fragment Frag, String Title)
{
MyFragment.add(Frag);
MyPageTitle.add(Title);
}
@Override
public Fragment getItem(int i)
{
return MyFragment.get(i);
}
@Nullable
@Override
public CharSequence getPageTitle(int position)
{
return MyPageTitle.get(position);
}
@Override
public int getCount()
{
return 4; // 4 pages total
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
int id = item.getItemId();
if (id == R.id.action_settings)
{
return true;
}
return super.onOptionsItemSelected(item);
}
}
我认为大多数代码(与GestureDetector相关)都必须在Page_2中? 也许我也必须使用
mDetector = new GestureDetector(getActivity(), simpleGestureListener);
在Page_2中,因为否则它将找不到它。
无论如何,无论我如何尝试,它都无法像单项活动测试应用程序那样发挥作用。
有什么主意吗?
答案 0 :(得分:0)
也许您只是在做一些测试,而不是仅仅通过左右滚动来更改背景颜色。 我不知道。我只是对您提供的代码提出建议。
有一个ViewPager.OnPageChangeListener。 就像Adapter或ClickListener是ListView一样,告诉您哪个页面/项目正在调用该方法。
您只需跟踪页面更改索引即可知道寻呼机向左或向右摆动。
int lastPosition = 0;
void onPageSelected(int position) {
if(position < lastPosition);
else if(position > lastPosition);
lastPosition = position;
}
顺便说一句,我没有测试我的代码。但是我今天才使用ViewPager.OnPageChangedListener。