Horizontal Recyclerview in Navigation drawer - android

I'm trying to implement a horizontal recyclerview in a Navigation Drawer. It works fine aside from the fact that when I swipe to the left, the Navigation Drawer gets closed. Is there a way to prevent the Navigation Drawer from closing just when the user interacts with the recyclerview? I tried this way:
mNavigationRecyclerHourly.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
Log.d(TAG, "onTouch: ");
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN: mDrawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
break;
case MotionEvent.ACTION_MOVE: mDrawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
break;
case MotionEvent.ACTION_UP: mDrawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
break;
}
return true;
}
});
Recyclerview xml:
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerview_navigation_hourly"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/progress_navigation_uv"
android:scrollbars="horizontal"
android:overScrollMode="never"
android:scrollbarThumbHorizontal="#drawable/thumb_navigation"
android:layout_alignParentStart="true"
android:padding="8dp">
</android.support.v7.widget.RecyclerView>
Thanks for your help!

Related

Passing Scroll event to another view

The Objective:
I am writing a view that behaves like a RecyclerView inside the ViewPager i-e; The view moves when the finger is moved horizontally and RecyclerView scrolls when the finger is moved vertically. Please note that my custom view has another objective while the finger is moving horizontally, so therefore I can't use viewpager here.
The Problem:
The overlay view handles the horizontal finger motion movement properly, however the vertical event when passed down to the RecyclerView doesn't result in scrolling.
The Question:
What should I do that the vertical finger movement is passed to the RecyclerView and result in Scrolling?
The Code:
mOverlayView.setOnTouchListener(new View.OnTouchListener() {
switch (action) {
case MotionEvent.ACTION_UP:
//some code
return true;
case MotionEvent.ACTION_DOWN:
mBackupTouchDownEvent = event;
// some code
return true;
case MotionEvent.ACTION_MOVE:
// some logic {
movingHorizontally = true;
}
return true;
} else
movingHorizontally = false;
break;
}
// executor came down here? This means finger moved vertically
mRecyclerView.requestFocus();
mRecyclerView.dispatchTouchEvent(mBackupTouchDownEvent); // don't cause scrolling
mRecyclerView.dispatchTouchEvent(event); // don't cause scrolling? Alternative solutoin?
return false;
}
XML:
<RelativeLayout
android:background="#color/colorPrimary"
android:layout_width="400dp"
android:layout_height="500dp"
android:layout_centerVertical="true"
>
<android.support.v7.widget.RecyclerView
android:layout_width="380dp"
android:layout_height="match_parent"
android:id="#+id/scrollView">
</android.support.v7.widget.RecyclerView>
<RelativeLayout
android:layout_width="300dp"
android:layout_height="match_parent"
android:background="#77555555"
android:id="#+id/overlayScreen"/>
</RelativeLayout>

How to use a view pager inside a navigation drawer? [duplicate]

I want to use a seekbar in a Navigation Drawer
Basically I need to slide left and right to set the seekbar and, well, you guessed it, it slides the navigation drawer...
What I would like to do is to slide the seekbar when it's focused and the navigationdrawer when it's not.
How should I do that?
Here is my code when setting an item (I haven't set the seekbar yet)
private View onCritereView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View mView = inflater.inflate(R.layout.fragment_critere, container, false);
LinearLayout mLinearLayout = (LinearLayout) mView.findViewById(R.id.critere_linearlayout);
View mViewElement = inflater.inflate(R.layout.item_critere, null);
((TextView)mViewElement.findViewById(R.id.critere_title)).setText("Prix Maximum");
mLinearLayout.addView(mViewElement);
return mView;
}
Here is the item_critere.xml with the seekbar:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/critere_item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="*">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Large Text"
android:id="#+id/critere_title"
android:layout_gravity="left|center_vertical"
android:layout_column="0"/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/critere_qualifier"
android:layout_column="1"
android:layout_span="4"/>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Medium Text"
android:id="#+id/critere_value"
android:layout_span="4"/>
</TableRow>
</TableLayout>
Thanks in advance :)
Just add an onTouchListener. When you touch the screen on the seekbar (action_down) disallow parent to intercept the event.
seekbar.setOnTouchListener(new ListView.OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
int action = event.getAction();
switch (action)
{
case MotionEvent.ACTION_DOWN:
// Disallow Drawer to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_UP:
// Allow Drawer to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
// Handle seekbar touch events.
v.onTouchEvent(event);
return true;
}
});
Use DrawerLayout.setDrawerLockMode
instead of ViewParent.requestDisallowInterceptTouchEvent.
In #dumazy answer requestDisallowInterceptTouchEvent solves this issue but it also creates another.
While you are scrolling on navigation drawer, If you touch on SeekBar, OnSeekBarChangeListener will trigger. So scrolling event will break and seekbar progress will start to change by moving your finger.
I modified the original answer a little bit to solve this issue using DrawerLockMode:
mSeekBar.setOnTouchListener(new SeekBar.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
// Disallow Drawer to intercept touch events.
// v.getParent().requestDisallowInterceptTouchEvent(true);
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
// Allow Drawer to intercept touch events.
// v.getParent().requestDisallowInterceptTouchEvent(false);
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNDEFINED);
break;
}
// Handle SeekBar touch events.
v.onTouchEvent(event);
return true;
}
});

how to make layouts transparent on click

I have two layouts and one imageView. The parent layout linearLayout1 have image background and its child layout relativeLayout1 have dark background. So when i move imageView on screen it display a selective area of linearLayout1 image . I applied touch motion but no idea how to display selective area. the code is here
public class MainActivity extends Activity {
// RelativeLayout viewRelative;
ImageButton imgBtnTarget;
Animation aniRotate, aniZoom;
PointF downPoint = new PointF();// Record Mouse Position When Pressed Down
PointF strtPointImg = new PointF(); // Record Start Position of 'img'
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgBtnTarget = (ImageButton) findViewById(R.id.imageButtontTarget);
imgBtnTarget.setOnTouchListener( new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// for moving image
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
PointF mv = new PointF(event.getX() - downPoint.x, event
.getY() - downPoint.y);
imgBtnTarget.setX((int) (strtPointImg.x + mv.x));
imgBtnTarget.setY((int) (strtPointImg.y + mv.y));
strtPointImg = new PointF(imgBtnTarget.getX(), imgBtnTarget.getY());
break;
}
return false;
}
});
}
}
and here is xml resource
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/back" >
<RelativeLayout
android:id="#+id/relativeViewBlack"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/black" >
<ImageButton
android:id="#+id/imageButtontTarget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#android:color/transparent"
android:scaleType="fitCenter"
android:src="#drawable/target" />
</RelativeLayout>
In Java code:
LinearLayout ll = (LinearLayout) findViewById(R.id.your_ll_id);
ll.setAlpha(TRANSPARENT_VALUE);
Or:
ll.setBackgroundColor(Color.TRANSPARENT);
If you want transparent all from the begining add to your Layout in XML:
android:background="#00ffffff"
EDIT:
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
v.setAlpha(0.5f);
break;
case MotionEvent.ACTION_UP:
v.setAlpha(1.0f);
break;
case MotionEvent.ACTION_MOVE:
//your code...
break;
}
return true;
}

ViewPager Swipe with Button OnTouchListener event

I have a ViewPager with 2 items into it and a Button for ViewPager Animation . I wanted to swipe the ViewPager with the TouchEvent of Button without moving my finger up i.e Sliding my finger from Button & getting the ViewPager Swipe
Currently I am able to get A swipe only when I move Up my finger & swipe not on the TouchEvent
Below is My code Snippets...of XML
<Button
android:id="#+id/slide_btn_speak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#android:drawable/ic_btn_speak_now"
android:contentDescription="#string/app_name" />
<TextView
android:id="#+id/slide_txtView_inside"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_toRightOf="#+id/slide_btn_speak"
android:text="#string/app_name" />
<android.support.v4.view.ViewPager
android:id="#+id/ui_pager"
android:layout_width="wrap_content"
android:layout_height="150dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/slide_btn_speak"
android:layout_toRightOf="#+id/slide_btn_speak"
android:scrollbars="horizontal" >
</android.support.v4.view.ViewPager>
<Button
android:id="#+id/slide_btn_drag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#android:drawable/ic_btn_speak_now"
android:contentDescription="#string/app_name" />
My Activity Snippets...
public class SlidingActitvity extends Activity {
ViewPager viewPager;
Button slide_button_start ;
Vibrator vibrator;
static int count ;
int x_position, y_position;
String hint[] ={" SWIPE TO CANCEL " , ""};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sliding_actitvity);
viewPager = (ViewPager) findViewById(R.id.ui_pager);
vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
slide_button_start = (Button) findViewById(R.id.slide_btn_drag);
slide_button_start.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
int viewPager_width = viewPager.getWidth();
x_position = (int) event.getX();
y_position = (int) event.getY();
if(x_position<= 0){
vibrator.cancel();
viewPager.bringToFront();
logIt("Touch..." +x_position +"..."+y_position);
}
break;
case MotionEvent.ACTION_DOWN:
vibrator.vibrate(100);
final Animation animation = AnimationUtils.loadAnimation(SlidingActitvity.this, R.anim.left);
ViewPagerAdapter adapter = new ViewPagerAdapter(SlidingActitvity.this , hint);
viewPager.setAdapter(adapter);
viewPager.setVisibility(ViewPager.VISIBLE);
viewPager.setAnimation(animation);
logIt("Touch...Down");
break;
case MotionEvent.ACTION_UP:
logIt("Touch...Up");
vibrator.cancel();
break;
case MotionEvent.ACTION_OUTSIDE:
logIt("Your are out...");
vibrator.cancel();
break ;
default:
break;
}
return true;
}
});
}
Any Answer is Appreciated...
By returning true in the button's OnTouchListener, you're consuming the touch event. Specifically, the ViewPager needs to be informed of MotionEvent.ACTION_DOWN so that it can intercept touch events when it detects that it is being swiped.
You will need to carefully consider in which cases touch events get passed up the view hierarchy such that the ViewPager can continue to perform its magic.

Using a Viewflipper with Gridview

I've created a calendar which works fine, using a GridView which has an OnClickListener.
Now I wrapped the two GridViews in a ViewFlipper. The ViewFlipper has an OnTouchListener which also works fine, I can change the view by using ontouch when dragging. The problem is though that I have to drag on the EMTPY space in the Activity in order to use the ViewFlipper. When I drag on the GridView, nothing happends at all. But I can click on the GridView for OnClickListener.
xml:
<ViewFlipper android:id="#+id/details"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<GridView
android:id="#+id/weeks"
android:numColumns="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="8">
</GridView>
<GridView
android:id="#+id/calendar"
android:numColumns="7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</GridView>
</LinearLayout>
android code:
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// Get the action that was done on this touch event
switch (arg1.getAction())
{
case MotionEvent.ACTION_DOWN:
{
// store the X value when the user's finger was pressed down
downXValue = arg1.getX();
break;
}
case MotionEvent.ACTION_UP:
{
// Get the X value when the user released his/her finger
currentX = arg1.getX();
// going backwards: pushing stuff to the right
if (currentX - downXValue < -(arg0.getWidth()/3))
{
mdh.nextMonth();
calendar.add(Calendar.MONTH, 1);
currentMonth.setText(new SimpleDateFormat("MMMM yyyy").format(calendar.getTime()));
cAdapter.notifyDataSetChanged();
updateWeeks();
// Set the animation
vf.setInAnimation(arg0.getContext(), R.anim.push_left_in);
vf.setOutAnimation(arg0.getContext(), R.anim.push_left_out);
// Flip!
vf.showPrevious();
}
// going forwards: pushing stuff to the left
if (currentX - downXValue > arg0.getWidth()/3)
{
mdh.previousMonth();
calendar.add(Calendar.MONTH, -1);
currentMonth.setText(new SimpleDateFormat("MMMM yyyy").format(calendar.getTime()));
cAdapter.notifyDataSetChanged();
updateWeeks();
// Set the animation
vf.setInAnimation(arg0.getContext(), R.anim.push_right_in);
vf.setOutAnimation(arg0.getContext(), R.anim.push_right_out);
// Flip!
vf.showNext();
}
break;
}
gridview.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
return detector.onTouchEvent(event);
}
});
I had the same issue with a ViewFlipper and ScrollViews.
Try adding the onTouchListener to your GridView aswell and it should work.

Categories

Resources