gpt4 book ai didi

java - fragment View 寻呼机显示为空

转载 作者:行者123 更新时间:2023-12-02 10:47:24 24 4
gpt4 key购买 nike

我尝试了很多这个问题的答案,但它们都不适合我。我有一个 View 寻呼机,其中包含三个 fragment ,但是当我在手机上运行该应用程序时, fragment 不会出现,并且 View 寻呼机显示为空。

这是主要 Activity 代码:

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
assert actionBar != null;
actionBar.setDisplayShowTitleEnabled(false);


mAuth = FirebaseAuth.getInstance();
database = FirebaseDatabase.getInstance();
myRef = database.getReference("posts");

mViewPager = findViewById(R.id.viewpager);
adapter = new SamplePagerAdapter(getSupportFragmentManager(),this);
tabLayout = findViewById(R.id.tablayout);
mViewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(mViewPager);

mViewPager.setCurrentItem(1);
TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabOne.setText("تعلم");
tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.learn, 0, 0);
tabLayout.getTabAt(0).setCustomView(tabOne);

TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabTwo.setText("الصفحة الرئيسية");
tabTwo.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.home, 0, 0);
tabLayout.getTabAt(1).setCustomView(tabTwo);

TextView tabThree = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabThree.setText("التحديات");
tabThree.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.challenges, 0, 0);
tabLayout.getTabAt(2).setCustomView(tabThree);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
tabPosition = tab.getPosition();
Toast.makeText(MainActivity.this,
"Selected page position: " + tab.getPosition(), Toast.LENGTH_SHORT).show();
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {

}

@Override
public void onTabReselected(TabLayout.Tab tab) {

}
});

}

and this is the view pager adapter code :

public class SamplePagerAdapter extends FragmentPagerAdapter {
Context context;

public SamplePagerAdapter(FragmentManager fragmentManager, Context context) {
super(fragmentManager);
this.context = context;
}

/**
* @return the number of pages to display
*/
@Override
public int getCount() {
return 3;
}

/**
* @return true if the value returned from {@link #instantiateItem(ViewGroup, int)} is the
*/
@Override
public boolean isViewFromObject(View view, Object o) {
return o == view;
}

// BEGIN_INCLUDE (pageradapter_getpagetitle)

/**
* Return the title of the item at {@code position}. This is important as what this method
* <p>
* Here we construct one using the position value, but for real application the title should
* refer to the item's contents.
*/
@Override
public CharSequence getPageTitle(int position) {
String title = "";
switch (position) {
case 0:
title = "تعلم";
break;
case 1:
title = "الصفحة الرئيسية";
break;
case 2:
title = "التحديات";
break;
}
return title;
}
// END_INCLUDE (pageradapter_getpagetitle)

@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Toast.makeText(context, "position 0", Toast.LENGTH_SHORT).show();
return new LearnFragment();
case 1:
Toast.makeText(context, "position 1", Toast.LENGTH_SHORT).show();
return new HomeFragment();
case 2:
Toast.makeText(context, "position 2", Toast.LENGTH_SHORT).show();
return new ChallengesFragment();
}

return null;
}

}

这是 Activity 主要 xml 代码:

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/sample_main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/color1">

<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/app_name"
android:textColor="@color/white"
android:textSize="@dimen/text_size_in_toolbar" />
</android.support.v7.widget.Toolbar>

<android.support.design.widget.TabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/color1">

</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>


<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="@android:color/white" />

</LinearLayout>

最佳答案

@Override
public boolean isViewFromObject(View view, Object o) {
return o == view;
}

删除此代码

关于java - fragment View 寻呼机显示为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52450180/

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