Tôi gặp sự cố khi tạo lịch dựa trên GridView. Đây là Lưới:OnClickListener không hoạt động đối với mục đầu tiên trong GridView
Đây được coi là một cuốn lịch dân cư với các sự kiện, vì vậy tôi có bộ chuyển đổi của tôi thực hiện OnClickListener và tôi đặt người nghe rằng đối với mỗi nút trong lịch. Nó hoạt động hoàn hảo cho mỗi nút đơn EXCEPT cái đầu tiên (trong trường hợp này là số 30). Khi tôi nhấp vào nó chỉ không hoạt động, nhưng khi tôi nhấp vào nút khác sau khi tôi đã cố gắng nhấp vào nút đầu tiên, nó thực hiện nhấp chuột cho lần đầu tiên trước khi thực hiện nhấp chuột cho nút khác.
Tôi đã quét khoảng 10 trang câu hỏi có liên quan và không tìm thấy ai đó gặp sự cố này. Xin vui lòng giúp đỡ!
Như hỏi, đây là chức năng getView mã của tôi:
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
ViewHolder holder;
if (row == null)
{
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.calendar_day_gridcell, parent, false);
holder.gridCell = (Button) row.findViewById(R.id.calendar_day_gridcell);
holder.multiDayEvent = (EventLengthView)row.findViewById(R.id.eventLengthView);
}
else{
holder = (ViewHolder)row.getTag();
}
int calendarGridHeight = (calendarView.getHeight()-5)/(getCount()/7);
AbsListView.LayoutParams params = new AbsListView.LayoutParams(
android.view.ViewGroup.LayoutParams.FILL_PARENT,
calendarGridHeight);
row.setLayoutParams(params);
//Change the background drawable depending on the position in the calendar
if ((position+1) % 7 == 0){
holder.gridCell.setBackgroundDrawable(getResources().getDrawable(R.drawable.calendar_button_selector_end_row));
}
if (getCount() - position < 8){
holder.gridCell.setBackgroundDrawable(getResources().getDrawable(R.drawable.calendar_button_selector_end_column));
}
if (position == getCount()-1){
holder.gridCell.setBackgroundDrawable(getResources().getDrawable(R.drawable.calendar_button_selector_end));
}
holder.gridCell.setOnClickListener(this);
holder.gridCell.setTag(null);//clear tags
// ACCOUNT FOR SPACING
String[] day_color = list.get(position).split("-");
int theday = Integer.parseInt(day_color[0]);
int themonth = Integer.parseInt(day_color[2]);
int theyear = Integer.parseInt(day_color[3]);
String date = DateFormat.format("dd/M/yy", new Date(theyear,themonth,theday)).toString();
if ((!eventsMap.isEmpty()) && (eventsMap != null))
{
if (eventsMap.containsKey(date))
{
holder.multiDayEvent.SetMeasure(calendarView.getWidth()/7, calendarGridHeight);
holder.multiDayEvent.setVisibility(View.VISIBLE);
//holder.singleDayEvent.setVisibility(View.VISIBLE);
Event event = (Event) eventsMap.get(date);
holder.multiDayEvent.AddEvent(event);
holder.gridCell.setTag(event);
}
else{
//holder.singleDayEvent.setVisibility(View.GONE);
holder.multiDayEvent.setVisibility(View.GONE);
}
}
// Set the Day GridCell
holder.gridCell.setText(Integer.toString(theday));
if (day_color[1].equals("GREY"))
{
holder.gridCell.setTextColor(Color.GRAY);
}
if (day_color[1].equals("WHITE"))
{
holder.gridCell.setTextColor(Color.WHITE);
}
if (day_color[1].equals("BLUE"))
{
holder.gridCell.setTextColor(Color.BLUE);
}
row.setTag(holder);
return row;
}
public class ViewHolder{
Button gridCell;
ImageView singleDayEvent;
EventLengthView multiDayEvent;
}
public void onClick(View view)
{
if (view.getTag() != null){
Event event = (Event)view.getTag();
eventListView.setAdapter(new EventListAdapter(CalendarScreen.this, event));
eventListViewLayout.setVisibility(View.VISIBLE);
eventListViewLayout.startAnimation(fadeIn);
}
else if (eventListViewLayout.getVisibility() == View.VISIBLE){
onBackPressed();
}
}
Các onClick được gọi cho mỗi gridcell TRỪ người đầu tiên ở góc trên cùng bên trái
Vui lòng đặt mã bạn đã sử dụng .. –
[Tại sao bạn không thử ví dụ này] (http://w2davids.wordpress.com/android-simple-calendar/) – Praveenkumar
mã của tôi dựa trên ví dụ đó – Valentin