Instantly share code, notes, and snippets.
Last active
January 19, 2020 09:08
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save esabook/1c2bb13eadf4ea4849ac81af7b484fbe to your computer and use it in GitHub Desktop.
switchable card view
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ***; | |
import android.content.Context; | |
import android.graphics.Rect; | |
import android.os.Handler; | |
import android.util.AttributeSet; | |
import android.view.MotionEvent; | |
import android.view.View; | |
import android.view.ViewConfiguration; | |
import androidx.annotation.NonNull; | |
import androidx.annotation.Nullable; | |
import androidx.recyclerview.widget.DividerItemDecoration; | |
import androidx.recyclerview.widget.LinearLayoutManager; | |
import androidx.recyclerview.widget.PagerSnapHelper; | |
import androidx.recyclerview.widget.RecyclerView; | |
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; | |
public class BannerRecycleView extends RecyclerView { | |
public BannerRecycleView(@NonNull Context context) { | |
this(context, null); | |
} | |
public BannerRecycleView(@NonNull Context context, @Nullable AttributeSet attrs) { | |
this(context, attrs, 0); | |
} | |
public BannerRecycleView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
init(); | |
} | |
void init() { | |
final LinearLayoutManager layoutManager = new LinearLayoutManager(null, LinearLayoutManager.HORIZONTAL, false); | |
setLayoutManager(layoutManager); | |
runnable = new Runnable() { | |
@Override | |
public void run() { | |
int position = layoutManager.findFirstVisibleItemPosition() + 1; | |
position = position == getAdapter().getItemCount() ? 0 : position; | |
smoothScrollToPosition(position); | |
handler.postDelayed(this, 4000); | |
} | |
}; | |
handler.postDelayed(runnable, 4000); | |
addOnScrollListener(new OnScrollListener() { | |
@Override | |
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) { | |
super.onScrollStateChanged(recyclerView, newState); | |
if (newState == RecyclerView.SCROLL_STATE_IDLE) { | |
handler.postDelayed(runnable, 4000); | |
} else { | |
handler.removeCallbacks(runnable); | |
} | |
} | |
}); | |
PagerSnapHelper snapHelper = new PagerSnapHelper(); | |
snapHelper.attachToRecyclerView(this); | |
ItemIndicator itemIndicator = new ItemIndicator(); | |
addItemDecoration(itemIndicator); | |
final int w = getPaddingLeft(); | |
DividerItemDecoration itemSeparator = new DividerItemDecoration(getContext(), DividerItemDecoration.HORIZONTAL) { | |
@Override | |
public void getItemOffsets(Rect outRect, | |
View view, | |
RecyclerView parent, | |
RecyclerView.State state) { | |
if (parent.getChildAdapterPosition(view) == parent.getAdapter().getItemCount() - 1) { | |
outRect.set(0, 0, 0, 0); | |
} else { | |
outRect.set(0, 0, w, 0); | |
} | |
} | |
}; | |
itemSeparator.setDrawable(getResources().getDrawable(R.drawable.divider)); | |
addItemDecoration(itemSeparator); | |
setClipChildren(false); | |
setClipToPadding(false); | |
} | |
private Handler handler = new Handler(); | |
Runnable runnable = new Runnable() { | |
@Override | |
public void run() { | |
} | |
}; | |
public void interceptSwipeRefreshLayoutWhenScroll(final SwipeRefreshLayout swipeRefreshLayout) { | |
final int mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop() * 2; | |
setOnTouchListener(new View.OnTouchListener() { | |
private float mPrevX; | |
@Override | |
public boolean onTouch(View v, MotionEvent event) { | |
switch (event.getAction()) { | |
case MotionEvent.ACTION_DOWN: | |
mPrevX = MotionEvent.obtain(event).getX(); | |
swipeRefreshLayout.setEnabled(false); | |
break; | |
case MotionEvent.ACTION_UP: | |
final float eventX = event.getX(); | |
float xDiff = Math.abs(eventX - mPrevX); | |
if (xDiff > mTouchSlop) { | |
swipeRefreshLayout.setEnabled(true); | |
return false; | |
} | |
} | |
return false; | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment