我想使用HorizontalScrollView实现Gallery。但它会抛出java.lang.illegalStateException。 logcat暗示HorizontalScrollView只能托管一个直接子节点。我只设置了一个直接子项 - LinearLayout。我从网上搜索过,但没有解决问题。希望有人能帮助我。 Belows是代码。
XML
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:scrollbars="none"
>
<LinearLayout
android:id="@+id/gallery_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
item xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="10dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingBottom="20dp"
>
<ImageView
android:id="@+id/trend_item_imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="8dp"
android:src="@drawable/gallery0"
/>
<TextView
android:id="@+id/trend_item_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="权利的游戏"
android:textSize="16sp"
/>
</LinearLayout>
片段
public class DiscoverFragment extends Fragment {
private int[] imageResources = {R.drawable.gallery0, R.drawable.gallery1, R.drawable.gallery2,
R.drawable.gallery3, R.drawable.gallery4, R.drawable.gallery5};
private String[] imageDescriptions = {"权利的游戏", "风吹的风景", "插画背景", "美食君", "吃", "你好四月"};
private List<TrendItem> trendItemList;
private LinearLayout galleryLayout;
public DiscoverFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_discover, container, false);
ButterKnife.bind(this, view);
galleryLayout = ButterKnife.findById(view, R.id.gallery_layout);
initData();
setView();
return view;
}
private void initData(){
trendItemList = new ArrayList<>();
for (int i = 0; i < imageResources.length; i++){
TrendItem trendItem = new TrendItem();
trendItem.setImageResource(imageResources[i]);
trendItem.setText(imageDescriptions[i]);
trendItemList.add(trendItem);
}
}
private void setView(){
for (int i = 0; i < trendItemList.size(); i++){
LayoutInflater inflater = LayoutInflater.from(getContext());
View view = inflater.inflate(R.layout.trend_item, galleryLayout, false);
ImageView trendItemImageView = ButterKnife.findById(view, R.id.trend_item_imageview);
TextView trendItemTextView = ButterKnife.findById(view, R.id.trend_item_textview);
trendItemImageView.setImageResource(trendItemList.get(i).getImageResource());
trendItemTextView.setText(trendItemList.get(i).getText());
galleryLayout.addView(view);
}
}
}
答案 0 :(得分:0)
意思是,您必须将ImageView
,TextView
添加到Linearlayout
。当您添加imageview时,您将其添加到HorizontalScrollview
中,其中还有一个LinearLayout
,通过向HorizontalScrollView添加2个子元素,您无法做到这一点。
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:scrollbars="none">
<LinearLayout
android:id="@+id/gallery_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/trend_item_imageview"
android:layout_width="your_size"
android:layout_height="your_size"
android:src="@drawable/gallery0"/>
<TextView
android:id="@+id/trend_item_textview"
android:layout_width="your_size"
android:layout_height="your_size"
android:text="权利的游戏"
android:textSize="16sp"/>
</LinearLayout>
</HorizontalScrollView>