如何在一个活动之间使用onTouchEvent在布局之间滑动
问题描述:
swipping between two layouts我是新手android开发人员,我需要实现一个活动与几个布局或框架,我只想要一个显示在一个时间。我也想滑动这些布局(左侧或右侧)使用onTouchEvent,如图所示。 对此有何建议?提前致谢。如何在一个活动之间使用onTouchEvent在布局之间滑动
答
要达到此目的,您必须使用带页面滑动的选项卡。创建两个片段。
请按照这个过程。
我想你试图达到这个目标的方式是一种老方法。
对于带有选项卡的滑动页面,请执行以下操作。
在github上下载或复制以下两个文件并粘贴您的项目。除了setDistributeEvenly方法之外,这与developers.google.com上的相同。
activity_main.xml中
<your.package.name.SlidingTabLayout
android:clickable="true"
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</your.package.name.SlidingTabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
MyAdapter.java(在这里,我使用的两页只)
class MyPagerAdapter extends FragmentPagerAdapter
{
String[] title = {"All","Favourites"};
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment=null;
if (position==0)
fragment= new All();
if (position==1)
fragment= new Favourites();
return fragment;
}
@Override
public int getCount() {
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
return title[position];
}
}
tab_view.xml(查看选项卡的唯一,如果你想要你也可以使用ImageView他重)
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/tab_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text=""
android:padding="15dp"
android:textStyle="bold"
android:textSize="25dp"
/>
</FrameLayout>
MainActivity.java
private SlidingTabLayout tabLayout;
private ViewPager pager;
tabLayout= (SlidingTabLayout) findViewById(R.id.tabs);
pager = (ViewPager) findViewById(R.id.pager);
tabLayout.setCustomTabView(R.layout.tab_view,R.id.tab_title);
MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager());
pager.setAdapter(adapter);
tabLayout.setDistributeEvenly(true);
tabLayout.setViewPager(pager);
上面的代码是罚款。但最新的方式来实现与页面刷卡标签是通过Android支持设计库。
答
我想你想要SwipeViews(http://developer.android.com/design/patterns/swipe-views.html)。在http://developer.android.com/training/implementing-navigation/lateral.html
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
更多细节:您可以通过使用ViewPager实现这一目标。快乐的编码!
有太多你想要的教程。所有你需要的是使用谷歌来帮助你。 –