同一 Activity 中的多个视图
Multiple views in the same Activity
我正在开发一个类似 Tinder 的 Android 应用程序,我正在使用 this 库来实现刷卡功能。
对于滑动屏幕,我使用这种布局:
<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>
并在 activity 中对其充气:
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_swipe);
...
我现在正计划添加不同类型的卡片(具有不同的设计),但我面临着对同一 activity 使用多个视图的问题。
例如,一张卡片上会有一个人。另一张卡片可能有问题等等。在同一个 activity 中使用不同视图有什么聪明的方法吗?预期的功能是您会看到 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);
...
有什么方法可以根据某些 input/attribute 加载不同的视图吗?
干杯!
当然可以。
从适配器检查这两个方法
getViewTypeCount()
Returns the number of types of Views that will be created by
getView(int, View, ViewGroup).
getItemViewType(int position)
Get the type of View that will be created by getView(int, View,
ViewGroup) for the specified item.
覆盖 getViewTypeCount
并为您的适配器设置不同视图的数量。并在 getItemViewType
中检查您的项目,并 return 该项目的类型。
你可以这样使用它
// 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
}
}
如果您需要任何进一步的帮助/解释,请告诉我。
我正在开发一个类似 Tinder 的 Android 应用程序,我正在使用 this 库来实现刷卡功能。 对于滑动屏幕,我使用这种布局:
<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>
并在 activity 中对其充气:
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_swipe);
...
我现在正计划添加不同类型的卡片(具有不同的设计),但我面临着对同一 activity 使用多个视图的问题。 例如,一张卡片上会有一个人。另一张卡片可能有问题等等。在同一个 activity 中使用不同视图有什么聪明的方法吗?预期的功能是您会看到 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);
...
有什么方法可以根据某些 input/attribute 加载不同的视图吗?
干杯!
当然可以。
从适配器检查这两个方法
getViewTypeCount()
Returns the number of types of Views that will be created by getView(int, View, ViewGroup).
getItemViewType(int position)
Get the type of View that will be created by getView(int, View, ViewGroup) for the specified item.
覆盖 getViewTypeCount
并为您的适配器设置不同视图的数量。并在 getItemViewType
中检查您的项目,并 return 该项目的类型。
你可以这样使用它
// 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
}
}
如果您需要任何进一步的帮助/解释,请告诉我。