Giter Site home page Giter Site logo

dandanyouxiang / cardview Goto Github PK

View Code? Open in Web Editor NEW

This project forked from andreimisiukevich/cardview

0.0 1.0 0.0 2.14 MB

CardsView | CarouselView | CoverflowView | CubeView for Xamarin.Forms ๐Ÿ‡ง๐Ÿ‡พ

License: MIT License

C# 100.00%

cardview's Introduction

GIF

CardsView CarouselView CoverFlowView CubeView

CoverFlowView sample: https://github.com/AndreiMisiukevich/CardView/blob/master/PanCardViewSample/PanCardViewSample/Views/CoverFlowSampleXamlView.xaml

Setup

  • Available on NuGet: CardsView NuGet
  • Add nuget package to your Xamarin.Forms .NETSTANDARD/PCL project and to your platform-specific projects
  • Just in case add (AFTER Forms.Init(...)):
    • CardsViewRenderer.Preserve() for iOS AppDelegate in FinishedLaunching
    • CardsViewRenderer.Preserve() for Android MainActivity in OnCreate
Platform Version
Xamarin.iOS iOS 7+
Xamarin.Mac All
Xamarin.Android API 15+
Windows 10 UWP 10+
Tizen 4.0+
Gtk All
WPF .NET 4.5
.NET Standard 2.0+

Custom Animations

You are able to create custom animations, just implement ICardProcessor or extend created processors (change animation speed or type) https://github.com/AndreiMisiukevich/CardView/tree/master/PanCardView/Processors

Samples

The sample you can find here https://github.com/AndreiMisiukevich/CardView/tree/master/PanCardViewSample

C#:

-> Create CardsView and setup it

var cardsView = new CardsView
{
    ItemTemplate = new DataTemplate(() => new ContentView()) //your template
};
cardsView.SetBinding(CardsView.ItemsSourceProperty, nameof(PanCardSampleViewModel.Items));
cardsView.SetBinding(CardsView.SelectedIndexProperty, nameof(PanCardSampleViewModel.CurrentIndex));

-> Optionaly you can create ViewModel... or not... as you wish

-> Indicators bar (For CarouselView, perhaps). It's easy to add indicators -> Just add IndicatorsControl into your carouselView as a child view.

carouselView.Children.Add(new IndicatorsControl());

XAML:

<cards:CarouselView 
    ItemsSource="{Binding Items}"
    SelectedIndex="{Binding CurrentIndex}">
    <cards:CarouselView.ItemTemplate>
        <DataTemplate>
            <ContentView>
                <Frame 
                    VerticalOptions="Center"
                    HorizontalOptions="Center"
                    HeightRequest="300"
                    WidthRequest="300"
                    Padding="0" 
                    HasShadow="false"
                    IsClippedToBounds="true"
                    CornerRadius="10"
                    BackgroundColor="{Binding Color}">
                    
                    <Image Source="{Binding Source}"/> 
                    
                </Frame>
            </ContentView>
        </DataTemplate>
    </cards:CarouselView.ItemTemplate>

    <controls:LeftArrowControl/>
    <controls:RightArrowControl/>
    <controls:IndicatorsControl/>
</cards:CarouselView>

Also you are able to manage IndicatorsControl appearing/disappearing. For example if user doesn't select new page during N miliseconds, the indicators will disappear. Just set ToFadeDuration = 2000 (2 seconds delay before disappearing) Yoy manage LeftArrowControl and RightArrowControl as well as IndicatorsControl (ToFadeDuration is presented too).

Indicators styling:

 <ContentPage.Resources>
    <ResourceDictionary>
        <Style x:Key="ActiveIndicator" TargetType="Frame">
            <Setter Property="BackgroundColor" Value="Red" />
        </Style>
        <Style x:Key="InactiveIndicator" TargetType="Frame">
            <Setter Property="BackgroundColor" Value="Transparent" />
            <Setter Property="OutlineColor" Value="Red" />
        </Style>
    </ResourceDictionary>
</ContentPage.Resources>

... 

<controls:IndicatorsControl ToFadeDuration="1500"
          SelectedIndicatorStyle="{StaticResource ActiveIndicator}"
          UnselectedIndicatorStyle="{StaticResource InactiveIndicator}"/>

if you want to add items directly through xaml

...
    <cards:CarouselView.ItemsSource>
            <x:Array Type="{x:Type View}">
                <ContentView>
                    <Image Source="yourImage.png"/>
                </ContentView>
                <RelativeLayout>
                    <Button Text="Click" />
                </RelativeLayout>
                <StackLayout>
                    <Label Text="any text"/>
                </StackLayout>
            </x:Array>
    </cards:CarouselView.ItemsSource>
...

-> If you want to customize indicators, you need set SelectedIndicatorStyle and/or UnselectedIndicatorStyle, or you are able to extend this class and override several methods. Also you can customize position of indicators (You need to set Rotation / AbsoluteLayout Flags and Bounds etc.)

This class is describing default indicators styles (each default indicator item is Frame) https://github.com/AndreiMisiukevich/CardView/blob/master/PanCardView/Styles/DefaultIndicatorItemStyles.cs

Workarounds

-> If you want to put your cardsView/carouselView INTO scroll view, you should to use ParentScrollView instead of Xamarin.Forms.ScrollView. Set VerticalSwipeThresholdDistance rahter big value on Android.. otherwise sometimes fast scroll gestures can be interpritated as swipe.

-> If you want to put cardsView/carouselView INTO ListView or INTO any another scrollable view you should follow these steps

  1. Create your own class and implement IOrdinateHandlerParentView interface (It's needed only for iOS, but do it into shared project)
  2. Create the renderer for this class (For Android)

Check these classes (I implemented it for ParentScrollView. You can use it as example, nothing difficult :)) https://github.com/AndreiMisiukevich/CardView/blob/master/PanCardView/Controls/ParentScrollView.cs https://github.com/AndreiMisiukevich/CardView/blob/master/PanCardView.Droid/ParentScrollViewRenderer.cs

-> If you want to put your cardsView/carouselView INTO a TabbedPage on Android:

  1. Add an event handler for the UserInteraction event
  2. On UserInteractionStatus.Started: Disable TabbedPage Swipe Scrolling
  3. On UserInteractionStatus.Ending/Ended: Enabled TabbedPage Swipe Scrolling

Example:

  1. TabbedPage:
public partial class TabbedHomePage : Xamarin.Forms.TabbedPage
{
    public static TabbedHomePage Current { get; private set; }

    public TabbedHomePage()
    {
        Current = this;
    }

    public static void DisableSwipe()
    {
        Current.On<Android>().DisableSwipePaging();
    }
    
    public static void EnableSwipe()
    {
        Current.On<Android>().EnableSwipePaging();
    }
}
  1. Page with CardsView/CarouselView:
public PageWithCarouselView()
{
    InitializeComponent();

    carouselView.UserInteracted += CarouselView_UserInteracted;
}

private void CarouselView_UserInteracted(PanCardView.CardsView view, PanCardView.EventArgs.UserInteractedEventArgs args)
{
    if (args.Status == PanCardView.Enums.UserInteractionStatus.Started)
    {
        TabbedHomePage.DisableSwipe();
    }
    if (args.Status == PanCardView.Enums.UserInteractionStatus.Ended)
    {
        TabbedHomePage.EnableSwipe();
    }
}

-> If you don't want to handle vertical swipes or they interrupt your scrolling, you can set VerticalSwipeThresholdDistance = "2000" This property responds for vertical swipe detecting threshold

-> If all these tricks didn't help you, you may use IsPanInteractionEnabled = false This trick disables pan interaction, but preserve ability to swipe cards.

-> If you get crashes during ItemsSource update, try to add/set items in Main Thread (Device.BeginInvokeOnMainThread)

-> GTK use click / double click for forward/back navigation.

Check source code for more info, or ๐Ÿ‡ง๐Ÿ‡พ just ask me =) ๐Ÿ‡ง๐Ÿ‡พ

Full documentation

https://github.com/AndreiMisiukevich/CardView/tree/master/docs

License

The MIT License (MIT) see License file

Contribution

Feel free to create issues and PRs ๐Ÿ˜ƒ

cardview's People

Contributors

andreimisiukevich avatar b099l3 avatar baranpirincal avatar erdugen avatar ernestoyaquello avatar jtone123 avatar karlingen avatar logicsandbox avatar maukur avatar phenek avatar pomezny avatar tdoell avatar troyvnit avatar vecalion avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.