]> gitweb.fperrin.net Git - Dictionary.git/commitdiff
Put language buttons in Layout for better performance.
authorReimar Döffinger <Reimar.Doeffinger@gmx.de>
Mon, 11 Dec 2017 00:19:25 +0000 (01:19 +0100)
committerReimar Döffinger <Reimar.Doeffinger@gmx.de>
Mon, 11 Dec 2017 00:19:25 +0000 (01:19 +0100)
res/layout/dictionary_manager_row.xml
src/com/hughes/android/dictionary/DictionaryManagerActivity.java
src/com/hughes/android/dictionary/IsoUtils.java

index eeaf5ed29d0a16cba3e450a5df4dfa77a724604f..8889d45d6357ee37865926e0f495d8a9f383a3e0 100644 (file)
             android:layout_height="wrap_content"
             android:orientation="horizontal"
             android:paddingRight="5dip" >
+            <Button android:layout_width="60dip" android:layout_height="40dip" />
+            <ImageButton android:layout_width="60dip" android:layout_height="40dip" />
+            <Button android:layout_width="60dip" android:layout_height="40dip" />
+            <ImageButton android:layout_width="60dip" android:layout_height="40dip" />
         </LinearLayout>
 
         <!--
index ca0c5e1dd7573c98466c0e3eda8be972af20befe..694f8da0ebc4ed6b8c105441bc6efb5443df921e 100644 (file)
@@ -60,6 +60,7 @@ import android.widget.Button;
 import android.widget.CompoundButton;
 import android.widget.CompoundButton.OnCheckedChangeListener;
 import android.widget.FrameLayout;
+import android.widget.ImageButton;
 import android.widget.LinearLayout;
 import android.widget.ListAdapter;
 import android.widget.ListView;
@@ -618,6 +619,7 @@ public class DictionaryManagerActivity extends ActionBarActivity {
             if (row.dictionaryInfo == null) {
                 return row.onDevice ? 0 : 1;
             }
+            assert row.dictionaryInfo.indexInfos.size() <= 2;
             return 2;
         }
 
@@ -652,10 +654,6 @@ public class DictionaryManagerActivity extends ActionBarActivity {
         if (row == null) {
             row = LayoutInflater.from(parent.getContext()).inflate(
                        R.layout.dictionary_manager_row, parent, false);
-        } else {
-            // TODO: avoid this
-            LinearLayout buttons = (LinearLayout) row.findViewById(R.id.dictionaryLauncherButtons);
-            buttons.removeAllViews();
         }
         final TextView name = (TextView) row.findViewById(R.id.dictionaryName);
         final TextView details = (TextView) row.findViewById(R.id.dictionaryDetails);
@@ -681,21 +679,31 @@ public class DictionaryManagerActivity extends ActionBarActivity {
                     downloadDictionary(downloadable.downloadUrl, downloadable.zipBytes, downloadButton);
                 }
             });
+            downloadButton.setVisibility(View.VISIBLE);
         } else {
-            downloadButton.setVisibility(View.INVISIBLE);
+            downloadButton.setVisibility(View.GONE);
         }
 
         LinearLayout buttons = (LinearLayout) row.findViewById(R.id.dictionaryLauncherButtons);
+
         final List<IndexInfo> sortedIndexInfos = application
                 .sortedIndexInfos(dictionaryInfo.indexInfos);
         final StringBuilder builder = new StringBuilder();
         if (updateAvailable) {
             builder.append(getString(R.string.updateAvailable));
         }
-        for (IndexInfo indexInfo : sortedIndexInfos) {
-            final View button = IsoUtils.INSTANCE.createButton(buttons.getContext(), dictionaryInfo,
+        assert buttons.getChildCount() == 4;
+        for (int i = 0; i < 2; i++) {
+            final Button textButton = (Button)buttons.getChildAt(2*i);
+            final ImageButton imageButton = (ImageButton)buttons.getChildAt(2*i + 1);
+            if (i >= sortedIndexInfos.size()) {
+                textButton.setVisibility(View.GONE);
+                imageButton.setVisibility(View.GONE);
+                continue;
+            }
+            final IndexInfo indexInfo = sortedIndexInfos.get(i);
+            final View button = IsoUtils.INSTANCE.setupButton(textButton, imageButton, dictionaryInfo,
                                 indexInfo, application.languageButtonPixels);
-            buttons.addView(button);
 
             if (canLaunch) {
                 button.setOnClickListener(
@@ -704,10 +712,9 @@ public class DictionaryManagerActivity extends ActionBarActivity {
                                                application.getPath(dictionaryInfo.uncompressedFilename),
                                                indexInfo.shortName, "")));
 
-            } else {
-                button.setEnabled(false);
-                button.setFocusable(false);
             }
+            button.setEnabled(canLaunch);
+            button.setFocusable(canLaunch);
             if (builder.length() != 0) {
                 builder.append("; ");
             }
index 4cafedc11bee6f049a95b4f42da22a8ee94d2dc4..9b381fbb456384ec28aacc3d891487ead984cd88 100644 (file)
@@ -234,4 +234,26 @@ public enum IsoUtils {
         result.setLayoutParams(new LinearLayout.LayoutParams(size, size * 2 / 3));
         return result;
     }
+
+    public View setupButton(Button textButton, ImageButton imageButton,
+                            final DictionaryInfo dictionaryInfo,
+                            final IndexInfo indexInfo, int size) {
+        LanguageResources languageResources = isoCodeToResources.get(indexInfo.shortName);
+        View result;
+
+        if (languageResources == null || languageResources.flagId <= 0) {
+            textButton.setText(indexInfo.shortName);
+            textButton.setVisibility(View.VISIBLE);
+            imageButton.setVisibility(View.GONE);
+            result = textButton;
+        } else {
+            imageButton.setImageResource(languageResources.flagId);
+            imageButton.setScaleType(ImageView.ScaleType.FIT_CENTER);
+            textButton.setVisibility(View.GONE);
+            imageButton.setVisibility(View.VISIBLE);
+            result = imageButton;
+        }
+        // Assume dimensions have been set before in Layout
+        return result;
+    }
 }