Showing posts with label view handler. Show all posts
Showing posts with label view handler. Show all posts

Friday, 21 February 2014

HighLighting the Selected List Item using SingleTon Pattern in Android

 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) {
                     globalSingleton =  new 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 :



Source Code :  Coming Soon..

Sunday, 11 August 2013

Instant Adapter View Handler Example in Android

Get Instant Adapter Library from the following Url : https://github.com/ragunathjawahar/instant-adapter

Import and Add Instant library Project to your Project as shown below :





Then use the following code :

In activity_main.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ListView
        android:id="@+id/name_list_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</RelativeLayout>


In list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/person_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/person_name"
            android:layout_width="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_height="wrap_content"
            android:text="TextView" />
      
    </LinearLayout>

</RelativeLayout>

Create Person.java Model

package com.rajeshvijayakumar.insta;

public class Person {

    private int photoResId;
    private String name;

    public Person(int photoResId, String name) {

        this.photoResId = photoResId;
        this.name = name;
    }

    public int getPhotoResId() {
        return photoResId;
    }

    public void setPhotoResId(int photoResId) {
        this.photoResId = photoResId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

MainActivity.java

package com.rajeshvijayakumar.insta;
public class MainActivity extends Activity implements Evaluator<Person>,
        OnItemClickListener {

    private ListView mPersonListView;
    private InstantAdapter<Person> mAdapter;
    private List<Person> mPersons;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mPersonListView = (ListView) findViewById(R.id.name_list_view);
        mPersons = getPersons();
        mAdapter = new InstantAdapter<Person>(MainActivity.this,
                R.layout.list_item, Person.class, mPersons);
        mAdapter.setViewHandler(R.layout.list_item, MainActivity.this);
        mAdapter.setViewHandler(R.id.person_image, MainActivity.this);
        mAdapter.setViewHandler(R.id.person_name, MainActivity.this);
        mPersonListView.setAdapter(mAdapter);
        mPersonListView.setOnItemClickListener(this);
    }

    private List<Person> getPersons() {
        List<Person> personLst = new ArrayList<Person>();
        personLst.add(new Person(R.drawable.ic_fav, "Rajesh"));
        personLst.add(new Person(R.drawable.ic_rate, "Mahesh"));
        personLst.add(new Person(R.drawable.ic_person, "Akshay"));
        personLst.add(new Person(R.drawable.ic_group, "Aakash"));
        return personLst;
    }

    @Override
    public void handleView(ListAdapter adapter, View parent, View view,
            Person person, int position) {

        switch (view.getId()) {
        case R.id.person_image:
            ((ImageView) view).setImageResource(person.getPhotoResId());
            break;
        case R.id.person_name:
            ((TextView) view).setText(person.getName());
            break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adpaView, View v, int position,
            long id) {
        Toast.makeText(this, mPersons.get(position).getName().toString(),
                Toast.LENGTH_SHORT).show();
    }
}

Output :


Source Code :  Coming soon to Github...........