简体   繁体   English

如何在Android中创建水平和垂直滚动的列表视图?

[英]How can I create a listview that scrolls both horizontally and vertically in Android?

I need a listview that scrolls in vertical and horizontal direction. 我需要一个在垂直和水平方向上滚动的列表视图。

<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:overScrollMode="always"
    android:scrollbarAlwaysDrawVerticalTrack="true">

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="true"
        android:scrollbars="vertical|horizontal">

    </ListView>
</HorizontalScrollView>

This is my code, but it's make only horizontally listview. 这是我的代码,但仅制作水平列表视图。

这张照片是我的列表视图,仅水平滚动。

First of all. 首先。 Don't use ListView. 不要使用ListView。 Instead use the RecyclerView with custom LayoutManager. 而是将RecyclerView与自定义LayoutManager一起使用。

As a solution try this. 作为解决方案,请尝试此。 Create a RecyclerView with both horizontal and vertical scrolling 创建水平和垂直滚动的RecyclerView

Create list view inside the horizontal scroll view, skeleton as follows 在水平滚动视图内创建列表视图,框架如下

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

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true">

        <ListView
            android:id="@+id/simpleListView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:divider="@color/material_blue_grey_800"
            android:dividerHeight="1dp" />

    </HorizontalScrollView>
</ScrollView>

Create item_list file: 创建item_list文件:

<?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="match_parent"
    android:padding="20dp"
    android:orientation="horizontal">
<ImageView
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:src="@drawable/ic_launcher_background"
    android:text="btn"/>
    <TextView
        android:id="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:padding="10dp"
        android:textColor="#000000" />
</LinearLayout>

Java code: Java代码:

public class MainActivity extends AppCompatActivity {

    ListView simpleList;
    String myList[] = {"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.","Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.","Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.","Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book."};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        simpleList =findViewById(R.id.simpleListView);
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.item_list, R.id.textView, myList);
        simpleList.setAdapter(arrayAdapter);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 android 工作室中垂直和水平填充图像数组 - How can I fill array of Images both vertically and horizontally in android studio 如何让我的布局水平和垂直滚动? - How can I make my layout scroll both horizontally and vertically? 如何在Android中水平和垂直滚动ViewPager? - How to scroll ViewPager both horizontally and vertically in Android? Android ListView如何垂直和水平滚动? - Android ListView how to scroll vertically and horizontally ? 水平和垂直滚动。 如何在代码视图中显示Java代码并水平和垂直滚动代码? - Scrolling Both horizontally and vertically . how can i show my java code in a code view and scroll my code both horizontally and vertically? 如何在Android中使对象水平或垂直移动? - How can I make an object to move horizontally or vertically in Android? 您如何使用weightSum垂直和水平缩放(我的行不通)Android - How can you use weightSum to both scale vertically and horizontally (mine doesn't work) android 如何创建列表视图,该列表视图仅在Android中水平滚动? - How can I create my listview which it's only scroll horizontally in Android? 如何将片段垂直和水平居中? - How can I center a fragment vertically and horizontally? Scrollview在android中水平和垂直滚动表格 - Scrollview to scroll table both horizontally and vertically in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM