我希望每个标签都能显示不同的内容。要开始,我想对Place
标签进行更改。问题是,Android模拟器中没有反映任何变化。
我所指的图像
在这种情况下,我希望文本something
显示,但事实并非如此。我究竟做错了什么?我还提供了可能与此问题相关的代码。
这是MainActivity.java
:
import android.net.Uri;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity implements Place.OnFragmentInteractionListener, Profile.OnFragmentInteractionListener, Take.OnFragmentInteractionListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabLayout tabLayout = (TabLayout)findViewById(R.id.tabLayout);
tabLayout.addTab(tabLayout.newTab().setText("Place"));
tabLayout.addTab(tabLayout.newTab().setText("Take"));
tabLayout.addTab(tabLayout.newTab().setText("Profile"));
final ViewPager viewPager = (ViewPager)findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(),tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.setOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
@Override
public void onFragmentInteraction(Uri uri) {
}
}
这是fragment_place.xml
:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Place"
android:layout_centerInParent="true"
android:textSize="30dp"
android:id="@+id/textView" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="41dp"
android:layout_marginStart="41dp"
android:layout_marginTop="80dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Something" />
</RelativeLayout>
这是PagerAdapter.java
:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNoOfTabs;
public PagerAdapter(FragmentManager fm, int NumberOfTabs) {
super(fm);
this.mNoOfTabs = NumberOfTabs; // set global number of tabs to local number of tabs
}
@Override
public Fragment getItem(int position) {
switch(position) {
case 0:
Place place = new Place();
return place;
case 1:
Profile profile = new Profile();
return profile;
case 2:
Take take = new Take();
return take;
default:
return null;
}
}
@Override
public int getCount() {
return 0;
}
}
答案 0 :(得分:0)
我没有在你的代码中看到你为片段提供布局xml
答案 1 :(得分:0)
将行更改为给定代码。
@Override
public int getCount() {
return mNoOfTabs ;
}
另外,请确保您的布局是否属于Place(Fragment)onViewCreated()
方法