簡體   English   中英

同一活動中有多個視圖

[英]Multiple views in the same Activity

我正在開發類似Tinder的Android應用程序,並且正在使用庫來實現刷卡功能。 對於滑動屏幕,我使用以下布局:

<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>

並在活動中對其進行誇大:

protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_swipe);
    ...

我現在正在計划添加不同類型的卡(具有不同的設計),並且面臨着對同一活動使用多個視圖的問題。 例如,一張卡上會有一個人。 另一張卡片可能有問題,等等。在同一活動中是否有任何明智的方式使用不同的視圖? 預期的功能是,您看到4-5張卡片上有人,然后看到卡片上有問題/反饋請求,然后您應該再見到別人,依此類推。

Swipe庫使用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)創建的View的類型數。

  • getItemViewType(int position)

    獲取將由getView(int,View,ViewGroup)為指定項目創建的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
    }
}

如果您需要其他幫助/解釋,請告訴我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM