如何在Android中添加水平滚动视图和列表视图

时间:2015-05-26 03:56:50

标签: android android-listview

我正在尝试创建一个应用程序,我需要创建一个列表视图,但最重要的是我想要一个水平列表视图来处理多个数据。我很困惑,我们怎么能这样做。请帮助我这个

MainActivityXML

<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=".HomePage">

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:id="@+id/mydata"
            android:orientation="vertical">
        </LinearLayout>

</ScrollView>

mydata我想添加动态xml布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <LinearLayout
        android:id="@+id/mainLinearLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:orientation="horizontal" >
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher"
            android:layout_marginTop="5dp"
            android:layout_marginLeft="10dp"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="10dp"
            android:text="ABC DEF JKL"
            android:textColor="@android:color/black" />
    </LinearLayout>
</RelativeLayout>

main_activity.xml我想添加水平列表视图,但我无法做到这一点。

Horizo​​ntialListView

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="#fff"
  >

  <com.test.ui.HorizontialListView
    android:id="@+id/listview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#ddd"
  />

</LinearLayout>

这是我的horizontalitems

 <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#fff"
    >

    <ImageView
        android:id="@+id/image"
        android:layout_width="150dip"
        android:layout_height="150dip"
        android:scaleType="centerCrop"
        android:src="@mipmap/ic_launcher"
        />

    <TextView
        android:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#000"
        android:gravity="center_horizontal"
        />

</LinearLayout>

我想创造这样的东西 I want to create like this

我正面临的问题是如何连接它们。请在此指导我

4 个答案:

答案 0 :(得分:1)

您需要的是RecyclerView,您可以

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/recyclerView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/white"
                    android:clipToPadding="false"/>

然后在代码中,您可以像这样设置水平方向。

mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
mRecyclerView.setLayoutManager(mLayoutManager);

您可以使用RecyclerView.Adapter为其添加视图。全部设定:))

答案 1 :(得分:0)

从你给出的图片来看,我认为不需要滚动视图,你正在寻找这样的东西。

<LinearLayout 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:orientation="vertical">

    <com.test.ui.HorizontialListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>


    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">

        // add the layout for filter and sort by here

    </LinearLayout> 

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1">
    </ListView>

</LinearLayout>

答案 2 :(得分:0)

对于水平滚动部分,使用 Horizo​​ntalScrollView 包围的 LinearLayout ,然后以编程方式添加每个内部LinearLayout。

以下部分使用自定义ListView。

我想那可以胜任。

答案 3 :(得分:0)

可能会帮助你

 <HorizontalScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

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

// You can use your own code over here

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@mipmap/ic_launcher" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@mipmap/ic_launcher" />

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@mipmap/ic_launcher" />

        <ImageView
            android:id="@+id/imageView4"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@mipmap/ic_launcher" />

        <ImageView
            android:id="@+id/imageView5"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@mipmap/ic_launcher" />

        <ImageView
            android:id="@+id/imageView6"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@mipmap/ic_launcher" />

    </LinearLayout>


</HorizontalScrollView>
相关问题