我遇到的问题是,当我启动应用程序时,我的ViewPager文本没有显示。但是,当我滑动选项卡时,文本最终会显示。我不确定问题是什么,我希望在我的应用程序启动后立即显示该文本。任何帮助,将不胜感激!感谢。
国家/地区活动(主要活动)
package myapps.countryapp;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import static myapps.countryapp.R.id.pager;
public class CountriesActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_countries);
ViewPager viewPager = (ViewPager) findViewById(pager);
viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
}
public class MyAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 2;
//Create tab tiles.
private String tabTiles[] = new String[] {"Countries", "Map"};
Context context;
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
if(position == 0){
Fragment fragmentList = new Countries();
return fragmentList;
}else {
Countries fragmentList = new Countries();
return fragmentList;
}
}
@Override
public int getCount() {
return PAGE_COUNT;
}
@Override
public CharSequence getPageTitle(int position) {
return tabTiles[position];
}
}
}//end of class
Countries.java(片段文件)
package myapps.countryapp;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.app.Fragment;
/**
* A simple {@link Fragment} subclass.
*/
public class Countries extends Fragment {
public Countries() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_countries, container, false);
return view;
}
}
activity_countries( MAIN XML FILE )
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="fill_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="myapps.countryapp.CountriesActivity">
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id = "@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.PagerTabStrip
android:layout_width="match_parent"
android:layout_height = "40dp"
android:paddingTop = "4dp"
android:paddingBottom = "10dp"
android:layout_gravity = "top"
android:id = "@+id/pagerStrip">
</android.support.v4.view.PagerTabStrip>
</android.support.v4.view.ViewPager>
fragment_countries.xml
<FrameLayout 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" tools:context="myapps.countryapp.Countries">
<!-- TODO: Update blank fragment layout -->
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/MyListView">
</ListView>
</FrameLayout>
答案 0 :(得分:0)
好的,这是我的活动示例,viewpager和fragment:
活性:
public class KataloziSlike extends AppCompatActivity {
private AdapterCustomPage mAdapterCustomPage;
private ViewPager mViewPager;
private TextView tv, brojStranica;
private ImageButton settings, share, locirajMe, back;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_katalozi_slike);
if (Static.katalozi.getSlikeKataloga().size() == 1) {
Message.message(KataloziSlike.this, "Katalog ima samo jednu stranu");
}
brojStranica = (TextView) findViewById(R.id.tvBrojStranicaKataloga);
brojStranica.setText(String.valueOf(1) + "/" + String.valueOf(Static.katalozi.getSlikeKataloga().size() + 1));
mAdapterCustomPage = new AdapterCustomPage(getFragmentManager(), this);
mViewPager = (ViewPager) findViewById(R.id.view_pagerZaSlikeKataloga);
mViewPager.setAdapter(mAdapterCustomPage);
mViewPager.setCurrentItem(0);
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (Static.katalozi.getSlikeKataloga().size() != 1) {
if (position == 0) {
Message.message(KataloziSlike.this, "Na prvoj stranici kataloga ste");
} else if (position == Static.katalozi.getSlikeKataloga().size() - 1) {
Message.message(KataloziSlike.this, "Na poslednjoj stranici kataloga ste");
}
}
if (position <= Static.katalozi.getSlikeKataloga().size() - 1) {
YourFragmentInterface fragment = (YourFragmentInterface) mAdapterCustomPage.instantiateItem(mViewPager, position);
if (fragment != null) {
fragment.fragmentBecameVisible();
}
}
brojStranica.setText(String.valueOf(position + 1) + "/" + String.valueOf(Static.katalozi.getSlikeKataloga().size() + 1));
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
}
适配器:
public class AdapterCustomPage extends FragmentStatePagerAdapter {
protected Context mContext;
int maxBrojStrana = Static.katalozi.getSlikeKataloga().size();
int pozicija;
private ArrayList<Katalozi> mKatalozi;
public AdapterCustomPage(FragmentManager fm, Context context) {
super(fm);
mContext = context;
}
@Override
public Fragment getItem(int position) {
// Create fragment object
if (position == maxBrojStrana) {
Fragment fragment = new ImageFragmentMapKatalozi();
return fragment;
} else {
Fragment fragment = new ImageFragmentKatalozi();
fragment.onPause();
Bundle args = new Bundle();
Log.i("Poizicija", String.valueOf(position));
String url = Static.katalozi.getSlikeKataloga().get(position);
args.putString("url", url);
fragment.setArguments(args);
String link = Static.katalozi.getUrl();
args.putString("linkKataloga", link);
return fragment;
}
}
@Override
public int getItemPosition(Object object) {
pozicija = super.getItemPosition(object);
return super.getItemPosition(object);
}
@Override
public int getCount() {
return maxBrojStrana + 1;
}
}
和片段:
public class ImageFragmentKatalozi extends Fragment implements KataloziSlike.YourFragmentInterface {
private PhotoViewAttacher mAttacher;
private ImageView imageView;
private Bundle args;
private Button button;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_katalozi_slike, container, false);
args = getArguments();
imageView = (ImageView) rootView.findViewById(R.id.imageViewAdapterZaListanjeKataloga);
Callback imageLoadedCallback = new Callback() {
@Override
public void onSuccess() {
if (mAttacher != null) {
mAttacher.update();
} else {
mAttacher = new PhotoViewAttacher(imageView);
}
}
@Override
public void onError() {
// TODO Auto-generated method stub
}
};
Picasso.with(getActivity()).load(args.getString("url")).placeholder(R.drawable.load).error(R.drawable.error).into(imageView, imageLoadedCallback);
mAttacher = new PhotoViewAttacher(imageView);
button = (Button) rootView.findViewById(R.id.zaProveru);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
return rootView;
}
@Override
public void onResume() {
super.onResume();
Toast.makeText(getActivity(), "sadsadasd", Toast.LENGTH_SHORT);
}
@Override
public void fragmentBecameVisible() {
mAttacher.update();
}
}
你的问题是,在制作第一个片段时,只有当你滑动(片段改变时)然后显示它时,它才会被设置。回到我的例子