Skip to content

Instantly share code, notes, and snippets.

@jayrambhia
Last active June 17, 2016 04:08
Show Gist options
  • Save jayrambhia/429228d96d6f8bd73590 to your computer and use it in GitHub Desktop.
Save jayrambhia/429228d96d6f8bd73590 to your computer and use it in GitHub Desktop.
Inbox style Swipe ListView
<?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="80dp"
android:background="#ffebeff1"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:padding="8dp"
android:background="#ffffeb3b"
android:id="@+id/audio_object_shareview"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/share_96_l"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:padding="8dp"
android:background="#ff558b2f"
android:id="@+id/audio_object_deleteview"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="@drawable/delete_96_l"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:padding="8dp"
android:weightSum="1"
android:background="#ffebeff1"
android:id="@+id/audio_object_mainview"
android:orientation="horizontal" >
<!-- add views here that you want to show at the top !-->
</LinearLayout>
</RelativeLayout>
public class PlayListObject extends ArrayAdapter<AudioObject> {
private int layoutResource;
private LayoutInflater inflater;
private float density = 2f;
private ListView listView;
public SPlayerPlayListObject(Context con, int resource) {
super(con, resource);
layoutResource = resource;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View workingView = null;
if (convertView == null) {
workingView = inflater.inflate(layoutResource, null);
} else {
workingView = convertView;
}
final AudioObjectHolder holder = getAudioObjectHolder(workingView);
final AudioObject entry = getItem(position);
/* set values here */
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.mainView.getLayoutParams();
params.rightMargin = 0;
params.leftMargin = 0;
holder.mainView.setLayoutParams(params);
workingView.setOnTouchListener(new SwipeDetector(holder, position));
return workingView;
}
private AudioObjectHolder getAudioObjectHolder(View workingView) {
Object tag = workingView.getTag();
AudioObjectHolder holder = null;
if (tag == null || !(tag instanceof AudioObjectHolder)) {
holder = new AudioObjectHolder();
holder.mainView = (LinearLayout)workingView.findViewById(R.id.audio_object_mainview);
holder.deleteView = (RelativeLayout)workingView.findViewById(R.id.audio_object_deleteview);
holder.shareView = (RelativeLayout)workingView.findViewById(R.id.audio_object_shareview);
/* initialize other views here */
workingView.setTag(holder);
} else {
holder = (AudioObjectHolder) tag;
}
return holder;
}
public static class AudioObjectHolder {
public LinearLayout mainView;
public RelativeLayout deleteView;
public RelativeLayout shareView;
/* other views here */
}
public void setListView(ListView view) {
listView = view;
}
// swipe detector class here
}
public class PlayListObject extends ArrayAdapater<AudioObject> {
// class that is shown above
public class SwipeDetector implements View.OnTouchListener {
private static final int MIN_DISTANCE = 300;
private static final int MIN_LOCK_DISTANCE = 30; // disallow motion intercept
private boolean motionInterceptDisallowed = false;
private float downX, upX;
private AudioObjectHolder holder;
private int position;
public SwipeDetector(AudioObjectHolder h, int pos) {
holder = h;
position = pos;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
downX = event.getX();
return true; // allow other events like Click to be processed
}
case MotionEvent.ACTION_MOVE: {
upX = event.getX();
float deltaX = downX - upX;
if (Math.abs(deltaX) > MIN_LOCK_DISTANCE && listView != null && !motionInterceptDisallowed) {
listView.requestDisallowInterceptTouchEvent(true);
motionInterceptDisallowed = true;
}
if (deltaX > 0) {
holder.deleteView.setVisibility(View.GONE);
} else {
// if first swiped left and then swiped right
holder.deleteView.setVisibility(View.VISIBLE);
}
swipe(-(int) deltaX);
return true;
}
case MotionEvent.ACTION_UP:
upX = event.getX();
float deltaX = upX - downX;
if (Math.abs(deltaX) > MIN_DISTANCE) {
// left or right
swipeRemove();
} else {
swipe(0);
}
if (listView != null) {
listView.requestDisallowInterceptTouchEvent(false);
motionInterceptDisallowed = false;
}
holder.deleteView.setVisibility(View.VISIBLE);
return true;
case MotionEvent.ACTION_CANCEL:
holder.deleteView.setVisibility(View.VISIBLE);
return false;
}
return true;
}
private void swipe(int distance) {
View animationView = holder.mainView;
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) animationView.getLayoutParams();
params.rightMargin = -distance;
params.leftMargin = distance;
animationView.setLayoutParams(params);
}
private void swipeRemove() {
remove(getItem(position));
notifyDataSetChanged();
}
}
}
@kerimovscreations
Copy link

Can you type here your "AudioObject" class, I'm beginner and it's hard to determine any class which have to fit this concept.

@ao1ansal
Copy link

ao1ansal commented Oct 6, 2015

please share the main activity aswell....can u please share the source dode

@razikatz94
Copy link

razikatz94 commented Jun 17, 2016

getX() should be getRawX(). getRawX() returns absolute coordinates, rather than relative to the view. This way the view will move smoothly and wont jump around as you move it because the margins are constantly changing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment