I'm developing a Tinder-like Android application and I am using this library to implement the card swiping functionality. For the swipe screen, I use this layout:
<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>
and inflating it in the activity:
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_swipe);
...
I am now planning on adding different types of cards (that have different designs) and am facing the problem of using multiple views for the same activity. For instance, one card will have a person on it. Another card might have a question, etc. Is there any smart way of using different views in the same activity? The intended functionality is that you see 4-5 cards with people on them, then you see a card with a question / feedback request, then you should see people again, and so on.
The Swipe library uses an ArrayAdapter to keep the objects that represent cards, and the layout of the card is given by the getView() function:
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);
...
Is there any way to make this load different views according to some input/attribute?
Cheers!
Sure it is possible.
Check this two methods from adapter
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.
Override getViewTypeCount
and set the number of different views for your adapter. And in getItemViewType
check your item, and return a type for that item.
You could use it like this
// 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
}
}
If you need any further help / explanation, let me know.