@Override public View getView(int position, View convertView, ViewGroup group) {
View view = convertView; ViewHolder holder; if (view == null) { view = _inflater.inflate(R.layout.list_item_layout, group, false); holder = new ViewHolder(view); view.setTag(holder); } else { holder = (ViewHolder) view.getTag(); }
final Dto data = getItem(position);
if (CommonUtil.isEmpty(data.getName())) { holder._name.setVisibility(View.GONE); } else { holder.name.setText(data.getName()); holder.name.setVisibility(View.VISIBLE); }
// Grid if (CommonUtil.isNotEmpty(data.getDataList())) { GridAdapter adapter = new GridAdapter(getContext(), data.getDataList()); holder._grid.setAdapter(adapter); holder._grid.setVisibility(View.VISIBLE); } else { holder._grid.setVisibility(View.GONE); } return view; }
static class ViewHolder { private final TextView name; private final TextView title; private final NonScrollableGridView _grid;
public ViewHolder(View view) { name = (TextView) view.findViewById(R.id.label_section_name); title = (TextView) view.findViewById(R.id.no_list_title); _grid = (NonScrollableGridView) view.findViewById(R.id.grid); } }<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/label_section_title" > <TextView android:id="@+id/label_section_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#EEEEEE" android:textColor="#000000" android:gravity="left" /> <TextView android:id="@+id/no_list_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#FFFFFF" android:textColor="#000000" android:visibility="gone" android:gravity="left" /> <!-- カスタムGridView --> <jp.co.test.NonScrollableGridView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#FFFFFF" android:id="@+id/grid" android:numColumns="2" android:layout_gravity="center_horizontal" /></LinearLayout> @Override public View getView(final int position, View convertView, ViewGroup group) { View view = convertView; ViewHolder holder; if (view == null) { view = _inflater.inflate(R.layout.collection, group, false); holder = new ViewHolder(view); view.setTag(holder); } else { holder = (ViewHolder) view.getTag(); }
Dto content = getItem(position);
if (CommonUtil.isNotEmpty(content.getNameLabel1())) { holder.name1.setText(content.getNameLabel1()); holder.name1.setVisibility(View.VISIBLE); } else { holder.name1.setVisibility(View.GONE); }
if (CommonUtil.isNotEmpty(content.getNameLabel2())) { holder.name2.setText(content.getNameLabel2()); holder.name2.setVisibility(View.VISIBLE); } else { holder.name2.setVisibility(View.GONE); } return view; }
/** * ビューをまとめたクラス */ static class ViewHolder { private final ImageView _img; private final TextView name1; private final TextView name2;
public ViewHolder(View view) { _img = (ImageView) view.findViewById(R.id.image); name1 = (TextView) view.findViewById(R.id.name1); name2 = (TextView) view.findViewById(R.id.name2); } }<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#FFFFFF"> <ImageView android:src="@drawable/close_button" android:id="@+id/image" android:layout_width="180dp" android:layout_height="180dp" android:layout_centerHorizontal="true" android:background="#FFFFFF"/> <TextView android:id="@+id/name1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#FFFFFF" android:textColor="#000000" android:gravity="left" android:layout_below="@+id/image" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <TextView android:id="@+id/name2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#FFFFFF" android:textColor="#000000" android:gravity="left" android:layout_below="@+id/name1" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /></RelativeLayout>public class NonScrollableGridView extends GridView { /** * コンストラクタ * * @param context context * @param attrs attrs */ public NonScrollableGridView(Context context, AttributeSet attrs) { super(context, attrs); }
/** * gridView のスクロールをしないよう、高さを中身の数に合わせて固定化する * * @param widthMeasureSpec 幅 * @param heightMeasureSpec 高さ */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int heightSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, heightSpec); getLayoutParams().height = getMeasuredHeight(); }}@Override
@SuppressWarnings("unchecked")
protected Integer doInBackground(Void... params) {
int count = _gridView.getChildCount();
if (count < _listMaxSize) {
_adapter.addList(_contentsArrayList, _listAddSize);
}
return count;
}
@Override
protected void onPostExecute(Integer count) {
_adapter.notifyDataSetChanged();
_gridView.invalidateViews();
if (_listMaxSize <= count + _listAddSize) {
_listView.removeFooterView(_footer);
} else {
_listView.setTag(R.id.listView, null);
}
}