我在使用自定义值的NumberPicker时遇到问题。打开对话框时,我通过在自定义数组数组中查找索引并将NumberPicker的值设置为此索引来获取要在NumberPicker中显示的值。跟踪显示在NumberPicker.Value中找到并设置了正确的索引;但是,NumberPicker显示不同索引的值。
在附图中,我的值为1/4,但显示为1/45。如果我选择1/6的值,它将显示为1/60。值为1,显示为1/1000。 如果我选择它,正确的索引保存在NumberPicker.Value中,所以我得到了所需的值,只是显示混乱,似乎选择了一个与所需值紧密匹配的东西(1/4为1/45, 1/60等1/60,并忽略我设置的数组中的索引。 如果我选择值2,3等,则NumberPicker中会显示正确的值。
以下是相关代码,问题是在模拟器和真实的Android设备上。
public class MeasuredExposureDialog : Android.Support.V4.App.DialogFragment
{
private ReciprocityAdapter _mAdapter = null;
private string[] shutterSpeeds = { "1/1000", "1/750", "1/500", "1/350", "1/180", "1/125", "1/90", "1/60", "1/45", "1/30", "1/20", "1/15", "1/10", "1/8", "1/6", **"1/4"**, "1/3", "1/2", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"
, "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40"
, "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70"
, "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100"
, "101", "102", "103", "104", "105", "106", "107", "108", "109", "110", "111", "112", "113", "114", "115", "116", "117", "118", "119", "120", "121", "122", "123", "124", "125", "126", "127", "128", "129", "130"
, "131", "132", "133", "134", "135", "136", "137", "138", "139", "140", "141", "142", "143", "144", "145", "146", "147", "148", "149", "150", "151", "152", "153", "154", "155", "156", "157", "158", "159", "160"
, "161", "162", "163", "164", "165", "166", "167", "168", "169", "170", "171", "172", "173", "174", "175", "176", "177", "178", "179", "180", "181", "182", "183", "184", "185", "186", "187", "188", "189", "190"
, "191", "192", "193", "194", "195", "196", "197", "198", "199", "200", "201", "202", "203", "204", "205", "206", "207", "208", "209", "210", "211", "212", "213", "214", "215", "216", "217", "218", "219", "220"
};
public static MeasuredExposureDialog NewInstance(Bundle bundle)
{
MeasuredExposureDialog fragment = new MeasuredExposureDialog();
fragment.Arguments = bundle;
return fragment;
}
public ReciprocityAdapter Adapter
{
set{ _mAdapter = value; }
}
public override Dialog OnCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder builder = new AlertDialog.Builder(Activity);
// Get the layout inflater
LayoutInflater inflater = Activity.LayoutInflater;
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
View view = inflater.Inflate(Resource.Layout.numberpicker, null);
builder.SetView(view);
builder.SetTitle("Measured exposure");
NumberPicker numberPicker = view.FindViewById<NumberPicker>(Resource.Id.numberPicker);
numberPicker.MaxValue = shutterSpeeds.Length - 1;
numberPicker.MinValue = 0;
numberPicker.SetDisplayedValues(shutterSpeeds);
numberPicker.WrapSelectorWheel = true;
string value = _mAdapter.GetEntrySetting(4);
Log.Info("myapp", "Setting stored value: " + value);
for (int i = 0; i < shutterSpeeds.Length; i++)
{
if (shutterSpeeds[i] == value)
{
numberPicker.Value = i;
Log.Info("myapp", "Setting value to index: " + i);
break;
}
}
//numberPicker.ClearFocus();
// Add action buttons
builder.SetPositiveButton("Select", (senderAlert, args) = >
{
_mAdapter.SetEntry(4, shutterSpeeds[numberPicker.Value]);
_mAdapter.NotifyDataSetChanged();
});
builder.SetNegativeButton("Cancel", (senderAlert, args) = >
{
});
return builder.Create();
}
}
适配器代码:
using Android.Content;
using Android.Graphics;
using Android.Support.V7.Widget;
using Android.Views;
using Android.Widget;
using System;
using System.Collections.Generic;
namespace Reciprocity.Fragments
{
public class ReciprocityAdapter : RecyclerView.Adapter
{
private View _itemView = null;
private List<Entry> _entryList;
public string GetEntrySetting(int position)
{
return _entryList[position].setting;
}
public void StoreEnvironment()
{
//store
var prefs = Android.App.Application.Context.GetSharedPreferences("Reciprocity2", FileCreationMode.Private);
var prefEditor = prefs.Edit();
if (_entryList[0].setting != "Tap to select a film")
prefEditor.PutString("selectedFilm", _entryList[0].setting);
prefEditor.PutString("measuredSeconds", _entryList[4].setting);
prefEditor.PutBoolean("useBGR", true);
prefEditor.PutString("filterFactor", _entryList[1].setting);
prefEditor.PutString("lensExtension", _entryList[3].setting);
prefEditor.PutString("focalLength", _entryList[2].setting);
prefEditor.Commit();
}
public void RestoreEnvironment()
{
var prefs = Android.App.Application.Context.GetSharedPreferences("Reciprocity2", FileCreationMode.Private);
var selectedFilm = prefs.GetString("selectedFilm", null);
if (selectedFilm != null)
{
_entryList[0].setting = selectedFilm;
}
_entryList[4].setting = prefs.GetString("measuredSeconds", "Tap to select");
_entryList[1].setting = prefs.GetString("filterFactor", "Tap to select");
_entryList[3].setting = prefs.GetString("lensExtension", "Tap to select");
_entryList[2].setting = prefs.GetString("focalLength", "Tap to select");
}
public ReciprocityAdapter(List<Entry> entries)
{
_entryList = entries;
}
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
// Inflate the CardView for the photo:
_itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.cardview, parent, false);
// Create a ViewHolder to hold view references inside the CardView:
ReciprocityViewHolder vh = new ReciprocityViewHolder(_itemView, OnClick);
return vh;
}
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
ReciprocityViewHolder vh = holder as ReciprocityViewHolder;
vh.category.Text = _entryList[position].category;
vh.setting.Text = _entryList[position].setting;
vh.image.SetImageResource(_entryList[position].colorbar);
vh.image.SetAdjustViewBounds(true);
CardView cv = vh.ItemView.FindViewById<CardView>(Resource.Id.cv);
cv.SetCardBackgroundColor((position == 5) ? Color.Yellow : Color.White);
}
private void OnClick(int position)
{
if (ItemClick != null)
ItemClick(this, position);
}
public override int ItemCount
{
get
{
return _entryList.Count;
}
}
public event EventHandler<int> ItemClick;
}
public class ReciprocityViewHolder : RecyclerView.ViewHolder
{
public ImageView image { get; private set; }
public TextView category { get; private set; }
public TextView setting { get; private set; }
public ReciprocityViewHolder(View itemView, Action<int> listener)
: base(itemView)
{
image = itemView.FindViewById<ImageView>(Resource.Id.colorBar);
setting = itemView.FindViewById<TextView>(Resource.Id.reciprocitySetting);
category = itemView.FindViewById<TextView>(Resource.Id.reciprocityCategory);
itemView.Click += (sender, e) => listener(base.AdapterPosition);
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<Space
android:layout_width="1dp"
android:layout_height="10dp" />
<NumberPicker
android:id="@+id/numberPicker"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="top|center_horizontal"
android:descendantFocusability="blocksDescendants"
android:gravity="center" />
<Space
android:layout_width="1dp"
android:layout_height="15dp" />
</LinearLayout>
输出如下:
Setting stored value: 1/4
Setting value to index: 15
它在对话框中显示的方式:
知道发生了什么事吗?