[RK3399][Android7.1.1]Settings-语言设置添加遥控操作支持

测试平台

Platform: RK3399
OS: Android 7.1.1

需求

Android7.1.1加入了TVSettings用于支持遥控,但是Settings默认没有对遥控操作进行适配。语言设置是比较常用的设置项,当前的需求是为其添加遥控操作支持。

解决方案

From 62719811ce12ce94d7996974f6a24b8305d16dda Mon Sep 17 00:00:00 2001
From: sjf <sjf@t-chip.com.cn>
Date: Sat, 23 Dec 2017 17:27:13 +0800
Subject: [PATCH] =?UTF-8?q?Android->Settings-Language:=E8=AF=AD=E8=A8=80?=
 =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E6=B7=BB=E5=8A=A0=E9=81=A5=E6=8E=A7=E6=94=AF?=
 =?UTF-8?q?=E6=8C=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../apps/Settings/res/layout/locale_drag_cell.xml  |  3 ++-
 packages/apps/Settings/res/values/custom_ids.xml   |  6 +++++
 .../localepicker/LocaleDragAndDropAdapter.java     | 26 +++++++++++++++++++---
 .../settings/localepicker/LocaleListEditor.java    | 16 ++++++++++++-
 4 files changed, 46 insertions(+), 5 deletions(-)
 create mode 100644 packages/apps/Settings/res/values/custom_ids.xml

diff --git a/packages/apps/Settings/res/layout/locale_drag_cell.xml b/packages/apps/Settings/res/layout/locale_drag_cell.xml
index db9454a..beeced0 100644
--- a/packages/apps/Settings/res/layout/locale_drag_cell.xml
+++ b/packages/apps/Settings/res/layout/locale_drag_cell.xml
@@ -20,8 +20,9 @@
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:minHeight="?android:listPreferredItemHeight"
-              android:background="?android:colorBackground"
+              android:background="?android:attr/selectableItemBackground"
               android:layoutDirection="locale"
+              android:focusable="true"
               android:textDirection="locale">
 
     <CheckBox
diff --git a/packages/apps/Settings/res/values/custom_ids.xml b/packages/apps/Settings/res/values/custom_ids.xml
new file mode 100644
index 0000000..d9e2568
--- /dev/null
+++ b/packages/apps/Settings/res/values/custom_ids.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>  
+	<item type="id" name="tag_index"/>  
+	<item type="id" name="tag_feeditem">  
+</item>  
+</resources>  
\ No newline at end of file
diff --git a/packages/apps/Settings/src/com/android/settings/localepicker/LocaleDragAndDropAdapter.java b/packages/apps/Settings/src/com/android/settings/localepicker/LocaleDragAndDropAdapter.java
index 86498af..c1548c3 100644
--- a/packages/apps/Settings/src/com/android/settings/localepicker/LocaleDragAndDropAdapter.java
+++ b/packages/apps/Settings/src/com/android/settings/localepicker/LocaleDragAndDropAdapter.java
@@ -43,7 +43,16 @@ import java.util.Locale;
 
 
 class LocaleDragAndDropAdapter
-        extends RecyclerView.Adapter<LocaleDragAndDropAdapter.CustomViewHolder> {
+        extends RecyclerView.Adapter<LocaleDragAndDropAdapter.CustomViewHolder> implements View.OnClickListener {
+
+    public static interface OnItemClickListener {
+        void onItemClick(View view , int position);
+    }
+
+    private OnItemClickListener mOnItemClickListener = null;
+     public void setOnItemClickListener(OnItemClickListener listener) {
+        this.mOnItemClickListener = listener;
+    }
 
     private static final String TAG = "LocaleDragAndDropAdapter";
     private static final String CFGKEY_SELECTED_LOCALES = "selectedLocales";
@@ -144,9 +153,19 @@ class LocaleDragAndDropAdapter
     public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
         final LocaleDragCell item = (LocaleDragCell) LayoutInflater.from(mContext)
                 .inflate(R.layout.locale_drag_cell, viewGroup, false);
+        item.setOnClickListener(this);
         return new CustomViewHolder(item);
     }
 
+        @Override
+    public void onClick(View v) {
+        if (mOnItemClickListener != null) {
+            mOnItemClickListener.onItemClick(v,(int)(v.getTag(R.id.tag_index)));
+        }
+    }
+
+
     @Override
     public void onBindViewHolder(final CustomViewHolder holder, int i) {
         final LocaleStore.LocaleInfo feedItem = mFeedItemList.get(i);
@@ -160,13 +179,14 @@ class LocaleDragAndDropAdapter
         dragCell.setShowMiniLabel(!mRemoveMode);
         dragCell.setShowHandle(!mRemoveMode && mDragEnabled);
         dragCell.setChecked(mRemoveMode ? feedItem.getChecked() : false);
-        dragCell.setTag(feedItem);
+        dragCell.setTag(R.id.tag_feeditem,feedItem);
+        dragCell.setTag(R.id.tag_index, i);
         dragCell.getCheckbox()
                 .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                     @Override
                     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                         LocaleStore.LocaleInfo feedItem =
-                                (LocaleStore.LocaleInfo) dragCell.getTag();
+                                (LocaleStore.LocaleInfo) dragCell.getTag(R.id.tag_feeditem);
                         feedItem.setChecked(isChecked);
                     }
                 });
diff --git a/packages/apps/Settings/src/com/android/settings/localepicker/LocaleListEditor.java b/packages/apps/Settings/src/com/android/settings/localepicker/LocaleListEditor.java
index 281afa0..c9fc4a5 100644
--- a/packages/apps/Settings/src/com/android/settings/localepicker/LocaleListEditor.java
+++ b/packages/apps/Settings/src/com/android/settings/localepicker/LocaleListEditor.java
@@ -41,6 +41,9 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.Locale;
 
+import android.util.Log;
+import com.android.settings.localepicker.LocaleDragAndDropAdapter.OnItemClickListener;
+
 /**
  * Drag-and-drop editor for the user-ordered locale lists.
  */
@@ -69,7 +72,18 @@ public class LocaleListEditor extends SettingsPreferenceFragment
 
         LocaleStore.fillCache(this.getContext());
         final List<LocaleStore.LocaleInfo> feedsList = getUserLocaleList(this.getContext());
-        mAdapter = new LocaleDragAndDropAdapter(this.getContext(), feedsList);
+        mAdapter = new LocaleDragAndDropAdapter(this.getContext(), feedsList);    
+        mAdapter.setOnItemClickListener(new OnItemClickListener(){
+            @Override    
+            public void onItemClick(View view , int position){
+                if(position != 0)
+                {
+                    mAdapter.onItemMove(position, 0);
+                    mAdapter.doTheUpdate();
+                }
+            }
+        });
+
     }
 
     @Override
-- 
2.7.4

你可能感兴趣的:([RK3399][Android7.1.1]Settings-语言设置添加遥控操作支持)