[Android] Auto-hide keyboard while scrolling suggestions

Geoffrey Métais git at videolan.org
Wed Mar 4 16:46:24 CET 2015


vlc-ports/android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Wed Mar  4 16:45:41 2015 +0100| [913c6a134a1e23c8504a44d0cf28c0e1ae7bfe5a] | committer: Geoffrey Métais

Auto-hide keyboard while scrolling suggestions

> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=913c6a134a1e23c8504a44d0cf28c0e1ae7bfe5a
---

 .../src/org/videolan/vlc/gui/MainActivity.java     |    9 +++++++-
 .../videolan/vlc/gui/SearchSuggestionsAdapter.java |   23 +++++++++++++++-----
 2 files changed, 26 insertions(+), 6 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/MainActivity.java b/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
index 658e5a1..164309d 100644
--- a/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
@@ -53,9 +53,11 @@ import android.view.LayoutInflater;
 import android.view.Menu;
 import android.view.MenuInflater;
 import android.view.MenuItem;
+import android.view.MotionEvent;
 import android.view.View;
 import android.view.Window;
 import android.view.WindowManager;
+import android.view.inputmethod.InputMethodManager;
 import android.widget.AdapterView;
 import android.widget.AdapterView.OnItemClickListener;
 import android.widget.ListView;
@@ -92,7 +94,7 @@ import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
 
-public class MainActivity extends ActionBarActivity implements OnItemClickListener, SearchView.OnQueryTextListener {
+public class MainActivity extends ActionBarActivity implements OnItemClickListener, SearchView.OnQueryTextListener, SearchSuggestionsAdapter.SuggestionDisplay {
     public final static String TAG = "VLC/MainActivity";
 
     protected static final String ACTION_SHOW_PROGRESSBAR = "org.videolan.vlc.gui.ShowProgressBar";
@@ -856,6 +858,11 @@ public class MainActivity extends ActionBarActivity implements OnItemClickListen
         }
     };
 
+    public void hideKeyboard(){
+        ((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
+                getWindow().getDecorView().getRootView().getWindowToken(), 0);
+    }
+
     public static void showProgressBar() {
         Intent intent = new Intent();
         intent.setAction(ACTION_SHOW_PROGRESSBAR);
diff --git a/vlc-android/src/org/videolan/vlc/gui/SearchSuggestionsAdapter.java b/vlc-android/src/org/videolan/vlc/gui/SearchSuggestionsAdapter.java
index d0217fb..1c7c7b7 100644
--- a/vlc-android/src/org/videolan/vlc/gui/SearchSuggestionsAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/SearchSuggestionsAdapter.java
@@ -26,6 +26,7 @@ import android.content.Context;
 import android.database.Cursor;
 import android.support.v4.widget.CursorAdapter;
 import android.view.LayoutInflater;
+import android.view.MotionEvent;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.TextView;
@@ -41,9 +42,15 @@ public class SearchSuggestionsAdapter extends CursorAdapter {
     public final static String TAG = "VLC/SearchSuggestionsAdapter";
 
     MediaLibrary mMediaLibrary = MediaLibrary.getInstance();
+    SuggestionDisplay activity;
+
+    public interface SuggestionDisplay {
+        public void hideKeyboard();
+    }
 
     public SearchSuggestionsAdapter(Context context, Cursor cursor){
         super(context, cursor, false);
+        activity = (SuggestionDisplay) context;
     }
 
     @Override
@@ -54,17 +61,23 @@ public class SearchSuggestionsAdapter extends CursorAdapter {
     }
 
     @Override
-    public void bindView(View view, final Context context, final Cursor cursor) {
-        final int position = cursor.getPosition();
+    public void bindView(View view, final Context context, Cursor cursor) {
+        final String location = cursor.getString(cursor.getColumnIndex(MediaDatabase.MEDIA_LOCATION));
         TextView tv = (TextView) view.findViewById(android.R.id.text1);
         tv.setText(cursor.getString(cursor.getColumnIndex(MediaDatabase.MEDIA_TITLE)));
         tv.setBackgroundColor(Util.getColorFromAttribute(context, R.attr.background_menu));
         tv.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
-                cursor.moveToPosition(position);
-                MediaWrapper media = mMediaLibrary.getMediaItem(cursor.getString(cursor.getColumnIndex(MediaDatabase.MEDIA_LOCATION)));
-                Util.openMedia(context, media);
+                Util.openMedia(context, mMediaLibrary.getMediaItem(location));
+            }
+        });
+        view.setOnTouchListener(new View.OnTouchListener() {
+            @Override
+            public boolean onTouch(View v, MotionEvent event) {
+                if (event.getAction() == MotionEvent.ACTION_DOWN)
+                    activity.hideKeyboard();
+                return false;
             }
         });
     }



More information about the Android mailing list