我使用了scrollview,因为我的视图很长,我需要让用户向下滚动以查看TextViews和ImageViews等项目。但是在底部我需要一个GridView来显示无限的图像列表。
我需要能够滚动查看文字视图
和
我还需要能够访问GridView并滚动
然而,我意识到你不能在Scrollview中有一个GridView,因为它们都涉及滚动和一些奇怪的错误阻止我滚动GridView。我的情况有什么替代方案?
代码示例:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="30dp"
android:text="Hello there"
android:textColor="#CC000000"
android:textSize="25sp" />
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="30dp"
android:text="Partner Name: "
android:textColor="#CC000000"
android:textSize="25sp" />
<!-- Lot more TextViews -->
<!-- And then comes the GridView -->
<GridView
android:id="@+id/picturefeed"
android:numColumns="3"
android:layout_marginTop="20dp"
android:layout_below="@+id/partner_details"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</GridView>
</ScrollView>
答案 0 :(得分:1)
您需要具有GridLayoutManager的异质RecyclerView。
您应该查看Creating Lists and Cards指南。
以下是一个示例:
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_main_layout);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.your_recycler_view);
final MyAdapter adapter = new MyAdapter();
GridLayoutManager layoutManager = new GridLayoutManager(this, 3); // a row can be 3 spans wide
layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
switch(adapter.getItemViewType(position)){
case MyAdapter.TYPE_TEXT:
return 3; // it's going to take up a row
case MyAdapter.TYPE_IMAGE:
return 1; // it's going to take up 1/3 of a row
default:
return -1;
}
}
});
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
}
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public static final int TYPE_TEXT = 1;
public static final int TYPE_IMAGE = 2;
@Override
public int getItemViewType (int position) {
if (position < 2) { // the first two items of the RecyclerView will be the TextView ones
// you can (should?) use a more sophisticated method if you want to
return TYPE_TEXT;
}
// the rest of the items are images
return TYPE_IMAGE;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder;
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
switch (viewType) {
case TYPE_TEXT:
View v1 = inflater.inflate(R.layout.layout_vh_text, parent, false);
viewHolder = new TextViewHolder(v1);
break;
case TYPE_IMAGE:
View v2 = inflater.inflate(R.layout.layout_vh_image, parent, false);
viewHolder = new ImageViewHolder(v2);
break;
default:
throw new IllegalArgumentException("Unsupported view type: " + viewType);
}
return viewHolder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
switch (viewHolder.getItemViewType()) {
case TYPE_TEXT:
TextViewHolder vh1 = (TextViewHolder) viewHolder;
// do something with vh1
break;
case TYPE_IMAGE:
ImageViewHolder vh2 = (ImageViewHolder) viewHolder;
// do something with vh2
break;
default:
throw new IllegalArgumentException("Unsupported view type: " + viewType);
}
}
public class TextViewHolder extends RecyclerView.ViewHolder {
// ...
}
public class ImageViewHolder extends RecyclerView.ViewHolder {
// ...
}
// rest of the adapter's code, not explained here as it's not part of the question
}