我的gridview有问题。我有7列gridview,但当我点击一行上的任何列时,我得到一行中第一项的索引。
例如,当我点击第二行的第三列时,获取索引7,但应该是10.
我为此扩展了BaseAdapter。
CalendarAdapter adapter = new CalendarAdapter(getActivity(), dateFormat.format(month.getTime()), issues, height, getDays(month));
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(calendarItemClickListener);
...
private AdapterView.OnItemClickListener calendarItemClickListener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Log.d(TAG, "onItemClick: " + i); // here is bad index
}
};
适配器:
public CalendarAdapter(Activity c, String monthCalendar, List<Issue> items, float height, List<Calendar> dates) {
this.dates = dates;
}
@Override
public int getCount() {
return dates.size();
}
@Override
public Object getItem(int position) {
Log.v(TAG, "Position: " + position + ", " + dateFormat.format(dates.get(position).getTime()));
return hashMap.get(dateFormat.format(dates.get(position).getTime()));
}
@Override
public boolean isEnabled(int position) {
return dateFormat.format(dates.get(position).getTime()).contains(month);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.item_calendar, null);
AbsListView.LayoutParams param = new AbsListView.LayoutParams(
(int) (height / ROW_NUMBER),
(int) (height / ROW_NUMBER));
convertView.setLayoutParams(param);
holder = new ViewHolder();
holder.dayView = (TextView) convertView.findViewById(R.id.date);
holder.downloadViewButton = (TextView) convertView.findViewById(R.id.downloadViewButton);
holder.previewImageView = (ImageView) convertView.findViewById(R.id.preview);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
...
XML
<GridView
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="7"
android:verticalSpacing="2dp"
android:horizontalSpacing="2dp"
android:stretchMode="columnWidth"
android:gravity="center" />
知道为什么?
提前致谢