gpt4 book ai didi

java - 如何解决viewpager中的相同内容复制

转载 作者:行者123 更新时间:2023-12-04 10:00:10 26 4
gpt4 key购买 nike

我正在使用 TabLayout 和 ViewPager 开发应用程序。

结果,我设置了图像以显示在不同的 fragment 中,例如。 fragment_page1、fragment_page2、fragment_page3 等。但是 Fragment_page1 中的图像在所有 fragment 中都是可见的。请问这个问题怎么解决?

这是我的 FragmentAdapter 类

public class SimpleFragmentPageAdapter extends FragmentPagerAdapter {
private String[] tabtitle=new String[]{"Description","Directions","Developers","Chant Page","Chant Page",};
Context context;
private int pagecount=4;
public SimpleFragmentPageAdapter(FragmentManager fm, Context context) {
super(fm);
this.context=context;
}

@Override
public Fragment getItem(int position) {
switch (position) {
case 0: //Fragment # 0 - This will show DevelopersFragment
return DescriptionFragment.newInstance(0, "page # 0");
case 1: //Fragment # 0 - This will show Page1Fragment
return Page1Fragment.newInstance(1, "Chant # Page1");
case 2: //Fragment # 0 - This will show Page1Fragment
return Page1Fragment.newInstance(2, "Chant # Page2");
case 3: //Fragment # 0 - This will show Page1Fragment
return Page1Fragment.newInstance(3, "Chant # Page2");
//etc
default:
return null;
}
}

@Override
public int getCount() {
return pagecount;
}

@Nullable
@Override
public CharSequence getPageTitle(int position) {
return "Chant Page" + position;
}
}

这是我的 fragment_page1 xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:gravity="top|fill_vertical"
android:background="#efc393"
tools:context=".Page1Fragment">

<ScrollView
android:id="@+id/scroller"
android:layout_width="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_height="wrap_content">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/ImPage1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="@drawable/page1_1" />

<ImageView
android:id="@+id/ImPage2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/ImPage1"
android:scaleType="fitXY"
android:src="@drawable/page1_2" />

<ImageView
android:id="@+id/ImPage3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/ImPage2"
android:scaleType="fitXY"
android:src="@drawable/page1_3" />

<ImageView
android:id="@+id/ImPage4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/ImPage3"
android:scaleType="fitXY"
android:src="@drawable/page1_4" />

</RelativeLayout>

</ScrollView>

</RelativeLayout>

这是我的 fragment_page2
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:gravity="top|fill_vertical"
android:background="#efc393"
tools:context=".Page2Fragment">

<ScrollView
android:id="@+id/scroller"
android:layout_width="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_height="wrap_content">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/ImPage2a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="@drawable/page2_1" />

<ImageView
android:id="@+id/ImPage2b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/ImPage2a"
android:scaleType="fitXY"
android:src="@drawable/page2_2" />

<ImageView
android:id="@+id/ImPage3c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/ImPage2b"
android:scaleType="fitXY"
android:src="@drawable/page2_3" />

</RelativeLayout>

这是运行 ViewPager 的 Activity
 <com.google.android.material.tabs.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tab_layout"
app:tabSelectedTextColor="#ffffff"
app:tabMode="scrollable"
app:tabGravity="center"
app:tabTextColor="#A56302"
app:tabBackground="@drawable/pager"
app:tabIndicatorColor="#f10606"
app:tabIndicatorHeight="7dp">

</com.google.android.material.tabs.TabLayout>

<!-- Tab divider -->
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:background="@android:color/black" />

<androidx.viewpager.widget.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contextClickable="true"
android:id="@+id/view_pager"
android:layout_marginBottom="70dp"
android:layout_below="@+id/tab_layout"
app:tabMode="fixed"
app:tabGravity="fill"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

</androidx.viewpager.widget.ViewPager>


<!-- view for AdMob Banner Ad -->
<FrameLayout
android:id="@+id/ad_view_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_alignParentBottom="true" />


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tips_7);


ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("CHANT");



ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
viewPager.setAdapter(new com.aronti.chanthymnography.SimpleFragmentPageAdapter(getSupportFragmentManager(), this));

//Attach the page change listener inside the activity
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
// This method will be invoked when a new page becomes selected
@Override
public void onPageSelected(int position) {
Toast.makeText(Tips_7.this, "Selected Chant:" + position, Toast.LENGTH_SHORT).show();
}

// This method will be invoked when the current page is scrolled
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetpixels) {

}

// Called when the scroll state changes:
//SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING, SCROLL_STATE_SETTLING
@Override
public void onPageScrollStateChanged(int state) {

}
});

tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(viewPager);

}

最佳答案

您应该在 getItem(int position) 中设置不同的类部分。

Return the Fragment associated with a specified position.


@Override
public Fragment getItem(int position) {
switch (position) {
case 0: //Fragment # 0 - This will show DevelopersFragment
return DescriptionFragment.newInstance(0, "page # 0");
case 1: //Fragment # 0 - This will show Page1Fragment
return Page1Fragment.newInstance(1, "Chant # Page1");
case 2: //Fragment # 0 - This will show Page1Fragment
return Page2Fragment.newInstance(2, "Chant # Page2");
case 3: //Fragment # 0 - This will show Page1Fragment
return Page3Fragment.newInstance(3, "Chant # Page2");
//etc
default:
return null;
}
}

关于java - 如何解决viewpager中的相同内容复制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61852144/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com