Java 同一活动中的多个视图

Java 同一活动中的多个视图,java,android,android-activity,layout,swipe,Java,Android,Android Activity,Layout,Swipe,我正在开发一个类似Tinder的Android应用程序,并使用library实现刷卡功能。 对于滑动屏幕,我使用以下布局: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" style="@style/BackgroundStyle" android:

我正在开发一个类似Tinder的Android应用程序,并使用library实现刷卡功能。 对于滑动屏幕,我使用以下布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        style="@style/BackgroundStyle"
        android:padding="16dp">
        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
                <com.lorentzos.flingswipe.SwipeFlingAdapterView
                    xmlns:android="http://schemas.android.com/apk/res/android"
                    xmlns:app="http://schemas.android.com/apk/res-auto"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:rotation_degrees="16"
                    app:max_visible="2"
                    app:min_adapter_stack="1"
                    android:id="@+id/swipeView"/>
        </RelativeLayout>
        <fragment
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:id="@+id/tabmenu"/>

</RelativeLayout>
我现在正计划添加不同类型的卡(具有不同的设计),并且面临着为同一活动使用多个视图的问题。 例如,一张卡片上会有一个人。另一张卡可能有问题等。在同一活动中使用不同视图是否有明智的方法?预期的功能是,您看到4-5张卡片上有人,然后您看到一张卡片上有问题/反馈请求,然后您应该再次看到人,依此类推

刷卡库使用ArrayAdapter来保存表示卡的对象,卡的布局由getView()函数给出:

public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.cardlayout, parent, false);
        ...
有没有办法根据输入/属性加载不同的视图

干杯

当然有可能

从适配器检查这两种方法

  • getViewTypeCount()

    返回将由创建的视图类型数 getView(int、View、ViewGroup)

  • getItemViewType(int位置)

    获取将由getView(int,View, 视图组),用于指定的项目

覆盖
getViewTypeCount
并为适配器设置不同视图的数量。然后在
getItemViewType
中检查您的项目,并返回该项目的类型

你可以这样使用它

// Using two different layouts 
@Override
public int getViewTypeCount()
{
    return 2;
}

// Get the type of item  
@Override
public int getItemViewType(int position)
{
    if (arrayList.get(position).isUserType())
    {
        return ITEM_USER;
    }
    else
    {
        return ITEM_PHOTO;
    }
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    if (getItemViewType(position) == ITEM_USER)
    {
        // Inflate first layout
    }
    else
    {
        // Inflate second layout
    }
}

如果您需要任何进一步的帮助/解释,请告诉我。

谢谢男士:)这看起来是一个非常好的解决方案!我会马上开始做的欢迎您,如果您有任何问题,请告诉我!:)
// Using two different layouts 
@Override
public int getViewTypeCount()
{
    return 2;
}

// Get the type of item  
@Override
public int getItemViewType(int position)
{
    if (arrayList.get(position).isUserType())
    {
        return ITEM_USER;
    }
    else
    {
        return ITEM_PHOTO;
    }
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    if (getItemViewType(position) == ITEM_USER)
    {
        // Inflate first layout
    }
    else
    {
        // Inflate second layout
    }
}