每个gridView单元格内的ViewPager

时间:2014-09-18 13:26:11

标签: android gridview android-viewpager adapter

我正在尝试使用一些时间表来实现某种日历。

我创建了一个GridView,因为同一天我可能会有多次将ViewPager放在每个单元格中。我创建了所有的适配器,但是有一个奇怪的问题:寻呼机的内容只在gridview的第一个单元格中可见。

如果我在单元格中放置一个简单的TextView,那么所有内容都会在第1,第3和第5单元格中正确显示。

以下是我的文件:

grid_row.xml :单个gridview单元格的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="@drawable/square"
android:orientation="vertical" >

<TextView
    android:id="@+id/row_day"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="sans-serif-bold"
    android:textColor="#cfd8dc"
    android:textSize="9sp" />

<android.support.v4.view.ViewPager
    android:id="@+id/row_orari_pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    </android.support.v4.view.ViewPager>
</LinearLayout>

WhereFragment.java

[...]
public class GridViewCustomAdapter extends BaseAdapter {
    ArrayList<Day> officeDay;

    public GridViewCustomAdapter(Day[] days) {
        officeDay = new ArrayList<Day>(Arrays.asList(days));
    }

    public int getCount() {
        return 7;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;

        if (row == null) {
            LayoutInflater inflater = getActivity().getLayoutInflater();
            row = inflater.inflate(R.layout.grid_row, parent, false);
            TextView day = (TextView) row.findViewById(R.id.row_day);
            ViewPager mPager = (ViewPager) row.findViewById(R.id.row_orari_pager);

            day.setText(days[position]);
            OrariPagerAdapter mPagerAdapter = new OrariPagerAdapter(getChildFragmentManager());
            mPager.setAdapter(mPagerAdapter);
            }
        return row;
    }

    public class OrariPagerAdapter extends FragmentStatePagerAdapter {
        public ArrayList<Time> currentOrari;
        public OrariPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public OrariObjectFragment getItem(int i) {
            OrariObjectFragment fragment = new OrariObjectFragment(currentOrari);
            Bundle args = new Bundle();
            args.putInt(OrariObjectFragment.ARG_OBJECT, i);
            fragment.setArguments(args);
            return fragment;
        }

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

        @Override
        public CharSequence getPageTitle(int position) {
            return "";
        }
    }

    @SuppressLint("ValidFragment")
    public class OrariObjectFragment extends Fragment {
        public static final String ARG_OBJECT = "object";
        public ArrayList<Time> orari;

        public OrariObjectFragment(ArrayList<Time> currentOrari) {
            orari = currentOrari;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.orari_layout, container, false);
            final int position = getArguments().getInt(ARG_OBJECT);

            TextView orario = (TextView) rootView.findViewById(R.id.row_orario);
            orario.setTextColor(Color.parseColor("#ff0000"));

            return rootView;
        }
    }
}

orari_layout.xml

<?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">

<TextView
    android:id="@+id/row_orario"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="sans-serif-light"
    android:textColor="#607d8b"
    android:textSize="15sp"
    android:text="aoisjsdasdas"/>

</LinearLayout>

这是结果,因此只填充第一个单元格,而不填充其他单元格。

gridview

有什么建议吗?谢谢大家

2 个答案:

答案 0 :(得分:0)

 if (row == null) {
            LayoutInflater inflater = getActivity().getLayoutInflater();
            row = inflater.inflate(R.layout.grid_row, parent, false);
            TextView day = (TextView) row.findViewById(R.id.row_day);
            ViewPager mPager = (ViewPager) row.findViewById(R.id.row_orari_pager);


            }

 day.setText(days[position]);
            OrariPagerAdapter mPagerAdapter = new OrariPagerAdapter(getChildFragmentManager());
            mPager.setAdapter(mPagerAdapter);

答案 1 :(得分:0)

if (row == null) {

当第一个单元格之后的所有时间中,行不为空时。

    if (row == null) {
       LayoutInflater inflater = getActivity().getLayoutInflater();
       row = inflater.inflate(R.layout.grid_row, parent, false);

    }
    TextView day = (TextView) row.findViewById(R.id.row_day);
    ViewPager mPager = (ViewPager) row.findViewById(R.id.row_orari_pager);
    day.setText(days[position]);
    OrariPagerAdapter mPagerAdapter = new OrariPagerAdapter(getChildFragmentManager());
    mPager.setAdapter(mPagerAdapter);