I have a Grid Layout with multiple TextViews as children. I have added an event listener to each TextView. I want to detect when a user swipes across multiple TextViews in the layout
i.e. I want to identify all the TextViews that have been touched while swiping.
I recieve touch event for only the first TextView that has been touched , I do not receive any touch events for other TextViews when user swipes his finger across multiple TextViews in GridLayout.
I have tried using
TextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
,
TextView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent event) {
}
});
and
TextView.setOnHoverListener(new View.OnHoverListener() {
#Override
public boolean onHover(View v, MotionEvent event) {
return false;
}
});
I have also tried overriding
#Override
public boolean onTouchEvent(MotionEvent event) {
}
What should i do now ?
try to grid view ItemClickListener.
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
// Get the GridView selected/clicked item text
String selectedItem = parent.getItemAtPosition(position).toString();
// Display the selected/clicked item text and position on TextView
tv.setText("GridView item clicked : " +selectedItem
+ "\nAt index position : " + position);
}
});
Related
I have a PopupWindow which is shown as a drop down when I click a Item on my ListView:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
View v = getLayoutInflater().inflate(R.layout.popup_click_menu, null);
final PopupWindow mypopupWindow = new PopupWindow(v,300, RelativeLayout.LayoutParams.WRAP_CONTENT, true);
mypopupWindow.showAsDropDown(view, -153, 0);
}
The result is this:
My question is: I want to have a dynamic position of my pop up. So if I click on the middle of the Item then it should be shown in the middle. How can I do that?
You can solve this with a TouchListener:
list.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View view, MotionEvent event) {
positionX = (int) event.getX();
return false; // not consumed; forward to onClick
}
});
Then you have the exact x-position and you can move the PopupWindow by onItemClick:
mypopupWindow.showAsDropDown(view, positionX, 0);
i already created a navigation drawer bar with a menu but i want to highlight and disable the selected item on the menu bar when i check the drawer bar.
how can i do that ?
also i did not see the click animation when i click the item on the menu
please help
Thanks.
this is the code i used
mRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
#Override
public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {
View child = recyclerView.findChildViewUnder(motionEvent.getX(),motionEvent.getY());
if(child!=null && mGestureDetector.onTouchEvent(motionEvent)){
Drawer.closeDrawers();
Toast.makeText(MainActivity.this, "The Item Clicked is: " + recyclerView.getChildPosition(child), Toast.LENGTH_SHORT).show();
int pos = recyclerView.getChildPosition(child);
if(pos== 1){
Intent intent = new Intent(getApplicationContext(),About.class);
startActivity(intent);
}
return true;
}
return false;
}
#Override
public void onTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {
}
#Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
});
RecyclerView does not handle item selection or states like a ListView does. Instead you have to handle this manually in your view holder.
The first thing you can do is declare your item view as clickable, in your `ViewHolder constructor :
public ViewHolder(View itemView) {
super(itemView);
// Make this view clickable
itemView.setClickable(true);
// ...
}
http://www.grokkingandroid.com/statelistdrawables-for-recyclerview-selection/
I have an EditText (vertically scrollable) inside one of the fragments in a ViewPager (horizontally swipable).
By default, touch events inside the EditText can swipe the ViewPager but not scroll the EditText. By using the code below (which I don't really understand), touch events inside the EditText can scroll the EditText but not swipe the ViewPager:
editText.setOnTouchListener(new View.OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
v.getParent().requestDisallowInterceptTouchEvent(! ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_UP));
return false;
}
});
How can I simultaneously allow swiping of the ViewPager and scrolling of the EditText?
You can handle this according to the solution given here.
pager.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View p_v, MotionEvent p_event)
{
editText.getParent().requestDisallowInterceptTouchEvent(false);
// We will have to follow above for all scrollable contents
return false;
}
});
For the EditText, you will have to add this snippet.
editText.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View p_v, MotionEvent p_event)
{
// this will disallow the touch request for parent scroll on touch of child view
p_v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
Hope it helps...
I implemented a gesture detector to find out when the user will fling my listview (and when the last element of the list is in view) so that I may add more data to the list. The list has a custom adapter that I've built. Each row has a few textviews in it and an image button. I was using the imagebutton (which is an arrow) with a click listener in my adapter to open another activity related to the pressed row.
Management wants the user to be able to click anywhere on the row in order to activate it. So:
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
View v = convertView;
final ViewHolder viewHolder;
.....
//viewHolder.ibShipmentDetails.setOnClickListener(new OnClickListener()
v.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View view)
{
....
}
}
But now, my fling detector won't work correctly. Sometimes it responds and afterwards works correctly; restart the activity, again it doesn't work.
Here is my gesture detector:
class MyGestureDetector extends GestureDetector.SimpleOnGestureListener
{
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if((e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY))
{
if(!loading && ((totalItemCount - visibleItemCount) <= firstVisibleItem))
{
if(allTabSelected)
{
allPageNo++;
}
else
{
openPageNo++;
}
new GetShipmentPreviews().execute(1);
}
}
}
And in my activity:
gestureDetector = new GestureDetector(this, new MyGestureDetector());
gestureListener = new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
return gestureDetector.onTouchEvent(event);
}
};
listView.setOnTouchListener(gestureListener);
What should I do to keep v.setOnClickListener and the onFling() command intact?
Turns out my MotionEvents from onFling are null. This apparently happens because the event gets somehow "eaten" by the onClickListener from my custom adapter.
I got the solution from this answer : https://stackoverflow.com/a/7159738/1688731
I added the following code to my Activity class:
#Override
public boolean dispatchTouchEvent(MotionEvent ev)
{
super.dispatchTouchEvent(ev);
return gestureScanner.onTouchEvent(ev);
}
You should use listViev.setOnItemClickListener() once, instead of calling v.setOnClickListener() for all list items.
I'm trying to stick a ViewPager (horizontal scroll) in a tableview (vertical scroll). It appears that it works unless you do any kind of vertical scroll in the ViewPager which happens normally when trying to swipe. Any ideas on how I can get this to work properly?
You can use code like this (the trick is to disallow vertical scrolls in the parent as soon as a horizontal scroll is detected):
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
}
#Override
public void onPageScrollStateChanged(int state) {
if (state == ViewPager.SCROLL_STATE_DRAGGING) {
viewPager.getParent().requestDisallowInterceptTouchEvent(
true);
}
}
});