簡體   English   中英

為什么在“位置”標簽中看不到任何更改

[英]Why can't I see any changes in my “Place” tab

我希望每個標簽顯示不同的內容。 首先,我想對“位置Place選項卡進行更改。 問題是,所有更改均未反映在Android Simulator中。

我指的是什么圖片

在這種情況下,我希望文字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;
    }
}

我在您的代碼中看不到您為片段提供布局xml的位置

將行更改為給定的代碼。

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

另外,請確保您膨脹的布局屬於onViewCreated()方法的Place(Fragment)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM