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...
Related
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);
}
});
I am having the html file in my asset folder. Now i am trying to access the content with onTouch event. The following is my code.I have added two images for the reference.
view = (WebView) findViewById(R.id.webview);
view.getSettings().setJavaScriptEnabled(true);
view.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return (event.getAction() == MotionEvent.ACTION_MOVE); // disabling touch events
}
});
view.loadUrl("file:///android_asset/www/index.html");
I am using touch listener on my views to drag them. findViewById(R.id.myimage1).setOnTouchListener(new MyTouchListener());
Unfortunately there are lots of views therefore I have to stack them in a scrollview.
Here is the touch listener:
private final class MyTouchListener implements View.OnTouchListener {
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(data, shadowBuilder, view, 0);
view.setVisibility(View.INVISIBLE);
return true;
} else {
return false;
}
}
}
Because of this touch listnener I am unable to scroll the listview.
When I try to scroll, the touch listener fires and view's shadow is created instead of scroll.
How can I use both events or can use Drag n Drop functionality in a ScrollView
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 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.