レイアウトを動的に追加するコードを書いたのですが、うまく行かず表示されません。
以下のthree_grid_row.xml
にgrid_item.xml
を3つ水平に並べたものを複数作成し、ルートビューであるfragment_video_info.xml
のLinearLayoutであるsuggestion_video_list
に追加します。
実行してみるとエラーは出ないのですがsuggest_video_list
が表示されません。
fragment_video_info.xml
の一部:
<LinearLayout
android:id="@+id/suggestion_video_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
three_grid_row.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
grid_item.xml
:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv_thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/suggest_duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="65dp"
android:layout_marginRight="4dp"
android:background="@drawable/border"
android:textColor="#FFFFFF"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/suggest_video_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:textAppearance="?android:attr/textAppearanceMediumInverse"/>
<TextView
android:id="@+id/suggest_ch_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/suggest_video_name"
android:textAppearance="?android:attr/textAppearanceSmall"/>
<TextView
android:id="@+id/suggest_video_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/suggest_ch_name"
android:textAppearance="?android:attr/textAppearanceSmall"/>
</RelativeLayout>
</LinearLayout>
</FrameLayout>
処理部:
if (!relatedVideoList.isEmpty()) {
//layoutInflaterを取得
LayoutInflater inflater = (LayoutInflater) getActivity().getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//LinearLayoutを入れる配列を作る
LinearLayout[] gridRowList = new LinearLayout[7];
for (int i = 0; i < 3; i++) {
gridRowList[i] = (LinearLayout) inflater.inflate(R.layout.three_grid_row, null);
}
for (int i = 3; i < 7; i++) {
gridRowList[i] = (LinearLayout) inflater.inflate(R.layout.three_grid_row, null);
gridRowList[i].setVisibility(View.GONE);
}
//LinearLayoutに追加
int count = 0;
for (VideoItems item : relatedVideoList) {
FrameLayout gridItem = null;
if (count < 3) {
gridItem = (FrameLayout) inflater.inflate(R.layout.grid_item, gridRowList[0], false);
} else if (count >= 3 && count < 6) {
gridItem = (FrameLayout) inflater.inflate(R.layout.grid_item, gridRowList[1], false);
} else if (count >= 6 && count < 9) {
gridItem = (FrameLayout) inflater.inflate(R.layout.grid_item, gridRowList[2], false);
} else if (count >= 9 && count < 12) {
gridItem = (FrameLayout) inflater.inflate(R.layout.grid_item, gridRowList[3], false);
} else if (count >= 12 && count < 15) {
gridItem = (FrameLayout) inflater.inflate(R.layout.grid_item, gridRowList[4], false);
} else if (count >= 15 && count < 18) {
gridItem = (FrameLayout) inflater.inflate(R.layout.grid_item, gridRowList[5], false);
} else if (count >= 18 && count < 21) {
gridItem = (FrameLayout) inflater.inflate(R.layout.grid_item, gridRowList[6], false);
}
//Viewをセット
if (gridItem != null) {
SuggestViewHolder holder = new SuggestViewHolder();
setGridItemView(gridItem, holder);
setGridItemContents(holder, item);
}
count++;
}
//LinearLayoutにViewをセット
LinearLayout suggestVideoContainer = (LinearLayout) view.findViewById(R.id.suggestion_video_list);
for (LinearLayout item : gridRowList) {
suggestVideoContainer.addView(item);
}
//関連動画を出すボタンの有無をセットする
if (gridRowList.length < 4 ) {
LinearLayout suggestButton = (LinearLayout)view.findViewById(R.id.suggest_exp_button);
suggestButton.setVisibility(View.GONE);
}
}
setGridItemView
:
private void setGridItemView(FrameLayout gridItem, SuggestViewHolder holder){
holder.suggestThumbnail = (ImageView)gridItem.findViewById(R.id.iv_thumbnail);
holder.suggestDuration = (TextView)gridItem.findViewById(R.id.suggest_duration);
holder.suggestVideoName = (TextView)gridItem.findViewById(R.id.suggest_video_name);
holder.suggestChannelName = (TextView)gridItem.findViewById(R.id.suggest_ch_name);
holder.suggestViewCount = (TextView)gridItem.findViewById(R.id.suggest_video_view);
}
setGridItemContents
:
private void setGridItemContents(SuggestViewHolder holder, VideoItems videoItems){
Picasso.with(getActivity()).load(videoItems.snippet.thumbnails.default1.url).into(holder.suggestThumbnail);
holder.suggestChannelName.setText(videoItems.snippet.channelTitle);
holder.suggestDuration.setText(durationConverter(videoItems.contentDetails.duration));
holder.suggestVideoName.setText(videoItems.snippet.title);
holder.suggestViewCount.setText(numberConverter(videoItems.statistics.viewCount, 0) + " " + getString(R.string.views));
}
なにか、間違えているところがありましたら教えて下さい。よろしくお願いします。