我有一个CollapsingToolbarLayout
及以下,我有一个LinearLayout
,其中包含fragments
和ViewPager
。但是,当我滚动时,fragments
都保持粘性,只有包含viewpager
的{{1}}正在滚动。如何同时制作RecyclerView
fragments
?
scrollable
答案 0 :(得分:0)
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;
}
}
FragmentSecond
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>