How to inflate another layout inside getView() of gridview adapter in android?

感情迁移 提交于 2019-12-09 17:07:27

问题


I want to create weekly calendar view and inside each grid item (each day) there are may be several activities.Out of this I have created weekly calendar view using grid view but I want to add activities if there are any for particular date by dynamically checking db. Like same as in image. Below is my getView() code..
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.calendar_week_gridcell, parent, false);
    }

    txtRowTitle = (TextView) row.findViewById(R.id.txtDayTitle);

    LinearLayout root = (LinearLayout) row.findViewById(R.id.linear_root);

    String dayTitle = list.get(position);
    txtRowTitle.setText(dayTitle);
    if (position == currentWeekDay - 1)
        root.setBackgroundResource(R.drawable.calheader);
    if (!activityMap.isEmpty() && activityMap.containsKey(activityDateList.get(position))) {
        TextView item = new TextView(mContext);
        item.setText(activityMap.get(activityDateList.get(position)));
        item.setBackgroundColor(Color.GREEN);
        root.addView(item);
    }

    return row;
}

}

Here I am temporarily trying to add text view dynamically but I want to inflate here my custom activity_item layout and add it to grid cell.


回答1:


do you mean that you wish to add multiple rows in the area that is now being taken by the "Busy" space ? do you want it to take as much space as it needs, or should it be scrollable?

anyway, it seems you are in the right direction. you used a linearLayout to hold the "busy" area, right? so just use a for-loop and put their the data you wish to show.

of course, because the gridView recycles items, you would need to empty the linearLayout every time you reach the getView, but it should still work.

if you wish to use the layoutInflater in the for-loop, that's also possible:

 subRow = inflater.inflate(R.layout.sub_row, linearLayout, true);


来源:https://stackoverflow.com/questions/17629542/how-to-inflate-another-layout-inside-getview-of-gridview-adapter-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!