Friday, 30 January 2015

Swipe ListView on item click

Java Code (Main Activity Class)


public class MainActivity extends Activity {

private SwipeListView lv_listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv_listView = (SwipeListView)findViewById(R.id.NoteList_Listview);
lv_listView.setAdapter(new AdapterClass(MainActivity.this));
}
}




Java Code (Adapter Class)


public class AdapterClass extends BaseAdapter{


Context context;


public AdapterClass(Context context) {
// TODO Auto-generated constructor stub
this.context=context;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return 5;
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub


LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.item_row, null);
View layout = (View)view.findViewById(R.id.NoteList_Row_Layout);
View  deleteLayout = (View)view.findViewById(R.id.NoteList_Row_delete);
ImageView image = (ImageView)view.findViewById(R.id.NoteList_Row_close);
TextView titel = (TextView)view.findViewById(R.id.NoteList_Row_titel);



LinearLayout.LayoutParams lp1 = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
layout.setLayoutParams(lp1);
LinearLayout.LayoutParams lp2 = new LayoutParams(SwipeListView.mRightViewWidth , LayoutParams.MATCH_PARENT);

deleteLayout.setLayoutParams(lp2);

titel.setText("Item: "+position);

image.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub

SwipeListView.mFirstX=200;
SwipeListView.mFirstY=14;
SwipeListView.motionPosition=position;
return true;
}
});

deleteLayout.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

Toast.makeText(context, "Click on Delete item "+position, Toast.LENGTH_SHORT).show();
}
});


return view;
}
}




Java Code (Swipe Class)


public class SwipeListView extends ListView {
private Boolean mIsHorizontal;
private View mPreItemView;
private View mCurrentItemView;
public static float mFirstX;
public static float mFirstY;
public static int motionPosition=-1;
public static int mRightViewWidth = 100;
private final int mDuration = 100;
private final int mDurationStep = 10;
private boolean mIsShown;


private boolean mIsFooterCanSwipe = false;

private boolean mIsHeaderCanSwipe = false;

public SwipeListView(Context context) {
super(context);
}

public SwipeListView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public SwipeListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// float lastX = 24;

float lastX = ev.getX();
float lastY = ev.getY();
//
Log.e("Hiiiiiii ", "onInterceptTouchEvent MotionPosition: "+motionPosition+" X: "+lastX+" y: " );
if(motionPosition>=0)
{
swipeOnClick(motionPosition,ev.getX(),ev.getY());

}


switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
mIsHorizontal = null;

break;

case MotionEvent.ACTION_MOVE:

break;

case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:


if (mIsShown) {
hiddenRight(mPreItemView);
}

if (mIsHorizontal != null && mIsHorizontal) {
if (mFirstX - lastX > mRightViewWidth / 2) {
showRight(mCurrentItemView);
} else {
hiddenRight(mCurrentItemView);
}

return false;
}


break;
}




return super.onInterceptTouchEvent(ev);
}

private boolean isHitCurItemLeft(float x) {
return x < getWidth() - mRightViewWidth;
}

private boolean judgeScrollDirection(float dx, float dy) {
boolean canJudge = true;

if (Math.abs(dx) > 30 && Math.abs(dx) > 2 * Math.abs(dy)) {
mIsHorizontal = true;
} else if (Math.abs(dy) > 30 && Math.abs(dy) > 2 * Math.abs(dx)) {
mIsHorizontal = false;
} else {
canJudge = false;
}

return canJudge;
}

private boolean judgeFooterView(float posX, float posY) {
// if footer can swipe
if (mIsFooterCanSwipe) {
return true;
}
// footer cannot swipe
int selectPos = pointToPosition((int) posX, (int) posY);
if (selectPos >= (getCount() - getFooterViewsCount())) {
// is footer ,can not swipe
return false;
}
// not footer can swipe
return true;
}

private boolean judgeHeaderView(float posX, float posY) {
// if header can swipe
if (mIsHeaderCanSwipe) {
return true;
}
// header cannot swipe
int selectPos = pointToPosition((int) posX, (int) posY);
if (selectPos >= 0 && selectPos < getHeaderViewsCount()) {
// is header ,can not swipe
return false;
}
// not header can swipe
return true;
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
float lastX = 24;
//float lastX = ev.getX();
float lastY = ev.getY();


Log.e("Hiiiiiii ", "onTouchEvent: "+ "X: "+lastX+" y: "+lastY );

// test footer and header
if (!judgeFooterView(mFirstX, mFirstY)
|| !judgeHeaderView(mFirstX, mFirstY)) {
return super.onTouchEvent(ev);
}

switch (ev.getAction()) {




case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:

Log.e("Hiiiiiii ", "onTouchEvent ACTION_CANCEL: " );


//clearPressedState();
if (mIsShown) {
hiddenRight(mPreItemView);
}

if (mIsHorizontal != null && mIsHorizontal) {
if (mFirstX - lastX > mRightViewWidth / 2) {
showRight(mCurrentItemView);
} else {
hiddenRight(mCurrentItemView);
}

return false;
}

break;
}

return super.onTouchEvent(ev);
}

private void clearPressedState() {
// TODO current item is still has background, issue
mCurrentItemView.setPressed(false);
setPressed(false);
refreshDrawableState();
// invalidate();
}

private void showRight(View view) {
Message msg = new MoveHandler().obtainMessage();
msg.obj = view;
msg.arg1 = view.getScrollX();
msg.arg2 = mRightViewWidth;
msg.sendToTarget();

mIsShown = true;
}

private void hiddenRight(View view) {
if (mCurrentItemView == null) {
return;
}
Message msg = new MoveHandler().obtainMessage();//
msg.obj = view;
msg.arg1 = view.getScrollX();
msg.arg2 = 0;

msg.sendToTarget();

mIsShown = false;
}

@SuppressLint("HandlerLeak")
class MoveHandler extends Handler {
int stepX = 0;
int fromX;
int toX;
View view;
private boolean mIsInAnimation = false;

private void animatioOver() {
mIsInAnimation = false;
stepX = 0;
}

@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (stepX == 0) {
if (mIsInAnimation) {
return;
}
mIsInAnimation = true;
view = (View) msg.obj;
fromX = msg.arg1;
toX = msg.arg2;
stepX = (int) ((toX - fromX) * mDurationStep * 1.0 / mDuration);
if (stepX < 0 && stepX > -1) {
stepX = -1;
} else if (stepX > 0 && stepX < 1) {
stepX = 1;
}
if (Math.abs(toX - fromX) < 10) {
view.scrollTo(toX, 0);
animatioOver();
return;
}
}

fromX += stepX;
boolean isLastStep = (stepX > 0 && fromX > toX)
|| (stepX < 0 && fromX < toX);
if (isLastStep) {
fromX = toX;
}

view.scrollTo(fromX, 0);
invalidate();

if (!isLastStep) {
this.sendEmptyMessageDelayed(0, mDurationStep);
} else {
animatioOver();
}
}
}

public int getRightViewWidth() {
return mRightViewWidth;
}

public void setRightViewWidth(int mRightViewWidth) {
this.mRightViewWidth = mRightViewWidth;
}

public void setFooterViewCanSwipe(boolean canSwipe) {
mIsFooterCanSwipe = canSwipe;
}

public void setHeaderViewCanSwipe(boolean canSwipe) {
mIsHeaderCanSwipe = canSwipe;
}

public void setFooterAndHeaderCanSwipe(boolean footer, boolean header) {
mIsHeaderCanSwipe = header;
mIsFooterCanSwipe = footer;
}


public void swipeOnClick(int position,float dx, float dy)
{

View currentItemView = getChildAt(position- getFirstVisiblePosition());
mPreItemView = mCurrentItemView;
mCurrentItemView = currentItemView;
mIsHorizontal=true;
//mIsShown =true;




if (mIsHorizontal == null) {
if (!judgeScrollDirection(dx, dy)) {
}
}

if (mIsHorizontal) {
if (mIsShown && mPreItemView != mCurrentItemView) {
hiddenRight(mPreItemView);
}

if (mIsShown && mPreItemView == mCurrentItemView) {
dx = dx - mRightViewWidth;
}

// can't move beyond boundary
if (dx < 0 && dx > -mRightViewWidth) {
mCurrentItemView.scrollTo((int) (-dx), 0);
}

} else {
if (mIsShown) {
hiddenRight(mPreItemView);
}
}




}

}


XML (Main Activity XML)

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.swipelistviewonperticularitrmclick.MainActivity" >

     <com.example.swipelistviewonperticularitrmclick.SwipeListView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/NoteList_Listview"
        
        />

</RelativeLayout>


XML (Adapter item XML)

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

    <LinearLayout
        android:id="@+id/NoteList_Row_Layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:padding="10dp"
        android:orientation="horizontal" >

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

            <TextView
                android:layout_weight="1"
                android:id="@+id/NoteList_Row_titel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:textStyle="bold" />
             
        </LinearLayout>

       

        <LinearLayout
            android:id="@+id/NoteList_Row_delete"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="#FF0000"
            android:gravity="center"
            android:orientation="horizontal"
             >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:text="Delete"
                android:textColor="@android:color/white"
                android:textStyle="bold" />
       
    </LinearLayout>

</LinearLayout>





No comments:

Post a Comment