Java 向下滑动碎片
我试图通过向下滑动来显示片段我正在使用Viewpager,但我只能向右或向左滑动,我需要的是一些类似于Snapchat的东西,他们使用片段,但可以通过向任何方向(向上向下、向右或向左)滑动来访问片段,这是我目前使用的代码,它可以完美地进行典型的片段移动(左右),但我需要添加一些东西,使我可以通过向下滑动来访问片段Java 向下滑动碎片,java,android,android-layout,android-studio,android-fragments,Java,Android,Android Layout,Android Studio,Android Fragments,我试图通过向下滑动来显示片段我正在使用Viewpager,但我只能向右或向左滑动,我需要的是一些类似于Snapchat的东西,他们使用片段,但可以通过向任何方向(向上向下、向右或向左)滑动来访问片段,这是我目前使用的代码,它可以完美地进行典型的片段移动(左右),但我需要添加一些东西,使我可以通过向下滑动来访问片段 import ... public class MainActivity extends AppCompatActivity { /** * The {@link android.s
import ...
public class MainActivity extends AppCompatActivity {
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
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);
// FullScreen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
// 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);
}
@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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_main, container, false);
return rootView;
}
}
/**
* 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) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new RegisterLogin();
break;
case 1:
fragment = new PrincipalPage();
break;
case 2:
fragment = new DevsArea();
break;
}
return fragment;
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
}
}
下面是XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.vs.versus.versus.MainActivity">
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
看看这里的
RecyclerView
将RecyclerView
方向设置为VERTICAL
,并使项目布局成为根视图视图
使用height=“match\u parent”
最后一步,实现SnapHelper
或直接使用LinearSnapHelper
并将其应用于RecyclerView
这里是SnapHelper
现在你有了一个垂直的ViewPager
,类似的东西
或者,只需使用其他花花公子项目
如果您需要全方位(左、右、上、下)滑动,请将垂直的
RecyclerView
包装成水平的ViewPager
谢谢您,我会尝试的,我希望这就是您想要的。如果没有,请告诉meI,我认为RecyclerView可能对我有所帮助。问题是,我不知道如何在RecyclerView中放置活动。请将RecyclerView设置为您活动的布局。我如何才能做到这一点?在我的活动的XML中还是在该活动的java文件中?