Instant Adapter Library Add to your Eclipse Project
https://github.com/ragunathjawahar/instant-adapter
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@+id/demo_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/par_relative"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="12dp"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/imageView1"
android:text="Large Text"
android:layout_centerVertical="true"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Contact.java
package com.example.insta;public class Contact {
private int drawableResId;
private String contactName;
public Contact(int drawableResId, String contactName) {
this.drawableResId = drawableResId;
this.contactName = contactName;
}
public int getDrawableResId() {
return drawableResId;
}
public void setDrawableResId(int drawableResId) {
this.drawableResId = drawableResId;
}
public String getContactName() {
return contactName;
}
public void setContactName(String contactName) {
this.contactName = contactName;
}
}
GlobalSingleTon.java
package com.example.insta;
public class GlobalSingleTon {
private static int position = -1;
private GlobalSingleTon globalSingleton = null;
private GlobalSingleTon() {}
public static synchronized GlobalSingleTon getInstance() {
if(globalSingleton == null) {
if(globalSingleton == null) {
globalSingleton = new GlobalSingleTon();
}
return globalSingleton;
}
return globalSingleton;
}
public static int getPosition() {
return position;
}
public static void setPosition(int position) {
GlobalSingleTon.position = position;
}
}
MainActivity.java
package com.example.insta;
import com.mobsandgeeks.adapters.InstantAdapter;
import com.mobsandgeeks.adapters.ViewHandler;
import android.app.Activity;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity implements ViewHandler<Contact> {
private ListView mContactListView;
private List<Contact> mContactList;
private InstantAdapter<Contact> mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContactListView = (ListView)findViewById(R.id.demo_list);
mContactList = new ArrayList<Contact>();
mContactList.add(new Contact(R.drawable.ic_launcher, "Rajesh"));
mContactList.add(new Contact(R.drawable.ic_launcher, "Rajesh"));
mContactList.add(new Contact(R.drawable.ic_launcher, "Rajesh"));
mContactList.add(new Contact(R.drawable.ic_launcher, "Rajesh"));
mContactList.add(new Contact(R.drawable.ic_launcher, "Rajesh"));
mAdapter = new InstantAdapter<Contact>(this, R.layout.list_item, Contact.class,
mContactList);
mAdapter.setViewHandler(R.id.imageView1, this);
mAdapter.setViewHandler(R.id.textView1, this);
mAdapter.setViewHandler(R.id.par_relative, this);
mContactListView.setAdapter(mAdapter);
}
@Override
public void handleView(ListAdapter adapter, View parent, final View view, Contact contact,
int position) {
switch (view.getId()) {
case R.id.imageView1:
((ImageView)view).setBackgroundResource(contact.getDrawableResId());
break;
case R.id.textView1:
((TextView)view).setText(contact.getContactName());
break;
case R.id.par_relative:
final int pos = position;
((RelativeLayout)view).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GlobalSingleTon.setPosition(pos);
mAdapter.notifyDataSetChanged();
}
});
}
if(position == GlobalSingleTon.getPosition()) {
view.setBackgroundColor(Color.GREEN);
} else {
view.setBackgroundColor(Color.WHITE);
}
}
}
output :
No comments:
Post a Comment