尽管设置了属性

时间:2016-09-14 20:21:59

标签: java android xml android-adapter android-gridview

在滚动视图中创建网格视图后,视图本身显示为正常,但第一个值根本不显示。我试过看类似的问题,但我仍然不明白出了什么问题。需要改变什么才能解决这个问题?

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin">

    <TextView
        android:id="@+id/History_textView0"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="@android:color/white"
        android:text="Years"
        style="@android:style/TextAppearance.Medium" />
    <com.ages.events.GridViewHistory
        android:id="@+id/History_GridView0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:columnWidth="100dp"
        android:numColumns="auto_fit"
        android:layout_marginBottom="20dp"
        android:stretchMode="columnWidth"
        android:layout_below="@+id/History_textView0"/>

    <TextView
        android:id="@+id/History_textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="@android:color/white"
        android:text="Events"
        style="@android:style/TextAppearance.Medium"
        android:layout_below="@+id/History_GridView0" />
</RelativeLayout>

的java

public class TabFragmentHistory extends android.support.v4.app.Fragment {

    static final String[] years = new String[]{
        "1863", "1864",  "1868", "1871", "1890", "1898", "1900", "1906", "1968", "1979"        
    };

    GridView mGridView0;
    public MyAdapter adapter;

    public TabFragmentHistory() {
        // Required empty constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.tab_fragment_history, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        View v = getView();
        assert v != null;

        mGridView0 = (GridView) v.findViewById(R.id.HistoryGridView0);
        adapter = new MyAdapter(getActivity().getApplicationContext(), 0);
        mGridView0.setAdapter(adapter);
        for (int i = 1; i < years.length; i++) {
            adapter.addAdapterItem(new AdapterItem(years[i]));
        }

        super.onActivityCreated(savedInstanceState);
    }

    public class MyAdapter extends ArrayAdapter< AdapterItem > {
        private List< AdapterItem > items = new ArrayList< AdapterItem >();

        // private static LayoutInflater inflater = null;
        public MyAdapter(Context context, int textviewid) {
            super(context, textviewid);
        }

        public void addAdapterItem(AdapterItem item) {
            items.add(item);
        }

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

        @Override
        public AdapterItem getItem(int position) {
            return ((null != items) ? items.get(position) : null);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @SuppressWarnings("deprecation")
        @Override
        public View getView(final int position, View convertView,
                            final ViewGroup parent) {
            View rowView;
            if (convertView == null) {
                rowView = getActivity().getLayoutInflater().inflate(R.layout.gridview_item, parent, false);
            } else {
                rowView = convertView;
            }
            TextView tv = (TextView) rowView.findViewById(R.id.item_gridview);
            tv.setText(items.get(position).first);

            return rowView;
        }
    }

    public class AdapterItem {
        public String first;

        // add more items
        public AdapterItem(String first) {
            this.first = first;

        }
    }
}

1 个答案:

答案 0 :(得分:1)

for (int i = 1; i < years.length; i++) {
    adapter.addAdapterItem(new AdapterItem(years[i]));
}

这应该从i = 0开始吗?