【问题标题】:Swipe down Fragment Android向下滑动 Fragment Android
【发布时间】:2018-04-23 07:29:41
【问题描述】:

我试图通过向下滑动来显示片段我正在使用 Viewpager 但我只能向右或向左滑动,我需要的是类似于 Snapchat 所做的它们使用片段但可以通过向任何方向滑动来访问它们(向上向下,向右或向左),这是我目前使用的代码,它可以完美地制作典型的片段运动(左右),但我需要添加一些东西,使我可以通过向下滑动来访问片段。

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>

【问题讨论】:

    标签: java android android-layout android-studio android-fragments


    【解决方案1】:

    看看RecyclerView这里https://developer.android.com/guide/topics/ui/layout/recyclerview.html

    RecyclerView 方向设置为VERTICAL 并使您的项目布局根View 使用height="match_parent"

    最后一步,实现SnapHelper或直接使用LinearSnapHelper并将其应用到RecyclerView

    这里是SnapHelper https://developer.android.com/reference/android/support/v7/widget/SnapHelper.html

    现在你有一个垂直的ViewPager,类似的东西。

    或者,只是使用其他一些花花公子项目

    https://github.com/kaelaela/VerticalViewPager

    https://github.com/chadguo/VerticalViewPager

    如果您需要完整的 4 向滑动(左、右、上、下),请将垂直的 RecyclerView 换成水平的 ViewPager

    【讨论】:

    • 谢谢,我会试试的????
    • 我希望这就是你要找的。如果没有,请告诉我
    • 我认为 RecyclerView 可以帮助我解决问题是我不知道如何将活动放入 RecyclerView
    • 将 recyclerciew 设置为您的活动的布局
    • 我该怎么做?在我的活动的 XML 中还是在活动的 java 文件中?
    猜你喜欢
    • 1970-01-01
    • 2017-02-05
    • 1970-01-01
    • 2013-10-31
    • 1970-01-01
    • 2011-04-11
    • 1970-01-01
    • 2019-02-12
    • 1970-01-01
    相关资源
    最近更新 更多