Android视图寻呼机再次调用时不显示片段

时间:2016-04-04 12:13:15

标签: android listview android-fragments android-viewpager android-tablayout

我的应用程序中有一个滑块菜单,当我点击菜单中的一个项目时,它会显示相应的片段。那个碎片有一个tablayout和一个viewpager。 Fragment xml在下面给出

<android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:background="@color/colorPrimary"
        app:tabSelectedTextColor="@color/textColorPrimary"
        app:tabTextColor="@color/colorAccent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="fill"/>
    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_below="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

下面给出了滑块菜单上点击的片段的代码片段

public class MyCoursesFragment extends Fragment {
    private TabLayout tabLayout;
    private ViewPager viewPager;

    public MyCoursesFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());
        adapter.addFragment(new ScheduledFragment(), "SCHEDULED");
        adapter.addFragment(new LiveStreamingFragment(), "LIVE STREAMING");
        adapter.addFragment(new UpcomingFragment(), "UPCOMING");
        viewPager.setAdapter(adapter);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

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

        public void addFragment(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.d("mlearning", "inside my course");
        View rootView = inflater.inflate(R.layout.fragement_courses, container, false);
        viewPager = (ViewPager) rootView.findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        tabLayout = (TabLayout) rootView.findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);

        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }
}

上面的片段中有三个标签。每个片段的xml包含一个列表视图,下面给出了xml。

<ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lvList"
        android:paddingLeft="10dp"
        android:paddingTop="10dp"
        android:backgroundTint="#888686" />

tablayout片段的代码如下:

public class ScheduledFragment  extends Fragment {

public ScheduledFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    Log.d("mlearning", "inside scheduled");
    View rootView = inflater.inflate(R.layout.fragment_scheduled, container, false);
    ArrayList<CoursePojo> searchResults = GetSearchResults();

    final ListView lv1 = (ListView) rootView.findViewById(R.id.lvList2);
    lv1.setAdapter(new MyCustomCourseList(getActivity(), searchResults));

    lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> a, View v, int position, long id) {
            Object o = lv1.getItemAtPosition(position);
            CoursePojo fullObject = (CoursePojo) o;
            //Toast.makeText(getBaseContext(), "You have chosen: " + " " + fullObject.getId(), Toast.LENGTH_LONG).show();


        }
    });
    return rootView;

}
private ArrayList<CoursePojo> GetSearchResults(){
    ArrayList<CoursePojo> results = new ArrayList<CoursePojo>();CoursePojo coursePojo3=new CoursePojo();
    coursePojo3.setName("Android");
    coursePojo3.setDesc("First os was released without a phone ");
    coursePojo3.setStartdate("20 Mar");
    coursePojo3.setEndDate("8 Apr");
    coursePojo3.setProgress("40");
    results.add(coursePojo3);




    // Inflate the layout for this fragment
    return results;
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
}

@Override
public void onDetach() {
    super.onDetach();
}} 

最初,当我单击菜单片段时,选项卡片段中的值就像代码中提到的那样。但是,如果我再次点击菜单滑块,则不显示选项卡布局中的项目。还有一些选项卡为空或某些选项卡具有一些值。我哪里错了?

2 个答案:

答案 0 :(得分:2)

使用FragmentPagerAdapter的FragmentStatePagerAdapter。

答案 1 :(得分:0)

尝试在onResume()

中的List中填充数据