gpt4 book ai didi

android - ViewPager + Fragments - 未显示 fragment

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:47:54 26 4
gpt4 key购买 nike

我正在尝试使用 viewpager 来显示多个 fragment 。目前我只有一个 fragment ,但它似乎从未被实例化。我尝试使用 pager.setCurrentItem(0) 但从未调用适配器中的 getItem 方法。有谁知道为什么我的 fragment 没有加载到下面的代码中?

主要 Activity

public class MainActivity extends SherlockFragmentActivity {

MyPagerAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );

// Create the FragmentPager Adapter and assign it to the viewpager
adapter = new MyPagerAdapter( getSupportFragmentManager(), this );
ViewPager pager = ( ViewPager ) findViewById( R.id.pager );
pager.setAdapter( adapter );
pager.setCurrentItem( 0 );

// Setup the tab page indicator
TabPageIndicator indicator = ( TabPageIndicator ) findViewById( R.id.indicator );
indicator.setViewPager( pager );
}

class MyPagerAdapter extends FragmentStatePagerAdapter {
private SparseArray<Fragment> fragmentPageMap;

public MyPagerAdapter(FragmentManager fm, Activity context) {
super( fm );
fragmentPageMap = new SparseArray<Fragment>();
}

@Override
public Fragment getItem(int index) {
switch ( index ) {
case 0:
if ( fragmentPageMap.get( index ) != null ) {
SherlockListFragment scheduleFragment = ScheduleListFragment.newInstance();
fragmentPageMap.put( index, scheduleFragment );
return scheduleFragment;
} else {
return fragmentPageMap.get( index );
}
default:
return null;
}
}

/*@Override
public void destroyItem(View container, int index, Object object) {
super.destroyItem( container, index, object );
fragmentPageMap.remove( index );
}*/

@Override
public CharSequence getPageTitle(int index) {
return ( ( ViewPagerPage ) fragmentPageMap.get( index ) ).getTitle();
}

@Override
public int getCount() {
return fragmentPageMap.size();
}
}

public interface ViewPagerPage {
String getTitle();
}

主要 Activity 布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<com.viewpagerindicator.TabPageIndicator
android:id="@+id/indicator"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold" />

<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />

</LinearLayout>

调度列表 fragment

public class ScheduleListFragment extends SherlockListFragment implements ViewPagerPage {

private ScheduleAdapter adapter;
private final String TITLE = "Schedule";

public static ScheduleListFragment newInstance() {
ScheduleListFragment newFrag = new ScheduleListFragment();
Bundle args = new Bundle();
newFrag.setArguments( args );
return newFrag;
}

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate( R.layout.fragment_schedule_list, null );
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate( R.menu.fragment_schedule_list, menu );
}

// Container Activity must implement this interface
public interface OnAddItemClickedListener {
public void onAddItemClicked();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch ( item.getItemId() ) {
case R.id.add_item:
Intent myIntent = new Intent( getActivity(), AddScheduleItemActivity.class );
startActivityForResult( myIntent, 1 );

return true;
default:
return super.onOptionsItemSelected( item );
}
}

public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated( savedInstanceState );
setHasOptionsMenu( true );

adapter = new ScheduleAdapter( getActivity() );

setListAdapter( adapter );
}

private class SampleItem {
public String tag;
public int iconRes;

public SampleItem(String tag, int iconRes) {
this.tag = tag;
this.iconRes = iconRes;
}
}

public class ScheduleAdapter extends ArrayAdapter<SampleItem> {

public ScheduleAdapter(Context context) {
super( context, 0 );
}

public View getView(int position, View convertView, ViewGroup parent) {
if ( convertView == null ) {
convertView = LayoutInflater.from( getContext() ).inflate( R.layout.results_list_row, null );
}
ImageView icon = ( ImageView ) convertView.findViewById( R.id.row_icon );
icon.setImageResource( getItem( position ).iconRes );
TextView title = ( TextView ) convertView.findViewById( R.id.row_title );
title.setText( getItem( position ).tag );

return convertView;
}

}

@Override
public String getTitle() {
return TITLE;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if ( resultCode == android.app.Activity.RESULT_OK ) {
String name = ( String ) data.getExtras().get( "TEXT" );
Toast.makeText( getActivity(), name, Toast.LENGTH_LONG ).show();
}
}

fragment 布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />

</LinearLayout>

谢谢,弥敦道

当前的解决方案:

更改适配器构造函数以初始化第一个 fragment 并将其添加到内部映射。有没有人认为这样做有什么问题,或者有没有改进上面发布的任何代码的方法?谢谢!

public MyPagerAdapter(FragmentManager fm, Activity context) {
super( fm );
fragmentPageMap = new SparseArray<Fragment>();
SherlockListFragment scheduleFragment = ScheduleListFragment.newInstance();
fragmentPageMap.put( 0, scheduleFragment );
}

最佳答案

getCount()MyPagerAdapter 中返回 0,因此不会创建 Fragment,如果需要 N,它应该返回 N 要实例化的 fragment 。

fragmentPageMap 在构造函数中创建,当调用 getCount() 并且在 getItem 之前调用 getCount() 时为空(index),这就是 getCount() 返回 0

的原因

在这种情况下,您不需要开关,因为您每次都在实例化相同的 fragment 。但如果您在实例化不同的 fragment 时需要它。

@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new Fragment1();
case 1:
return new Fragment2();
}
return null;
}

关于android - ViewPager + Fragments - 未显示 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14699022/

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