Hi I want to show a layout when I swipe up. I want that would be animated. When I swipe up below this layout appear another layout. I did this but it not works like I want to. A new layout show after all swipe not when I am swiping .
public class RelativeLayoutTouchListener implements View.OnTouchListener {
static final String logTag = "ActivitySwipeDetector";
private Activity activity;
static final int MIN_DISTANCE = 100;// TODO change this runtime based on screen resolution. for 1920x1080 is to small the 100 distance
private float downX, downY, upX, upY;
private View view;
// private MainActivity mMainActivity;
public RelativeLayoutTouchListener(MainActivity mainActivity, View view) {
activity = mainActivity;
this.view = view;
}
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
Log.e("sdasdas", "dsfdsfdsfsdf");
downX = event.getX();
downY = event.getY();
return true;
}
case MotionEvent.ACTION_UP: {
upX = event.getX();
upY = event.getY();
float deltaX = downX - upX;
float deltaY = downY - upY;
final ViewGroup.LayoutParams params = MainActivity.relativeLayout1.getLayoutParams();
if (deltaY < 0) {
params.height = (int) activity.getResources().getDimension(R.dimen.dimen_entry_in_dp);
// relativeLayout1.setLayoutParams(params);
view.setVisibility(View.GONE);
collapse(relativeLayout1, 400, params.height);
MainActivity.listView1.setVisibility(View.GONE);
} else {
// params.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
params.height = (int) activity.getResources().getDimension(R.dimen.dimen_entry_in_dp2);
// MainActivity.relativeLayout1.setLayoutParams(params);
expand(relativeLayout1, 400, params.height);
view.setVisibility(View.VISIBLE);
MainActivity.listView1.setVisibility(View.VISIBLE);
}
return false; // no swipe horizontally and no swipe vertically
}
// case MotionEvent.ACTION_UP:
}
return false;
}
public static void expand(final View v, int duration, int targetHeight) {
int prevHeight = v.getHeight();
v.setVisibility(View.VISIBLE);
ValueAnimator valueAnimator = ValueAnimator.ofInt(prevHeight, targetHeight);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
v.getLayoutParams().height = (int) animation.getAnimatedValue();
v.requestLayout();
}
});
valueAnimator.setInterpolator(new DecelerateInterpolator());
valueAnimator.setDuration(duration);
valueAnimator.start();
}
public static void collapse(final View v, int duration, int targetHeight) {
int prevHeight = v.getHeight();
ValueAnimator valueAnimator = ValueAnimator.ofInt(prevHeight, targetHeight);
valueAnimator.setInterpolator(new DecelerateInterpolator());
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
v.getLayoutParams().height = (int) animation.getAnimatedValue();
v.requestLayout();
}
});
valueAnimator.setInterpolator(new DecelerateInterpolator());
valueAnimator.setDuration(duration);
valueAnimator.start();
}
}
Use a Dialog class to achieve the animation with ease:
public class CustomDialog extends Dialog {
private Fragment fragment;
private Activity activity;
public CustomDialog(Fragment fragment) {
super(fragment.getContext(), R.style.DialogTheme);
this.fragment=fragment;
}
public CustomDialog(Activity activity){
super(activity, R.style.DialogTheme);
this.activity = activity;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.dialog_photo_upload);
Window window = getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
wlp.gravity = Gravity.BOTTOM;
wlp.windowAnimations = R.style.popupWindowDropDownAnimation;
window.setAttributes(wlp);
window.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
}
Call this from your Fragment or Activity whenever you want to show it:
private CustomDialog mCustomDialog = new CustomDialog(this).show();
Call this to hide it:
mCustomDialog.dismiss();
It'll produce something like
Related
There are texts on my static layout, the layout is an item in a Recyclerview. The touch event of the Recyclerview class controls the pinch zoom to the text with ScaleGestureDetector. The zooming senario is, when the user action move the screen of Recyclerview, getting the screenshot of the recyclerview and displaying the image over the Recyclerview, and the user zooming to the image. When the action up, applying new text size that coming from scaling to the items. The new text size is should be same with when the zooming to image displaying text size. For this I use RelativeSizeSpan and float scaler value. I want to limit the total text size changing but it just doesn't happen.
The real problem is, the pinch zoom can be done more than once and it is necessary to collect the scaling that each of them because the pinch zoom reseting each action pointer up. (mScaleFactor = 1.0f) And the all of scaling shouldn't cross the specified limit. (MAX_ZOOM and MIN_ZOOM)
Recyclerview:
private ScaleListener mScaleListener;
private ScaleGestureDetector mScaleGestureDetector;
#Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction() & MotionEvent.ACTION_MASK;
if(event.getPointerCount() == 2 && (action == MotionEvent.ACTION_MOVE || action == MotionEvent.ACTION_POINTER_UP)) {
if(mScaleGestureDetector == null){
mScaleListener = new ScaleListener(mRecyclerview, mContext);
mScaleGestureDetector = new ScaleGestureDetector(mContext, mScaleListener);
} return mScaleGestureDetector.onTouchEvent(event);
}
}
Adapter:
private void changeTextSize(float mScaleFactor){
...
float newFontSize = (relativeSizeSpan.getSizeChange() * mScaleFactor);
...
}
ScaleListener:
public class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
private static final float MAX_ZOOM = 2.5f;
private static final float MIN_ZOOM = 0.5f;
private float mScaleFactor = 1.0f;
private ImageView mScreenShotView;
private Context mContext;
private View mView;
public ScaleListener(View mView, Context mContext) {
this.mView = mView;
this.mContext = mContext;
init();
}
private void init(){
int mWidth = mView.getWidth();
int mHeight = mView.getHeight();
if(mWidth == 0 || mHeight == 0) return;
mScreenShotView = new ImageView(mContext);
mScreenShotView.setLayoutParams(new ViewGroup.LayoutParams(mWidth, mHeight));
ViewGroup mPhysicalParentLayout = (ViewGroup) mView.getParent();
mPhysicalParentLayout.addView(mScreenShotView, mPhysicalParentLayout.indexOfChild(mView));
}
#Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
mScreenShotView.setBackgroundDrawable(new BitmapDrawable(Kit.getScreenshot(mView)));
mScreenShotView.setAlpha(1f); mView.setAlpha(0f);
return true;
}
#Override
public boolean onScale(ScaleGestureDetector scaleGestureDetector){
mScaleFactor *= scaleGestureDetector.getScaleFactor();
mScaleFactor = Math.max(MIN_ZOOM, Math.min(mScaleFactor, MAX_ZOOM));
mScreenShotView.setScaleX(mScaleFactor);
mScreenShotView.setScaleY(mScaleFactor);
return true;
}
#Override
public void onScaleEnd(ScaleGestureDetector detector) {
((ReadBookRcAdapter)Objects.requireNonNull(((RecyclerView)mView).getAdapter())).changeTextSize(mScaleFactor);
mScreenShotView.animate().alpha(0f).setDuration(300).setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationStart(Animator animation) {
mView.animate().alpha(1f).setDuration(300).setListener(null);
}
#Override
public void onAnimationEnd(Animator animation) {
mScreenShotView.setScaleX(1.0f);
mScreenShotView.setScaleY(1.0f);
mScaleFactor = 1.0f;
}
});
}
}
Solved the issue with restricting value of TextView.setTextSize(). It's my adapter class:
public class ReadBookRcAdapter extends RecyclerView.Adapter<ReadBookRcAdapter.ReadBookViewHolder> {
private Context mContext;
private ArrayList<ReadBookTextBlockModel> mTextViewBlocks;
private float mTextSize;
public ReadBookRcAdapter(Context context) {
this.mContext = context;
this.mTextViewBlocks = new ReadBookTextBlockModel(context).getTextBlocks();
mTextSize = 15.0f;
}
class ReadBookViewHolder extends RecyclerView.ViewHolder {
TextView mTextViewItem;
ReadBookViewHolder(#NonNull View itemView) {
super(itemView);
this.mTextViewItem = itemView.findViewById(R.id.readBookTextRow);
}
void bind(ReadBookTextBlockModel dataList){
if (mTextSize != 15.0f) mTextViewItem.setTextSize(mTextSize);
mTextViewItem.setText(dataList.getBlockText());
}
}
#NonNull
#Override
public ReadBookViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rc_item_read_book, parent, false);
return new ReadBookViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ReadBookViewHolder holder, int position) {
ReadBookTextBlockModel model = mTextViewBlocks.get(position);
holder.bind(model);
}
public void setTextSize(float scale){
float maxTextSize = 37.5f;
float minTextSize = 7.5f;
if((mTextSize == minTextSize && scale < 1.0f) || (mTextSize == maxTextSize && scale >= 1.0f))
return;
float newTextSize = mTextSize * scale;
newTextSize = Math.max(minTextSize, Math.min(newTextSize, maxTextSize));
mTextSize = newTextSize;
Log.e("mTextSize*scale", String.valueOf(mTextSize));
notifyDataSetChanged();
}
}
I'm using vertical RecyclerView to list my items and SnapHelper to snap center item. The idea is to randomize selection, so user swipe screen or shake the device and it is scrolling to random position.
Number of items is 20, however I use Integer.MAX_VALUE for the number of elements in RecyclerView and initialize RecyclerView with position Integer.MAX_VALUE / 2 to create some kind of endless list.
To scroll to random position on device shake I need to know current snapped item position.
Is there any way to do it?
Here is my Fragment code:
public class PlaceListFragment extends Fragment {
private static final String TAG = "PlaceListFragment";
public static final String ARG_KEY1 = "key1";
private ArrayList<PlaceItem> places;
private RecyclerView recyclerView;
private SensorManager sensorManager;
private float accelValue;
private float accelLast;
private float shake;
SnapHelper snapHelper;
Vibrator vibe;
public static PlaceListFragment newInstance() {
Bundle args = new Bundle();
PlaceListFragment fragment = new PlaceListFragment();
fragment.setArguments(args);
return fragment;
}
public static PlaceListFragment newInstance(ArrayList<PlaceItem> places) {
Bundle args = new Bundle();
args.putParcelableArrayList(PlaceListActivity.KEY_PLACES, places);
PlaceListFragment fragment = new PlaceListFragment();
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate()");
super.onCreate(savedInstanceState);
places = getArguments().getParcelableArrayList(PlaceListActivity.KEY_PLACES);
accelValue = SensorManager.GRAVITY_EARTH;
accelLast = SensorManager.GRAVITY_EARTH;
shake = 0.00f;
vibe = (Vibrator) getActivity().getSystemService(Context.VIBRATOR_SERVICE);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_place_list, container, false);
recyclerView = (RecyclerView) v.findViewById(R.id.place_list);
snapHelper = new LinearSnapHelper();
snapHelper.attachToRecyclerView(recyclerView);
recyclerView.setOnFlingListener(snapHelper);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(new PlaceListAdapter(getActivity(), places));
recyclerView.scrollToPosition(PlaceListAdapter.MIDDLE);
sensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);
sensorManager.registerListener(sensorListener, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_NORMAL);
return v;
}
#Override
public void onResume() {
super.onResume();
}
#Override
public void onPause() {
super.onPause();
}
private final SensorEventListener sensorListener = new SensorEventListener() {
#Override
public void onSensorChanged(SensorEvent event) {
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
accelLast = accelValue;
accelValue = (float) Math.sqrt((double) (x*x + y*y + z*z));
float delta = accelValue - accelLast;
shake = shake * 0.9f + delta;
if (shake > 12) {
vibe.vibrate(200);
}
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
}
And here is adapter:
public class PlaceListAdapter extends RecyclerView.Adapter<PlaceListAdapter.PlaceAdapterHolder> {
private final FragmentActivity context;
public static final int HALF_MAX_VALUE = Integer.MAX_VALUE/2;
public static int MIDDLE;
private List<PlaceItem> placeItems;
public static class PlaceAdapterHolder extends RecyclerView.ViewHolder {
private ImageView image;
private TextView textMain;
private TextView textRating;
public PlaceAdapterHolder(View itemView) {
super(itemView);
image = (ImageView) itemView.findViewById(R.id.icon);
textMain = (TextView) itemView.findViewById(R.id.txt_main_line);
textRating = (TextView) itemView.findViewById(R.id.txt_right_field);
}
public void bindPlace(PlaceItem placeItem) {
String placeName = placeItem.getName() == null? "?":placeItem.getName();
String firstLetter = placeName.substring(0, 1);
ColorGenerator generator = ColorGenerator.MATERIAL; // or use DEFAULT
int color = generator.getColor(placeName);
TextDrawable drawable = TextDrawable.builder()
.beginConfig()
.toUpperCase()
.endConfig()
.buildRect(firstLetter, color);
image.setImageDrawable(drawable);
textMain.setText(placeItem.getName());
textRating.setText(placeItem.getRating());
}
}
public PlaceListAdapter(FragmentActivity context, List<PlaceItem> placeItems) {
this.context = context;
this.placeItems = placeItems;
MIDDLE = HALF_MAX_VALUE - HALF_MAX_VALUE % placeItems.size();
}
#Override
public PlaceListAdapter.PlaceAdapterHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.place_list_one_line_item, parent, false);
return new PlaceAdapterHolder(view);
}
#Override
public int getItemCount() {
return Integer.MAX_VALUE;
}
#Override
public void onBindViewHolder(PlaceListAdapter.PlaceAdapterHolder holder, final int position) {
final PlaceItem placeItem = getItem(position);
holder.bindPlace(placeItem);
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentManager fm = context.getSupportFragmentManager();
PlaceDetailsFragment dialog = PlaceDetailsFragment.newInstance(getItem(position));
dialog.show(fm, "DETAILS_DIALOG");
}
});
}
private PlaceItem getItem(int position)
{
return placeItems.get(position % placeItems.size());
}
}
I used this on a project that had a RecyclerView with SnapHelper, not sure if it is what you want.
mRecyclerView.setHasFixedSize(true);
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
mRecyclerView.setLayoutManager(mLayoutManager);
// specify an adapter (see also next example)
mAdapter = new DemoSlidesAdapter(getApplicationContext());
mRecyclerView.setAdapter(mAdapter);
final SnapHelper snapHelper = new LinearSnapHelper();
snapHelper.attachToRecyclerView(mRecyclerView);
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if(newState == RecyclerView.SCROLL_STATE_IDLE) {
View centerView = snapHelper.findSnapView(mLayoutManager);
int pos = mLayoutManager.getPosition(centerView);
Log.e("Snapped Item Position:",""+pos);
}
}
});
I try to use this code with a PagerSnapHelper to mimic the pager behaviour and it was useful but i found some corner cases to solve, if you move fast from the last page to the first one and keep swapping until see the boundarie then the IDLE state doesnt happen and you lose your index. to solve that I move out the position from the IF and add a extra condition for this corner case.
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
val centerView = snapHelper.findSnapView(mLayoutManager)
val pos = mLayoutManager.getPosition(centerView!!)
if (newState == RecyclerView.SCROLL_STATE_IDLE || (pos == 0 && newState == RecyclerView.SCROLL_STATE_DRAGGING)) {
Log.d("BINDING", "positionView SCROLL_STATE_IDLE: $pos")
}
}
Code is in kotlin hope it helps
private fun recyclerViewScrollListener() = object: RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
// layoutManager is LinearLayoutManager
val pos = layoutManager.findFirstCompletelyVisibleItemPosition()
Log.e(TAG, "onScrollStateChanged: $pos")
}
}
}
recyclerView.setOnScrollListener(recyclerViewScrollListener())
recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener(){
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
....
}
})
I have a RecyclerView that has a grid of items. Upon clicking on an item, it highlights.
I also want that when the user swipes right the a 'next' method is called, and when the user swipes left, a 'previous' method is called.
However, the two don't work out with each other, as each intercepts the other's events.
How do I get them to work together?
This is my code:
RecyclerView Adapter
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
myHolder = holder as MyView;
myHolder.mMainView.SetOnClickListener(this);
if (selected_position == position)
{
holder.ItemView.SetBackgroundColor(Color.LightGray);
}
else
{
holder.ItemView.SetBackgroundColor(Color.Transparent);
}
}
public void OnClick(View v)
{
int position = mRecyclerView.GetChildLayoutPosition((View)sender);
// Updating old as well as new positions
NotifyItemChanged(selected_position);
selected_position = position;
NotifyItemChanged(selected_position);
}
Fragment that contains the RecyclerView
calendarRecyclerView.SetOnTouchListener(this);
public bool OnTouch(View v, MotionEvent e)
{
switch (e.Action)
{
case MotionEventActions.Down:
x1 = e.GetX();
break;
case MotionEventActions.Up:
x2 = e.GetX();
float deltaX = x2 - x1;
if (Math.Abs(deltaX) > MIN_DISTANCE)
{
// Left to Right swipe action
if (x2 > x1)
{
NextMonth();
}
// Right to left swipe action
else
{
PreviousMonth();
}
}
break;
}
return false;
}
Because I put return false in the OnTouch event, the item's click event is fired. However, the MouseDown event doesn't fire in OnTouch, preventing swiping back detection (beacuse x1 is always 0).
OnTouch event gets called on the first click, and the OnClick gets called only on the second click
Because MotionEventActions.Down and OnClickconflict. As a workaround I suggest you to change the background color at the MotionEventActions.Down event.
Create your own click listener
Call the listener when you touch down your items.
The listener will callback to MainActivity to notify the item changed.
At the same time the touch event will called.
I have set the OnTouchListener in the viewholder :
public class MyViewHolder:RecyclerView.ViewHolder,IOnTouchListener
{
private TextView textView;
private MyItemClickListener mListener;
private Context myContext;
float x1 = 0;
float x2 = 0;
public TextView TextView { get { return textView; } }
public MyViewHolder(View v, MyItemClickListener mItemClickListener) : base(v)
{
textView = v.FindViewById<TextView>(Resource.Id.itemText);
mListener = mItemClickListener;
v.SetOnTouchListener(this);
}
public MyViewHolder(View v, MyItemClickListener mItemClickListener, Context myContext) : this(v, mItemClickListener)
{
this.myContext = myContext;
}
public bool OnTouch(View v, MotionEvent e)
{
switch (e.Action)
{
case MotionEventActions.Down:
x1 = e.GetX();
if (mListener != null)
{
mListener.OnItemClick(v, Position);
}
break;
case MotionEventActions.Up:
x2 = e.GetX();
float deltaX = x2 - x1;
if (Math.Abs(deltaX) > 5)
{
// Left to Right swipe action
if (x2 > x1)
{
NextMonth(v);
}
// Right to left swipe action
else
{
PreviousMonth(v);
}
}
break;
}
return true;
}
public Boolean NextMonth(View v)
{
Toast.MakeText(myContext, "NextMonth called", ToastLength.Short).Show();
return true;
}
public Boolean PreviousMonth(View v)
{
Toast.MakeText(myContext, "PreviousMonth called", ToastLength.Short).Show();
return true;
}
}
Defined the click interface :
public interface MyItemClickListener
{
void OnItemClick(View view, int postion);
}
set the click callback in the MainActivity to change the background color:
public class MainActivity : Activity,MyItemClickListener
{
RecyclerView mRecyclerView;
RecyclerView.LayoutManager mLayoutManager;
CustomAdapter mAdapter;
string[] dataSet;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
InitDataSet();
SetContentView(Resource.Layout.Main);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView = FindViewById<RecyclerView>(Resource.Id.recyclerView);
mRecyclerView.SetLayoutManager(mLayoutManager);
mAdapter = new CustomAdapter(dataSet,this);
mAdapter.setOnItemClickListener(this);
mRecyclerView.SetAdapter(mAdapter);
//mRecyclerView.SetOnTouchListener(this);
}
public void InitDataSet()
{
dataSet = new string[60];
for (int i = 0; i < 60; i++)
{
dataSet[i] = "This is element #" + i;
}
}
public void OnItemClick(View view, int postion)
{
mAdapter.NotifyItemChanged(CustomAdapter.selected_position);
CustomAdapter.selected_position = postion;
mAdapter.NotifyItemChanged(postion);
}
}
}
Note: Keep your finger move fast, if the speed is slow enough the MotionEventActions.Down will not be called.
Github souce code
Screen shot:
Try this in your onClick
public void OnClick(View v)
{
int position = mRecyclerView.GetChildLayoutPosition((View)sender);
int oldPosition = selectedPosition;
selected_position = position;
// Updating old as well as new positions
NotifyItemChanged(oldPosition);
NotifyItemChanged(selected_position);
}
Notice that you have to change the selected position before updating both items
When I scroll on Recyclerview in my app it closes.
Here is a screenshot:
The main page of app- when i scroll the reyclerview of main page my app was force close:
This is my adapter:
public class RecyclerViewDataAdapter extends RecyclerView.Adapter<RecyclerViewDataAdapter.ItemRowHolder> {
private ArrayList<SectionDataModel> dataList;
private Context mContext;
private ImageView bg;
private boolean isLang;
public RecyclerViewDataAdapter(Context context, ArrayList<SectionDataModel> dataList) {
this.dataList = dataList;
this.mContext = context;
}
#Override
public ItemRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item, null);
ItemRowHolder mh = new ItemRowHolder(v);
return mh;
}
#Override
public void onBindViewHolder(final ItemRowHolder itemRowHolder, int i) {
final String sectionName = dataList.get(i).getHeaderTitle();
final String sectionSub = dataList.get(i).getHeaderSubTitle();
final int sectionhesder = dataList.get(i).getHeader();
final int bgd = dataList.get(i).getPhoto();
final int bgg = dataList.get(i).getBg();
ArrayList singleSectionItems = dataList.get(i).getAllItemsInSection();
itemRowHolder.itemSubTitle.setText(sectionSub);
itemRowHolder.itemTitle.setText(sectionName);
itemRowHolder.bg.setImageResource(bgd);
itemRowHolder.bg.setBackgroundResource(bgg);
if (isLang = Locale.getDefault().getLanguage().equals("fa")) {
itemRowHolder.bg.setScaleType(ImageView.ScaleType.FIT_START);
} else {
itemRowHolder.bg.setScaleType(ImageView.ScaleType.FIT_END);
}
SectionListDataAdapter itemListDataAdapter = new SectionListDataAdapter(mContext, singleSectionItems);
itemRowHolder.recycler_view_list.setHasFixedSize(true);
itemRowHolder.recycler_view_list.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
itemRowHolder.recycler_view_list.setAdapter(itemListDataAdapter);
itemRowHolder.recycler_view_list.setNestedScrollingEnabled(false);
/* itemRowHolder.recycler_view_list.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
// Disallow ScrollView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_UP:
//Allow ScrollView to intercept touch events once again.
v.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
// Handle RecyclerView touch events.
v.onTouchEvent(event);
return true;
}
});*/
itemRowHolder.btnMore.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(mContext, TopicActivity.class);
intent.putExtra("topicn",sectionName.toString());
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
mContext.startActivity(intent);
}
});
/* Glide.with(mContext)
.load(feedItem.getImageURL())
.diskCacheStrategy(DiskCacheStrategy.ALL)
.centerCrop()
.error(R.drawable.bg)
.into(feedListRowHolder.thumbView);*/
}
#Override
public int getItemCount() {
return (null != dataList ? dataList.size() : 0);
}
public class ItemRowHolder extends RecyclerView.ViewHolder {
protected RelativeLayout itemHeader;
protected TextView itemTitle;
protected TextView itemSubTitle;
protected RecyclerView recycler_view_list;
protected Button btnMore;
public ImageView bg;
public ItemRowHolder(View view) {
super(view);
this.itemTitle = (TextView) view.findViewById(R.id.itemTitle);
this.itemSubTitle = (TextView) view.findViewById(R.id.itemsub);
this.itemHeader = (RelativeLayout) view.findViewById(R.id.header);
this.recycler_view_list = (RecyclerView) view.findViewById(R.id.recycler_view_list);
this.btnMore= (Button) view.findViewById(R.id.btnMore);
this.bg= (ImageView) view.findViewById(R.id.bg);
}}}
Here is my class to create my "cursor":
public class Ball extends View {
private final float x;
private final float y;
private final int r;
private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
public Ball(Context context, float x, float y, int r) {
super(context);
mPaint.setColor(0xFFFF0000);
this.x = x;
this.y = y;
this.r = r;
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(x, y, r, mPaint);
}
}
And here is my switch/case that I'm trying to do to make it work.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FrameLayout main = (FrameLayout) findViewById(R.id.main_view);
final TextView textView = (TextView)findViewById(R.id.textView);
final View touchView = (View) findViewById(R.id.touchView);
main.setOnTouchListener(new View.OnTouchListener() {
Ball ball;
#Override
public boolean onTouch(View v, MotionEvent event) {
int eventAction = event.getAction();
textView.setText(String.valueOf(" Azimuth: " + -1*(Math.round((event.getX()/13.33333333)-30)))
+ "\n Elevation: " + String.valueOf(-1*(Math.round((event.getY()/18.33333333)-30))));
float x = event.getX();
float y = event.getY();
FrameLayout flView = (FrameLayout) v;
//This creates a ton of circles. I just want one to appear and then go away.
//flView.addView(ball);
switch(eventAction){
case MotionEvent.ACTION_DOWN:
ball = new Ball(findViewById(R.id.main_view).getContext(), x, y, 5);
flView.addView(ball);
break;
case MotionEvent.ACTION_UP:
flView.removeView(ball);
break;
case MotionEvent.ACTION_MOVE:
ball = null;
break;
}
return true;
}
});
}
Basically I can create a ball/circle/cursor when I touch the screen, but I want it to delete and create another one when I click somewhere else. I'm trying to find out a way to delete the previous if possible, because then I can keep generating new balls/circle/cursors one at a time.