Ver Fonte

Initial commit for UIListViewCustomAdapter project.

Cristian Tanas há 11 anos atrás
pai
commit
279fae838e

+ 27 - 0
UIListViewCustomAdapter/AndroidManifest.xml

@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="com.example.uilistviewcustomadapter"
+    android:versionCode="1"
+    android:versionName="1.0" >
+
+    <uses-sdk
+        android:minSdkVersion="14"
+        android:targetSdkVersion="19" />
+
+    <application
+        android:allowBackup="true"
+        android:icon="@drawable/ic_launcher"
+        android:label="@string/app_name"
+        android:theme="@style/AppTheme" >
+        <activity
+            android:name=".MainActivity"
+            android:label="@string/app_name" >
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+        </activity>
+    </application>
+
+</manifest>

BIN
UIListViewCustomAdapter/ic_launcher-web.png


BIN
UIListViewCustomAdapter/libs/android-support-v4.jar


BIN
UIListViewCustomAdapter/res/drawable-hdpi/ic_launcher.png


BIN
UIListViewCustomAdapter/res/drawable-hdpi/usericon.png


BIN
UIListViewCustomAdapter/res/drawable-mdpi/ic_launcher.png


BIN
UIListViewCustomAdapter/res/drawable-xhdpi/ic_launcher.png


BIN
UIListViewCustomAdapter/res/drawable-xxhdpi/ic_launcher.png


+ 12 - 0
UIListViewCustomAdapter/res/layout/activity_main.xml

@@ -0,0 +1,12 @@
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    tools:context="${relativePackage}.${activityClass}" >
+
+    <ListView
+        android:id="@+id/customLv"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent" />
+
+</RelativeLayout>

+ 31 - 0
UIListViewCustomAdapter/res/layout/list_item.xml

@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="utf-8"?>
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:padding="10dp" >
+    
+    <ImageView 
+        android:id="@+id/itemIcon"
+        android:layout_width="50dp"
+        android:layout_height="50dp"
+        android:layout_alignParentLeft="true"
+        android:contentDescription="@string/itemIconDescription"/>
+    
+    <TextView 
+        android:id="@+id/itemUserName"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_toRightOf="@+id/itemIcon"
+        android:layout_alignTop="@+id/itemIcon"
+        android:layout_marginLeft="10dp"
+        android:textSize="24sp"/>
+    
+    <TextView 
+        android:id="@+id/itemUserBirthday"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_toRightOf="@+id/itemIcon"
+        android:layout_below="@+id/itemUserName"
+        android:layout_alignLeft="@+id/itemUserName"/>
+
+</RelativeLayout>

+ 11 - 0
UIListViewCustomAdapter/res/values-v11/styles.xml

@@ -0,0 +1,11 @@
+<resources>
+
+    <!--
+        Base application theme for API 11+. This theme completely replaces
+        AppBaseTheme from res/values/styles.xml on API 11+ devices.
+    -->
+    <style name="AppBaseTheme" parent="android:Theme.Holo.Light">
+        <!-- API 11 theme customizations can go here. -->
+    </style>
+
+</resources>

+ 12 - 0
UIListViewCustomAdapter/res/values-v14/styles.xml

@@ -0,0 +1,12 @@
+<resources>
+
+    <!--
+        Base application theme for API 14+. This theme completely replaces
+        AppBaseTheme from BOTH res/values/styles.xml and
+        res/values-v11/styles.xml on API 14+ devices.
+    -->
+    <style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
+        <!-- API 14 theme customizations can go here. -->
+    </style>
+
+</resources>

+ 8 - 0
UIListViewCustomAdapter/res/values/strings.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+
+    <string name="app_name">UIListViewCustomAdapter</string>
+    <string name="hello_world">Hello world!</string>
+    <string name="itemIconDescription">User picture</string>
+
+</resources>

+ 20 - 0
UIListViewCustomAdapter/res/values/styles.xml

@@ -0,0 +1,20 @@
+<resources>
+
+    <!--
+        Base application theme, dependent on API level. This theme is replaced
+        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
+    -->
+    <style name="AppBaseTheme" parent="android:Theme.Light">
+        <!--
+            Theme customizations available in newer API levels can go in
+            res/values-vXX/styles.xml, while customizations related to
+            backward-compatibility can go here.
+        -->
+    </style>
+
+    <!-- Application theme. -->
+    <style name="AppTheme" parent="AppBaseTheme">
+        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
+    </style>
+
+</resources>

+ 30 - 0
UIListViewCustomAdapter/src/com/example/uilistviewcustomadapter/MainActivity.java

@@ -0,0 +1,30 @@
+package com.example.uilistviewcustomadapter;
+
+import android.app.Activity;
+import android.os.Bundle;
+import android.widget.ListView;
+
+public class MainActivity extends Activity {
+	
+	private static final int MAX_USERS = 10;
+	
+	ListView				userListView;
+	UserListCustomAdapter	userAdapter;
+
+	@Override
+	protected void onCreate(Bundle savedInstanceState) {
+		super.onCreate(savedInstanceState);
+		setContentView(R.layout.activity_main);
+		
+		// Get a reference to the ListView
+		userListView = (ListView) findViewById(R.id.customLv);
+		
+		// Create a new UserListCustomAdapter for the list from a 
+		// randomly generated user list
+		userAdapter = new UserListCustomAdapter(this, 
+				UserData.generateRandomUsers(MAX_USERS));
+		
+		// Set the Adapter for the ListView
+		userListView.setAdapter(userAdapter);
+	}
+}

+ 52 - 0
UIListViewCustomAdapter/src/com/example/uilistviewcustomadapter/UserData.java

@@ -0,0 +1,52 @@
+package com.example.uilistviewcustomadapter;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class UserData {
+
+	private String 	name;
+	private String	birthday;
+	private int		picture;
+	
+	public String getName() {
+		return name;
+	}
+	
+	public void setName(String name) {
+		this.name = name;
+	}
+	
+	public String getBirthday() {
+		return birthday;
+	}
+	
+	public void setBirthday(String birthday) {
+		this.birthday = birthday;
+	}
+	
+	public int getPicture() {
+		return picture;
+	}
+	
+	public void setPicture(int picture) {
+		this.picture = picture;
+	}
+	
+	public static List<UserData> generateRandomUsers(int numberOfUsers) {
+		
+		List<UserData> generatedUsers = new ArrayList<>();
+		
+		for ( int i = 0; i < numberOfUsers; i++ ) {
+			
+			UserData user = new UserData();
+			user.name = Long.toHexString(Double.doubleToLongBits(Math.random()));
+			user.birthday = String.valueOf(Math.random());
+			user.picture = R.drawable.usericon;
+			
+			generatedUsers.add(user);
+		}
+		
+		return generatedUsers;
+	}
+}

+ 75 - 0
UIListViewCustomAdapter/src/com/example/uilistviewcustomadapter/UserListCustomAdapter.java

@@ -0,0 +1,75 @@
+package com.example.uilistviewcustomadapter;
+
+import java.util.List;
+
+import android.content.Context;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.BaseAdapter;
+import android.widget.ImageView;
+import android.widget.TextView;
+
+/**
+ * Custom adapter extending BaseAdapter.
+ * 
+ * Contains a list of UserData objects.
+ *
+ */
+public class UserListCustomAdapter extends BaseAdapter {
+	
+	// Application context
+	Context				context;
+	
+	// List of users that will be rendered in the ListView
+	List<UserData>		userList;
+
+	// Set the context and user list from the constructor
+	public UserListCustomAdapter(Context context, List<UserData> userList) {
+		this.context = context;
+		this.userList = userList;
+	}
+	
+	// AdapterViews call this method to know how many objects are to be displayed
+	@Override
+	public int getCount() {
+		return userList.size();
+	}
+
+	// AdapterViews call this method to retrieve an object at the specified position
+	@Override
+	public Object getItem(int position) {
+		return userList.get(position);
+	}
+
+	// AdapterViews call this method to get the object's ID at the specified position
+	@Override
+	public long getItemId(int position) {
+		return position;
+	}
+
+	// AdapterViews call this method to retrieve the row View that they must 
+	// display for the object at the specified position
+	@Override
+	public View getView(int position, View convertView, ViewGroup parent) {
+		
+		// The View corresponding to the ListView's row layout
+		View currentView = convertView;
+		
+		// If the convertView element is not null, the ListView is asking to 
+		// recycle the existing View, so there is no need to create the View; just update its content
+		if ( currentView == null ) {
+			LayoutInflater inflater = (LayoutInflater) context.
+					getSystemService(Context.LAYOUT_INFLATER_SERVICE);
+			currentView = inflater.inflate(R.layout.list_item, parent, false);
+		}
+		
+		UserData userData = userList.get(position);
+		((TextView) currentView.findViewById(R.id.itemUserName)).setText(userData.getName());
+		((TextView) currentView.findViewById(R.id.itemUserBirthday)).setText(userData.getBirthday());
+		((ImageView) currentView.findViewById(R.id.itemIcon)).setImageResource(userData.getPicture());
+		
+		return currentView;
+	}
+
+}