A big hello to all the readers of the LiveBox blog, this is the last article in the Multifunction adapter series.

In this article we will create an abstract multifunction adapter. We can use the adapter with different objects and also improve its functionality.

The layout of the line will be the same of the previous article.


<?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="65dp" >

 <RelativeLayout
 android:id="@+id/ancor"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 >

 <!-- CONTAINER FOR EFFECTIVE ROW SHOW -->

 <LinearLayout
 android:id="@+id/container_row_file_list"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_centerInParent="true" >

 <TextView
 android:id="@+id/tv_row_title"
 android:layout_height="fill_parent"
 android:layout_width="0dp"
 android:layout_weight="1"
 android:text="@string/app_name"
 android:gravity="center"
 />

 <ImageView
 android:id="@+id/iv_button1"
 android:layout_width="0dp"
 android:layout_height="50dp"
 android:layout_weight="1"
 android:src="@drawable/ic_launcher" />

 <CheckBox
 android:id="@+id/cb_adapter"
 android:layout_width="0dp"
 android:layout_height="fill_parent"
 android:layout_weight="1"
 android:focusable="false" />
 </LinearLayout>

 </RelativeLayout>

 <View
 android:id="@+id/on_touch_panel"
 android:layout_width="fill_parent"
 android:layout_height="65dp"
 android:background="@android:color/transparent" />

</RelativeLayout>

Now we can create interfaces that we can use to abstract objects used by our adapter. We will now create an interface for objects that pass the adapter and an interface for our holder.


/**
* Your Holder must implement this interface
*
*/

public interface AdapterItem {}

/**
* Your Holder must implement this interface
*
*/
public interface BaseAdapterHolder{}

Now we can create our multifunctional abstract adapter :


package com.livebox.buttonadapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ListView;
public abstract class AbstractTotalAdapter extends ArrayAdapter&amp;lt;AdapterItem&amp;gt; implements OnScrollListener{

public interface OnBaseArrayAdapterOnScroll{

void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount);
void onScrollStateChanged(AbsListView view, int scrollState);
}

/**
* Your Holder must implement this interface
* @author Nunzio
*
*/
public interface BaseAdapterHolder{}

public class MyClickListener implements OnClickListener,OnLongClickListener,OnTouchListener{

private final int mListPosition;
public int sliderCounter;
public float currentX;
public boolean ignoreClick;
public int tollerance;
public MyClickListener(int position){
this.mListPosition=position;
this.sliderCounter=0;
this.currentX=0;
}

@Override
public void onClick(View v) {
if(!ignoreClick){
swichClickedView(v, mListPosition);
}else{
ignoreClick=true;
}
}

@Override
public boolean onLongClick(View v) {
if(sliderCounter&amp;lt;=tollerance &amp;amp;&amp;amp; sliderCounter&amp;gt;=-tollerance){
return switchLongClickedView(v, mListPosition);
}else{
return false;
}
}

@Override
public boolean onTouch(View v, MotionEvent event) {
return listenOnTouch(v,event,mListPosition,this);
}

public void clearMotionValue(){
sliderCounter=0;
currentX=0;
}

}
protected HashMap&amp;lt;Integer, MyClickListener&amp;gt; mMyClickListenerList;
protected HashMap&amp;lt;Integer, Boolean&amp;gt; mCheckedMap;
protected AdapterView&amp;lt;?&amp;gt; mListView;
protected List&amp;lt;Integer&amp;gt; currentVisiblePosition;
protected OnBaseArrayAdapterOnScroll mCustomScrollListener;
protected Context mContext;
protected int mLayoutRow;
protected List&amp;lt;AdapterItem&amp;gt; mList;
protected LayoutInflater inflater;
protected int trasparent;
protected ScrollRowOnTouchSupport scrollTouchSupport;
public AbstractTotalAdapter(Context context, int layoutRiga,
List&amp;lt;AdapterItem&amp;gt; list,AdapterView&amp;lt;?&amp;gt; listView) {
super(context, layoutRiga, list);
mContext=context;
mLayoutRow=layoutRiga;
mList=list;
inflater=LayoutInflater.from(mContext);
mMyClickListenerList=new HashMap&amp;lt;Integer, MyClickListener&amp;gt;();
mCheckedMap=new HashMap&amp;lt;Integer, Boolean&amp;gt;();
mListView=listView;
currentVisiblePosition=new ArrayList&amp;lt;Integer&amp;gt;();
trasparent=context.getResources().getColor(android.R.color.transparent);
if(mListView instanceof ListView)
((ListView)mListView).setOnScrollListener(this);
else if (mListView instanceof GridView){
((GridView)mListView).setOnScrollListener(this);
}else{
//not implemented
throw new IllegalStateException(&amp;quot;AdapterView not implemented&amp;quot;);
}
}
//**************ABSTRACT METHOD*******************
/**
* You must retur your custom Holder
* @return the viewHolder
*/
protected abstract BaseAdapterHolder getViewHolder();

/**
* In this method you can find all your view and populate your Holder
* @param h il viewHolder
* @param v la view su cui e' stato fatto l'inflater
*/
protected abstract void findView(BaseAdapterHolder h,View v);
/**
* The same of fillView in {@link BaseArrayAdapter}
* @param h
* @param item
* @param v
* @param position
* @param isChecked true if current item isChecked
* @param mPositionListener {@link MyClickListener} assigned to this position
*/
protected abstract void fillRow(BaseAdapterHolder h, AdapterItem item, View v,int position,boolean isChecked,MyClickListener mPositionListener);
//**********************************************

//*************FINAL METHOD**********************
@Override
public final int getCount() {
int size=0;
if(mList!=null){
size=mList.size();
}
return size;
}
@Override
public final AdapterItem getItem(int position) {
if(mList!=null&amp;amp;&amp;amp;position&amp;lt;mList.size())
return mList.get(position);
else
return null;
}
@Override
public final View getView(int position, View convertView, ViewGroup parent) {
BaseAdapterHolder holder;
if(convertView==null){
holder=getViewHolder();
convertView=inflater.inflate(mLayoutRow, null);
findView(holder, convertView);
convertView.setTag(holder);
}else holder=(BaseAdapterHolder)convertView.getTag();
fillView(holder, mList.get(position),convertView,position);
return convertView;
}

//***********************************************
//***************OVERRIDE************************

@Override
public final void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
currentVisiblePosition.clear();
int index=firstVisibleItem;
for(int i=0;i&amp;lt;visibleItemCount;i++){
currentVisiblePosition.add(index);
index++;
}
recoverListenerMap(currentVisiblePosition);
if(mCustomScrollListener!=null){
mCustomScrollListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
}
}
@Override
public final void onScrollStateChanged(AbsListView view, int scrollState) {
if(mCustomScrollListener!=null){
mCustomScrollListener.onScrollStateChanged(view, scrollState);
}
}

//**********************************************

//**************PRIVATE METHOD********************
/**
* Populate row
* @param h
* @param item
*/
private void fillView(BaseAdapterHolder h,AdapterItem item,View v,int position){
MyClickListener newListener=null;
if(mMyClickListenerList.containsKey(position)){
newListener=mMyClickListenerList.get(position);
}else{
newListener=new MyClickListener(position);
mMyClickListenerList.put(position, newListener);
}
fillRow(h, item, v, position,mCheckedMap.containsKey(position),newListener);
if(getOnTouchRowPanelId()!=0){
v.findViewById(getOnTouchRowPanelId()).setOnTouchListener(newListener);
}
};

private void recoverListenerMap(List&amp;lt;Integer&amp;gt; currentVisiblePos){
HashMap&amp;lt;Integer, MyClickListener&amp;gt; newHashMap=new HashMap&amp;lt;Integer, MyClickListener&amp;gt;();
for(int i:currentVisiblePos){
newHashMap.put(i, mMyClickListenerList.get(i));
}
mMyClickListenerList.clear();
mMyClickListenerList=newHashMap;
}

//**********************************************

/**
* Return list of adapter
* @return
*/
public List&amp;lt;AdapterItem&amp;gt; getAdapterList(){
return mList;
}

/**
* Set adapter ListView
* @param listView
*/
public void setListView(AdapterView&amp;lt;?&amp;gt; listView){
mListView=listView;
}

/**
* Get adapter {@link ListView}
* @return
*/
public AdapterView&amp;lt;?&amp;gt; getListView(){
return this.mListView;
}
/**
* Call this method in callback of {@link com.liveboxcloud.android.livebox.framework.BaseArrayAdapterChecked.MyCheckedChangeListenerCallback}
* @param checked
* @param pos
*/
public void setItemChecked(boolean checked,int pos){
if(checked){
mCheckedMap.put(pos, true);
}else{
mCheckedMap.remove(pos);
}
}
/**
* Call this method for force list to check items
* @param checked
* @param pos
*/
public void setItemChecked(boolean checked,int pos,boolean refresh){
setItemChecked(checked, pos);
if(refresh)
this.notifyDataSetChanged();
}
/**
* Return true if there are checked item in list
* @return
*/
public boolean isCheckedMode(){
return mCheckedMap.size()!=0;
}
/**
* Refresh list with new List
* @param newList
*/
public void refreshList(List&amp;lt;AdapterItem&amp;gt; newList) {
mList=newList;
mCheckedMap.clear();
mMyClickListenerList.clear();
this.notifyDataSetChanged();
}
/**
* Set custom scroll listener
* @param listener
*/
public void setAdapterScrollListener(OnBaseArrayAdapterOnScroll listener){
mCustomScrollListener=listener;
}
/**
* Get all checked position in list
* @return
*/
public List&amp;lt;Integer&amp;gt; getCheckedPositionList(){
List&amp;lt;Integer&amp;gt; checkedPositionList=new ArrayList&amp;lt;Integer&amp;gt;();
for(int indx:mCheckedMap.keySet()){
checkedPositionList.add(indx);
}
return checkedPositionList;
}

/**
* Decheck all items
*/
public void deCheckAllItems(){
mCheckedMap.clear();
notifyDataSetChanged();
}
/**
* CheckAllItems
*/
public void checkAllItems(){
mCheckedMap.clear();
for(int i=0;i&amp;lt;getCount();i++){
mCheckedMap.put(i, true);
}
notifyDataSetChanged();
}

/**
* Return true if all items in list are checked
* @return
*/
public boolean isAllItemChecked(){
return mCheckedMap.size()==getCount();
}

/**
* Return true if item in passed position is checked
* @param position
* @return
*/
public boolean isItemChecked(int position){
return mCheckedMap.containsKey(position);
}

/**
* Get list of checked items
* @return
*/
public List&amp;lt;AdapterItem&amp;gt; getCheckedItems(){
List&amp;lt;Integer&amp;gt; positions=getCheckedPositionList();
List&amp;lt;AdapterItem&amp;gt; checkedItems=new ArrayList&amp;lt;AdapterItem&amp;gt;();
for(int i:positions){
checkedItems.add(getItem(i));
}
return checkedItems;
}

/**
* Force refresh of listview in the same position
*/
public void forceRefresh(){
int firstPosition=mListView.getFirstVisiblePosition();
this.notifyDataSetChanged();
mListView.setSelection(firstPosition);
}

/**
*
* @param v
* @param event
* @return
*/
public boolean listenOnTouch(View v, MotionEvent event,int listPosition,MyClickListener listener){
if(getScrollRowContainerId()==0||scrollTouchSupport==null){
return false;
}
boolean toReturn=true;
try {
//Get touched child
int diff=listPosition;
if(listPosition!=this.mListView.getFirstVisiblePosition()){
diff=listPosition-this.mListView.getFirstVisiblePosition();
}
View child=this.mListView.getChildAt(diff);
//*************
float tmpX=event.getX(event.getActionIndex());
float moveDifference=0;
//get scroll container
View container=child.findViewById(getScrollRowContainerId());
//get select action view
View rowTouchPanel=child.findViewById(getOnTouchRowPanelId());
int action=event.getActionMasked();
switch (action) {
case MotionEvent.ACTION_DOWN:
listener.ignoreClick=false;
listener.currentX=tmpX;
break;
case MotionEvent.ACTION_MOVE:
moveDifference=tmpX-listener.currentX;
listener.currentX=tmpX;
if((listener.sliderCounter+moveDifference&amp;lt;0&amp;amp;&amp;amp;isScrollLeftRowEnabled(listPosition))||(listener.sliderCounter+moveDifference&amp;gt;0&amp;amp;&amp;amp;isScrollRightRowEnabled(listPosition))){
listener.sliderCounter+=moveDifference;
if(listener.sliderCounter&amp;gt;0){
onScrollRight(container);
}else{
onScrollLeft(container);
}
int tollerance=(int)((float)v.getWidth()/100.0f*5);
listener.tollerance=tollerance;
if((listener.sliderCounter&amp;gt;tollerance)||(listener.sliderCounter&amp;lt;(-tollerance))||container.getX()!=0){
if(container.getX()==0){
scrollTouchSupport.onScrollStarted(listPosition, v, listener);
toReturn=false;
}
container.setX(listener.sliderCounter);
}
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
rowTouchPanel.setBackgroundColor(trasparent);
if(listener.sliderCounter&amp;gt;((v.getWidth()/10.0f)*4.0f)){
listener.clearMotionValue();
scrollTouchSupport.onRightScroll(listPosition,container);
}else if(listener.sliderCounter&amp;lt;-((v.getWidth()/10.0f)*4.0f)){
listener.clearMotionValue();
scrollTouchSupport.onLeftScroll(listPosition,container);
}else{
if(container.getX()!=0){
listener.ignoreClick=true;
}
listener.clearMotionValue();
scrollTouchSupport.onScrollStopped(listPosition,container,false);
}
toReturn=false;
default:
toReturn=false;
break;
}
container.dispatchTouchEvent(event);

return toReturn;
} catch (Exception e) {
e.printStackTrace();
listener.clearMotionValue();
v.setX(0);
scrollTouchSupport.onScrollStopped(-1,null,true);
return false;
}
}

/**
* You must override this method if you want move your row with on touch
* @return
*/
protected int getOnTouchRowPanelId(){return 0;}

/**
* You must override this method if you want move your row with on touch
* @return
*/
protected int getScrollRowContainerId(){return 0;}

/**
* Return false if you want lock left scroll
* @return
*/
protected boolean isScrollLeftRowEnabled(int listPosition){return true;}

/**
* Return false if you want lock right scroll
* @return
*/
protected boolean isScrollRightRowEnabled(int listPosition){return true;}

/**
* You must pass your PullToRefresh manage class that extends {@link ScrollRowOnTouchSupport}
* @param support
*/
public void setScrollRowSupport(ScrollRowOnTouchSupport support){
this.scrollTouchSupport=support;
scrollTouchSupport.setScrollRowAdapter(this);
}

/**
* Get scroll container
* @param position
* @return
*/
public View getScrollContainerByPosition(int position){
int diff=position;
if(position!=mListView.getFirstVisiblePosition()){
diff=position-mListView.getFirstVisiblePosition();
}
View child=mListView.getChildAt(diff).findViewById(getScrollRowContainerId());
return child;
}

/**
* Signal to {@link ScrollRowOnTouchSupport} that ScrollRow listen finished
*/
public void cancelRowScrollListenMode(){
if(scrollTouchSupport!=null){
scrollTouchSupport.cancelRowScrollMode();
}
}

/**
* Override this method if you want perform an action when selected row scrolling to right
* @param scrollContainer
*/
protected void onScrollRight(View scrollContainer){}

/**
* Override this method if you want perform an action when selected row scrolling to left
* @param scrollContainer
*/
protected void onScrollLeft(View scrollContainer){}

/**
* This method was called when scroll right animation finish
* @param position
*/
protected void onScrollRightFinish(int position){}

/**
* This method was called when scroll left animation finish
* @param position
*/
protected void onScrollLeftFinish(int position){}

/**
* You must override this method and call correct callback for clickedView
* @param clickedView view that was clicked
*/
public void swichClickedView(View clickedView,int listPosition){}

/**
* You must override this method if you want listen on long click
* @param clickedView
* @param listPosition
* @return
*/
public boolean switchLongClickedView(View clickedView,int listPosition){return false;}

/**
* Utility class that must be ovverride for scroll row support
* @author Nunzio
*
*/
public static abstract class ScrollRowOnTouchSupport implements OnTouchListener{

private int scrollRowPosition;
private MyClickListener scrollViewListener;
private boolean scrollRowMode;
private AbstractTotalAdapter adapter;
private ScrollAnimationHandler handler;

public ScrollRowOnTouchSupport() {
handler=new ScrollAnimationHandler();
}
public void setScrollRowAdapter(AbstractTotalAdapter adapter){
this.adapter=adapter;
}

public void onScrollStarted(int position, View row, MyClickListener listener) {
scrollRowMode=true;
scrollRowPosition=position;
scrollViewListener=listener;
}

public void onScrollStopped(int position,View container,boolean withoutAnimation){
if(container!=null&amp;amp;&amp;amp;container.getX()!=0){
new RowAnimationStoppedThread(position,container.getX(), handler,withoutAnimation ).start();
}else{
cancelRowScrollMode();
}
}

protected void cancelRowScrollMode(){
scrollRowMode=false;
scrollRowPosition=0;
scrollViewListener=null;
}

public void onLeftScroll(int position,View container) {
new RowAnimationScrollThread(container.getX(), container.getWidth(), handler, position).start();
}
private void onLeftScrollComplete(int position){
adapter.onScrollLeftFinish(position);
}

public void onRightScroll(int position,View container) {
new RowAnimationScrollThread(container.getX(), container.getWidth(), handler, position).start();
}

private void onRightScrollComplete(int position){
adapter.onScrollRightFinish(position);
}

@Override
public final boolean onTouch(View v, MotionEvent event) {
if(scrollRowMode&amp;amp;&amp;amp;adapter!=null){
return adapter.listenOnTouch(v, event, scrollRowPosition, scrollViewListener);
}else{
return onTouchAccepted(v, event);
}

}

protected abstract boolean onTouchAccepted(View v,MotionEvent event);

public class ScrollAnimationHandler extends Handler{

private final String TAG=ScrollAnimationHandler.class.getSimpleName();

public final static int SCROLL_STOPPED_CODE=1;
public final static int SCROLL_ANIMATION_CONTINUE=2;
public final static int SCROLL_ANIMATION_FINISH=3;
public final static int SCROLL_STOPPED_FINISH=4;

@Override
public void handleMessage(Message msg) {
try {
int position=0;
int diff=0;
View child=null;
float offset=0;
float childX=0;
float x=0;
switch (msg.what) {
case SCROLL_STOPPED_CODE:
position=msg.getData().getInt(&amp;quot;position&amp;quot;);
diff=position;
if(position!=adapter.getListView().getFirstVisiblePosition()){
diff=position-adapter.getListView().getFirstVisiblePosition();
}
child=adapter.getListView().getChildAt(diff).findViewById(adapter.getScrollRowContainerId());
offset=msg.getData().getFloat(&amp;quot;offset&amp;quot;);
childX=child.getX();
x=0.0f;
x=childX+offset;
if(offset&amp;gt;0&amp;amp;&amp;amp;x&amp;gt;0){
x=0;
}else if(offset&amp;lt;0&amp;amp;&amp;amp;x&amp;lt;0){
x=0;
}
child.setX(x);
break;
case SCROLL_STOPPED_FINISH:
if(msg.getData().getBoolean(&amp;quot;close&amp;quot;)){
position=msg.getData().getInt(&amp;quot;position&amp;quot;);
diff=position;
if(position!=adapter.getListView().getFirstVisiblePosition()){
diff=position-adapter.getListView().getFirstVisiblePosition();
}
child=adapter.getListView().getChildAt(diff).findViewById(adapter.getScrollRowContainerId());
child.setX(0);
}
cancelRowScrollMode();
break;
case SCROLL_ANIMATION_CONTINUE:
position=msg.getData().getInt(&amp;quot;position&amp;quot;);
diff=position;
if(position!=adapter.getListView().getFirstVisiblePosition()){
diff=position-adapter.getListView().getFirstVisiblePosition();
}
child=adapter.getListView().getChildAt(diff).findViewById(adapter.getScrollRowContainerId());
offset=msg.getData().getFloat(&amp;quot;offset&amp;quot;);
childX=child.getX();
x=0.0f;
x=childX+offset;
child.setX(x);
break;
case SCROLL_ANIMATION_FINISH:
position=msg.getData().getInt(&amp;quot;position&amp;quot;);
diff=position;
if(position!=adapter.getListView().getFirstVisiblePosition()){
diff=position-adapter.getListView().getFirstVisiblePosition();
}
child=adapter.getListView().getChildAt(diff).findViewById(adapter.getScrollRowContainerId());
offset=msg.getData().getFloat(&amp;quot;offset&amp;quot;);
if(child.getX()&amp;gt;0){
child.setX(child.getWidth());
onRightScrollComplete(position);
}else if(child.getX()&amp;lt;0){
child.setX(-child.getWidth());
onLeftScrollComplete(position);
}
break;
default:
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}

}

private class RowAnimationScrollThread extends Thread{

private final String TAG=RowAnimationScrollThread.class.getSimpleName();

private float currentX;
private float width;
private ScrollAnimationHandler h;
private int position;

public RowAnimationScrollThread(float currentX,float width,ScrollAnimationHandler h,int position) {
super();
this.currentX = currentX;
this.width=width;
this.h = h;
this.position = position;
}

@Override
public void run() {
float offset=0;
float distance=0;
float sign=1;
if(currentX&amp;gt;0){
distance=this.width-this.currentX;
}else{
distance=this.width+this.currentX;
sign=-1;
}
offset=(distance)*10.0f/300.0f;
Bundle data=new Bundle();
data.putFloat(&amp;quot;offset&amp;quot;, sign*offset);
data.putInt(&amp;quot;position&amp;quot;, this.position);
while(distance&amp;gt;=0.0f){
distance-=offset;
Message msg=Message.obtain(h, ScrollAnimationHandler.SCROLL_ANIMATION_CONTINUE);
msg.setData(data);
msg.sendToTarget();
try {
sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Message msg=Message.obtain(h, ScrollAnimationHandler.SCROLL_ANIMATION_FINISH);
msg.setData(data);
msg.sendToTarget();
}

}

private class RowAnimationStoppedThread extends Thread{

private final String TAG=RowAnimationStoppedThread.class.getSimpleName();

private float currentX;
private ScrollAnimationHandler h;
private int position;
private boolean withoutAnimation;

public RowAnimationStoppedThread(int position,float currentX,ScrollAnimationHandler h,boolean withoutAnimation) {
super();
this.currentX = currentX;
this.h=h;
this.position=position;
this.withoutAnimation=withoutAnimation;
}

@Override
public void run() {
if(withoutAnimation){
Bundle data=new Bundle();
data.putBoolean(&amp;quot;close&amp;quot;, true);
data.putInt(&amp;quot;position&amp;quot;, this.position);
Message msg=Message.obtain(h, ScrollAnimationHandler.SCROLL_STOPPED_FINISH);
msg.setData(data);
msg.sendToTarget();
}else{
float offset=0;
float distance=0;
float sign=1;
if(currentX&amp;gt;0){
distance=this.currentX;
sign=-1;
}else{
distance=-this.currentX;
}
offset=(distance*5.0f)/300.0f;
Bundle data=new Bundle();
data.putFloat(&amp;quot;offset&amp;quot;, sign*offset);
data.putBoolean(&amp;quot;close&amp;quot;, false);
data.putInt(&amp;quot;position&amp;quot;, this.position);
while(distance&amp;gt;=0.0f){
distance-=offset;
Message msg=Message.obtain(h, ScrollAnimationHandler.SCROLL_STOPPED_CODE);
msg.setData(data);
msg.sendToTarget();
try {
sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Message msg=Message.obtain(h, ScrollAnimationHandler.SCROLL_STOPPED_FINISH);
msg.setData(data);
msg.sendToTarget();
}
}

}

}
}

To create an adapter that supports scrollable row and multiselection button in the row and now we just have to extend our abstract adapter
and implement the abstract methods in this way:


package com.livebox.buttonadapter;

import java.util.List;
import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.Toast;

public class ButtonListAdapter extends AbstractTotalAdapter{

private Context c;

public ButtonListAdapter(Context context, int resource, List&amp;lt;AdapterItem&amp;gt; objects,AdapterView&amp;lt;?&amp;gt; listView) {
super(context, resource, objects,listView);
c=context;
mListView=listView;
}
@Override
public void swichClickedView(View v,int listPosition){
switch(v.getId()){
case R.id.iv_button1:
Toast.makeText(c, &amp;quot;Clicked button 1 on position: &amp;quot;+listPosition, Toast.LENGTH_SHORT).show();
break;
case R.id.cb_adapter:
if(mCheckedMap.containsKey(listPosition)){
mCheckedMap.remove(listPosition);
}else{
mCheckedMap.put(listPosition, true);
}
break;
}
}

@Override
public boolean switchLongClickedView(View v,int listPosition){
switch(v.getId()){
case R.id.iv_button1:
Toast.makeText(c, &amp;quot;LongClicked button 1 on position: &amp;quot;+listPosition, Toast.LENGTH_SHORT).show();
break;
}
return true;
}

/**
* This method was called when scroll right animation finish
* @param position
*/
protected void onScrollRightFinish(int position){
this.scrollTouchSupport.cancelRowScrollMode();
}

/**
* This method was called when scroll left animation finish
* @param position
*/
protected void onScrollLeftFinish(int position){
this.scrollTouchSupport.cancelRowScrollMode();
}

/**
* You must override this method if you want move your row with on touch
* @return
*/
@Override
protected int getOnTouchRowPanelId(){return R.id.on_touch_panel;}

/**
* You must override this method if you want move your row with on touch
* @return
*/
@Override
protected int getScrollRowContainerId(){return R.id.container_row_file_list;}

/**
* Return false if you want lock left scroll
* @return
*/
@Override
protected boolean isScrollLeftRowEnabled(int listPosition){return true;}

/**
* Return false if you want lock right scroll
* @return
*/
@Override
protected boolean isScrollRightRowEnabled(int listPosition){return true;}

private class Holder implements BaseAdapterHolder{
ImageView ivButton1;
CheckBox cb;
}
@Override
protected BaseAdapterHolder getViewHolder() {
// Return new instance of Holder
return new Holder();
}
@Override
protected void findView(BaseAdapterHolder h, View v) {
// find view
Holder holder=(Holder)h;
holder.ivButton1=(ImageView)v.findViewById(R.id.iv_button1);
holder.cb=(CheckBox)v.findViewById(R.id.cb_adapter);
}
@Override
protected void fillRow(
BaseAdapterHolder h,
AdapterItem item,
View v,
int position,
boolean isChecked,
com.livebox.buttonadapter.AbstractTotalAdapter.MyClickListener mPositionListener) {
// populate row
Holder holder=(Holder)h;
holder.cb.setChecked(isChecked);
holder.ivButton1.setOnClickListener(mPositionListener);
holder.ivButton1.setOnLongClickListener(mPositionListener);
holder.cb.setOnClickListener(mPositionListener);
}

}

This way we can save our precious several lines of code :)…

You can download the sample project from the link below:

MultifunctionAdapter 4

Android Multifunction adapter part 4

Leave a Reply