Android ListView → Android Custom ListView with CardViews – LongClick – Camposha

Android ListView

Android ListView

Android ListView

Android Custom ListView with CardViews – LongClick


This is a custom listview with images and text tutorial. We see how to handle the OnLongClickListeners and show a toast message.

Gradle Scripts

We’ll add some dependencies from the support library to our app level build.gadle:

Build.gradle

    apply plugin: 'com.android.application'

    android {
        compileSdkVersion 23
        buildToolsVersion "23.0.2"

        defaultConfig {
            applicationId "com.tutorials.hp.listviewlongclick"
            minSdkVersion 15
            targetSdkVersion 23
            versionCode 1
            versionName "1.0"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        testCompile 'junit:junit:4.12'
        compile 'com.android.support:appcompat-v7:23.2.1'
        compile 'com.android.support:design:23.2.1'
        compile 'com.android.support:cardview-v7:23.2.1'
    }

LAYOUTS

We’ll have three layouts.

1. activity_main.xml

  • This is the layout that will get inflated into the MainActivity’s user interface.
  • It’s root tag is CordinatorLayout
  • First it will include the content_main.xml which will host our listview.
  • This layout defines the appbar, toolbar and floating action button codes.
    <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout 
        
        
        android_layout_width="match_parent"
        android_layout_height="match_parent"
        android_fitsSystemWindows="true"
        tools_context="com.tutorials.hp.listviewlongclick.MainActivity">

        <android.support.design.widget.AppBarLayout
            android_layout_width="match_parent"
            android_layout_height="wrap_content"
            android_theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android_id="@+id/toolbar"
                android_layout_width="match_parent"
                android_layout_height="?attr/actionBarSize"
                android_background="?attr/colorPrimary"
                app_popupTheme="@style/AppTheme.PopupOverlay" />

        </android.support.design.widget.AppBarLayout>

        <include layout="@layout/content_main" />

        <android.support.design.widget.FloatingActionButton
            android_id="@+id/fab"
            android_layout_width="wrap_content"
            android_layout_height="wrap_content"
            android_layout_gravity="bottom|end"
            android_layout_margin="@dimen/fab_margin"
            android_src="@android:drawable/ic_dialog_email" />

    </android.support.design.widget.CoordinatorLayout>

2. content_main.xml

  • This layout will host our ListView.
  • It defines the main content of the main activity.
  • It will be interpolated into the activity_main.xml.
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 
        
        
        android_layout_width="match_parent"
        android_layout_height="match_parent"
        android_paddingBottom="@dimen/activity_vertical_margin"
        android_paddingLeft="@dimen/activity_horizontal_margin"
        android_paddingRight="@dimen/activity_horizontal_margin"
        android_paddingTop="@dimen/activity_vertical_margin"
        app_layout_behavior="@string/appbar_scrolling_view_behavior"
        tools_context="com.tutorials.hp.listviewlongclick.MainActivity"
        tools_showIn="@layout/activity_main">

        <ListView
            android_layout_width="wrap_content"
            android_layout_height="wrap_content"
            android_id="@+id/lv"

            android_layout_alignParentLeft="true"
            android_layout_alignParentStart="true" />
    </RelativeLayout>

3. model.xml

  • We call it model because it will model for us a single view item in our ListView.
  • Each view item will comprise an image and text.
  • And will actually be wrapped inside a CardView.

JAVA CLASSES

We now come to our custom Java classes:

1. Movie.java

  • This class is a POJO(Plain Old Java Object). It’s our data object.
  • It represents a single movie.
  • Each movie has its properties like name and image.
  • Each movie will be rendered in a CardView.

2. ItemLongClickLister.java

  • This is an interface.
  • It defines the signature for our onItemLongClick() method.

3. MyViewHolder.java

  • Our ViewHolder class. It will hold imageview and textview for recycling by our adapter class.
  • It will implement View.OnLongClickListener. This will help us in listening to long click events.

4. CustomAdapter.java

  • Our adapter class.
  • Dervies from android.widget.BaseAdapter;.
  • It will adapt our adapter to our custom views in our listview.

5. MainActivity.java

  • Our launcher and only activity.
  • We initialize our ListView.
  • We also instantiate our CustomAdapter here. Then set it to our ListView.

Download

[sociallocker id=8131]

[Download not found]

[/sociallocker]

Leave a Reply

Your email address will not be published. Required fields are marked *

Join Us
X