#android #android-fragments #scroll #android-viewpager #android-collapsingtoolbarlayout
#Android #android-фрагменты #прокрутка #android-viewpager #android-collapsingtoolbarlayout
Вопрос:
У меня есть a CollapsingToolbarLayout
, а ниже у меня есть a LinearLayout
с 2 fragments
и a ViewPager
. Однако, когда я прокручиваю, оба fragments
остаются липкими, и только viewpager
содержащее a RecyclerView
прокручивается правильно. Как я могу сделать fragments
scrollable
и то, и другое?
<android.support.design.widget.AppBarLayout
android:id="@ id/appbar"
android:layout_width="match_parent"
android:layout_height="200dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@ id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginBottom="90dp"
app:expandedTitleMarginEnd="64dp">
<ImageView
android:id="@ id/hero"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:fitsSystemWindows="true"
app:layout_collapseMode="parallax"/>
<android.support.v7.widget.Toolbar
android:id="@ id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="top"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="pin" />
<android.support.design.widget.TabLayout
android:id="@ id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/MyCustomTabLayout"
android:layout_gravity="bottom"
app:layout_collapseMode="pin"
app:tabMode="scrollable"/>
<LinearLayout android:id="@ id/ButtonBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center|start"
app:layout_collapseMode="pin"
android:fitsSystemWindows="true"
android:layout_marginTop="40dp"
android:layout_marginLeft="24dp"
android:padding="@dimen/activity_horizontal_margin_half">
<TextView
android:id="@ id/SignUpButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sign Up"
android:textAllCaps="true"
android:background="@drawable/rounded_border_white_inverted_selector"
android:textColor="@drawable/rounded_border_white_text_inverted_selector"
android:padding="@dimen/activity_horizontal_margin_half"/>
<TextView
android:id="@ id/LoginButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:textAllCaps="true"
android:layout_marginLeft="@dimen/activity_horizontal_margin_half"
android:background="@drawable/rounded_border_white_selector"
android:textColor="@drawable/rounded_border_white_text_selector"
android:padding="@dimen/activity_horizontal_margin_half"/>
</LinearLayout>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<fragment
android:id="@ id/fragmentSponsored"
android:name="SponsoredFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:layout="@layout/fragment_sponsored" />
<fragment
android:id="@ id/fragmentAnnouncement"
android:name="AnnouncementFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:layout="@layout/fragment_announcement" />
<android.support.v4.view.ViewPager
android:id="@ id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Ответ №1:
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@ id/mainactivity"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@ id/viewPager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
MainActivity
public class MainActivity extends AppCompatActivity {
MyPagerAdapter TabAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
pager.setOffscreenPageLimit(2);
private class MyPagerAdapter extends FragmentStatePagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int pos) {
switch (pos) {
case 0:
return SHGCreationFragmentFirst.newInstance("SHGCreationFragmentFirst, Instance 1");
case 1:
return MemberListFragment.newInstance("MemberListFragment, Instance 2");
}
return null;
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
@Override
public int getCount() {
return 2;
}
}
}
FragmentFirst
public class FragmentFirst extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_first, container, false);
return v;
}
public static FragmentFirst newInstance(String text) {
FragmentFirst f = new FragmentFirst();
Bundle b = new Bundle();
b.putString("msg", text);
f.setArguments(b);
return f;
}
}
Фрагментсекунда
public class FragmentSecond extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_second, container, false);
return v;
}
public static FragmentSecond newInstance(String text) {
FragmentSecond f = new FragmentSecond();
Bundle b = new Bundle();
b.putString("msg", text);
f.setArguments(b);
return f;
}
}
fragment_first.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="vertical"
android:padding="10dp">
</LinearLayout>
fragment_Second.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fcfcfc"
android:orientation="vertical"
android:padding="10dp">
</LinearLayout>
Комментарии:
1. Спасибо за ваш ответ, но это не решает проблему. Под прокруткой я подразумеваю вверх и вниз, а не прокрутку влево и вправо. Когда я прокручиваю RecyclerView, содержащийся на ViewPager, два фрагмента в верхней части ViewPager приклеиваются к вершине. Я хочу также сделать их прокручиваемыми.