Fragment is the CardView that can be automatically deployed on the carrier (the initial experience of an open-source project is written on GitHub), and cardviewgithub

Source: Internet
Author: User

Fragment is the CardView that can be automatically deployed on the carrier (the initial experience of an open-source project is written on GitHub), and cardviewgithub

Reprinted please indicate this article from the blog of the big glutinous rice (http://blog.csdn.net/a396901990). Thank you for your support!


Opening nonsense:


I have been watching the Material Desgin of Android5.0 the other day, and a new control is added in it --CardView. Google directly provided the CardView control this time and we can see that it has become very popular.

Previously, we can simulate the CardView effect by setting the rounded border, but now that Google has provided a new control, there is no reason not to use it. I previously wrote a Demo of automatic CardView Layout-ANDROID custom view-imitation waterfall layout (with source code) when learning custom layout)

Recently, I just learned about Git and want to try the effect of CardView in versions earlier than 5.0. So I modified it a little and wrote a Library Demo on Github. Before the introduction, let's take a look at the demo:





Introduction:


I have already said that the Demo in this article has been transformed from the Demo in the previous article. If you need more information, read the previous article first.

The following describes the functions of this Demo:


1. Use the new Google control CardView in Android5.0:

In my previous article about Android 5.0 Material Desgin, I introduced how to use CardView in Android 5.0. This example shows how to use CardView in Android 5.0.


2. Use Fragment as the carrier to display CardView:

Compared with the previous demo, The CardView in this example is not loaded with a simple view, but a Fragment, so we can put a series of logic in a CardView.


3. You can dynamically set the number of CardView displayed on the screen:

In many apps, we are used to using viewpager to slide the left and right views and switch fragment to display different contents, however, as the screen grows larger and more flat, multiple Fragment entries are displayed on one screen, which makes it more intuitive and makes more reasonable use of space.

For example, in the demonstration, the number of CardView displayed on each line is different between the vertical screen and the horizontal screen.


4. dynamically add and delete CardView

Compared with the previous demo, Fragment is used not only as the carrier of CardView, but also dynamically Delete and add CardView (the addition function does not have time to write, but the interface has been written ).


5. Open Source on GitHub as a Library

Like the open-source project on GitHub, I made this Demo open-source on GitHub as a Library. If you need it, you can customize the fragment (which must inherit from the CardFragment in the library package) add to CardManger in the library package, and use CardScrollView in the library package as the layout to achieve the above effect (details later)


Disadvantages:

1. The annotation in the project has not been added... Bad habits should be writing and adding...

2. When the child view touch event in CardView is deleted, it will be improved slowly...

3. No restoration or record processing is performed when you switch to the screen or exit the re-entry process...

4. There are too many shortcomings to address. Let's take a look. If you have any suggestions for improvement, leave a message. It would be better to raise the pull request directly on Github...




Usage:


1. clone the project or download and decompress it directly:

GitHub: https://github.com/a396901990/CardView_AutoLayout


2. Import the directory structure in eclipse as follows:


AutolayoutLibIs the encapsulated library file.

CardviewLibIs the cardview jar package of Google support-7 in Android 5.0.

MainActivityYes demo program

Their relationship is that cardviewLib is the library of autolayoutLib and autolayoutLib is the library of MainActivity.


3. Use:

Here we will briefly introduce how to use the encapsulated autolayoutLib in MainActivity:


1. First, make the custom Fragment inherit fromCardFragmentAnd some methods that must be called:

// First, the CardFragment class public class IDFragment extends {ImageView arrow; LinearLayout expandedView; boolean isShow = false; @ Override public View onCreateView (LayoutInflater inflater, ViewGroup container, bundle savedInstanceState) {return super. onCreateView (inflater, container, savedInstanceState) ;}@ SuppressLint ("InflateParams") @ Override public void onActivityCreated (Bu Ndle savedInstanceState) {// you need to set a touch listener for the rootview of the fragment. For details, see the CardFragment class getView () in the library package (). setOnTouchListener (this); super. onActivityCreated (savedInstanceState); // you need to call the setCardView () method in CardFragment to set the setCardView (getActivity () view of fragment (). getLayoutInflater (). inflate (R. layout. id_card, null, false); // if you need to set the title, you need to call the setTitle () method setTitle ("RESUME") in CardFragment; arrow = (ImageView) getView (). f IndViewById (R. id. arrow); expandedView = (LinearLayout) getView (). findViewById (R. id. expandedView); arrow. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View arg0) {expandedView. setVisibility (isShow? View. VISIBLE: View. GONE); arrow. setImageDrawable (isShow? GetResources (). getDrawable (android. R. drawable. arrow_up_float): getResources (). getDrawable (android. R. drawable. arrow_down_float); isShow =! IsShow ;}});}}

The main reason is that the content in the above four annotations must be implemented, and the rest will be written normally.


2. Add the custom CardFragment to the ActivityCardManagerMedium:

Public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); initCrads (); setContentView (R. layout. main_layout);} // stores the Fragment to be displayed in the CardManager class, and receives a List of CardFragment types (which must be called before setContentView) public void initCrads () {List <CardFragment> mCardFragments = new ArrayList <CardFragment> (); mCardFragments. add (new IDFragment (); mCardFragments. add (new CalcFragment (); mCardFragments. add (new PicFragment (); mCardFragments. add (new ClockFragment (); CardManager. getInstance (). setCardFragments (mCardFragments );}}


3. Use the layout file in the ActivityCardScrollView:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:auto="http://schemas.android.com/apk/res-auto"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <com.dean.autolayout.CardScrollView        android:id="@+id/myScrollView"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_margin="5dip"        auto:columns="1" >    </com.dean.autolayout.CardScrollView></LinearLayout>

The following describes how to use it.AutolayoutLib:



AutolayoutLib:


All the logic is encapsulated in this library. You can refer to the autolayoutLib usage steps described above to understand it as the cutting entry.


Recall the three steps for using autolayoutLib:

1. First, make the custom Fragment inherit CardFragment in autolayoutLib

2. Add the custom CardFragment to CardManager in the Activity.

3. The layout file in the Activity should use CardScrollView

So we can see that autolayoutLib is nothing more than the three classes.


Let's take a look at its directory structure:



I have already framed the red lines. The following describes their functions:


CardFragmentAdapter:

Inherited from FragmentPagerAdapter, which is used as the adapter of CardFragment.


CardScrollView:

Inherited from ScrollView. All views to be displayed are stored in this layout. The operations for adding and deleting CardFragment and adapter are included.


CardManager:

A model class that stores all CardFragment. I define it as the singleton mode so that you can save one copy globally.


AutoCardLayout:

With this custom laiyout, you can display the number of CardView s in each row based on the set column values. Do not display them in a waterfall layout. For more information, see the previous article.


CardFragment:

Defines the base class of Fragment and inherits from Fragment. Encapsulated some methods for processing CardFragment.


Cardview_container.xml:

The layout class of CardView, where you can set the CardView rounded corner size and background.




End:


First of all, forgive me for not introducing autolayoutLib in detail. The main reason is that I really don't have time to write it. It's three o'clock in the morning.


Recently, I was too busy. I started to learn Git systematically, installed the powerful artifact vim, And I had several demos in my mind to implement it. The most important thing is that world of warcraft 6.0 is about to be launched soon, And keyou is already in the rush...


If you are interested in this demo, you can download it and check it out, but it is impossible to directly use this lib in the project. The specific reason is mentioned in the above introduction. I am too lazy to complete it. After all, I write it mainly to practice git.

Some new functions may be extended along it in a few days.


To prevent people from reading the article, I will write the GitHub address here:

Https://github.com/a396901990/CardView_AutoLayout

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.