gpt4 book ai didi

android - ListView 或 2 个 ListView 中的 GridView 更好?

转载 作者:太空宇宙 更新时间:2023-11-03 13:54:18 24 4
gpt4 key购买 nike

我试图在我的 Activity 中使用带有 ListView 的 GridView ,但出现错误。

还有其他方法可以得到下面的结果吗?

在我的主要 Activity 中,我想要一个 ListView 显示大 ImageView ,第二个将它们显示为每行 2 个图像,我应该使用 GridView 还是第二个 ListView 就足够了?

enter image description here

这是我的代码

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fillViewport="true" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="sssss"
android:textSize="16sp"
android:padding="10dp"
android:background="#ffe474" />

<ListView
android:id="@+id/list3"
android:layout_width="wrap_content"
android:layout_height="1500dp"
android:background="#eeeeee" >
</ListView>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="Recommanded"
android:textSize="18sp"
android:padding="10dp"
android:background="#ffe474" />


<ListView
android:id="@+id/list1"
android:layout_width="wrap_content"
android:layout_height="1500dp"
android:background="#eeeeee" >
</ListView>

</LinearLayout>


</ScrollView>

最佳答案

更合适和直接的解决方案是将 RecyclerView 与 GridLayoutManager 一起使用。 GridLayoutManager 采用 SpanSizeLookup 对象,它允许您指定每个项目将占用多少跨度。

此问题的完整解决方案包括以下部分:

带有 RecyclerView、GridLayoutManager 和自定义 SpanSizeLookup 的 Activity

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
// specify that grid will consist of 2 columns
GridLayoutManager gridLayoutManager = new GridLayoutManager(getApplicationContext(), 2);
// provide our CustomSpanSizeLookup which determines how many spans each item in grid will occupy
gridLayoutManager.setSpanSizeLookup(new CustomSpanSizeLookup());
// provide our GridLayoutManager to the view
recyclerView.setLayoutManager(gridLayoutManager);
// this is fake list of images
List<Integer> imageResList = getMockedImageList();
// finally, provide adapter to the recycler view
Adapter adapter = new Adapter(imageResList);
recyclerView.setAdapter(adapter);
}


private List<Integer> getMockedImageList() {
// fake images list, you'd need to upload your own image resources
List<Integer> imageResList = new ArrayList<Integer>();

imageResList.add(R.drawable.img1);
imageResList.add(R.drawable.img2);
imageResList.add(R.drawable.img3);
imageResList.add(R.drawable.img4);
imageResList.add(R.drawable.img5);
imageResList.add(R.drawable.img6);

return imageResList;
}


private static class CustomSpanSizeLookup extends GridLayoutManager.SpanSizeLookup {
@Override
public int getSpanSize(int i) {
if(i == 0 || i == 1) {
// grid items on positions 0 and 1 will occupy 2 spans of the grid
return 2;
} else {
// the rest of the items will behave normally and occupy only 1 span
return 1;
}
}
}
}

RecyclerView 适配器

public class Adapter extends RecyclerView.Adapter {
// I assume that you will pass images as list of resources, but this can be easily switched to a list of URLS
private List<Integer> imageResList = new ArrayList<Integer>();

public Adapter(List<Integer> imageUrlList) {
this.imageResList = imageUrlList;
}


@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recycle_view_item, viewGroup, false);

return new ItemViewHolder(itemView);
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
ItemViewHolder itemViewHolder = (ItemViewHolder) viewHolder;
itemViewHolder.item.setImageResource(imageResList.get(i));
}

@Override
public int getItemCount() {
return imageResList.size();
}


private static class ItemViewHolder extends RecyclerView.ViewHolder {
private ImageView item;

public ItemViewHolder(View itemView) {
super(itemView);

this.item = (ImageView) itemView.findViewById(R.id.item_image);
}
}
}

activity_main.xml布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</RelativeLayout>

recycler_view_item.xml 布局

<?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="150dp"
android:padding="10dp">

<ImageView
android:id="@+id/item_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"/>

</LinearLayout>

最后一部分是确保在 RecyclerView 的 build.gradle 文件中添加依赖项:

dependencies {
compile 'com.android.support:recyclerview-v7:21.0.+'
}

结果如下:

enter image description here

这个解决方案的优点是:

  1. 它具有可扩展性、轻量级并且高度可定制
  2. 它很高效,因为它会在您上下滚动时回收 View
  3. 它不需要处理触摸事件,一旦您添加任何额外的触摸功能,触摸事件就很容易变得很难处理

希望对您有所帮助。

关于android - ListView 或 2 个 ListView 中的 GridView 更好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31739128/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com