我正在开发一个类似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).
getItemViewType(int position)
获取将由getView(int, View, 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
}
}
如果您需要进一步的帮助/解释,请告诉我。