我正在使用列表视图来显示数字数组。在点击事件中,我正在使用数字选择器,我的目的是显示一个可以实时更改数字的数字选择器[可以在滚动数字选择器时在列表的文本视图中看到]。
如果我点击顶部对象,数字选择器应显示在底部,反之亦然。问题是:如果列表大于屏幕大小,它启用滚动,点击事件焦点刷新,数字选择器根据刷新的屏幕状态。此外,用户无法看到最后一个号码更改。数字实际上正在发生变化,但用户在通过数字选择器进行更改时无法看到它。
假设我的数组大小为50,屏幕一次显示10个数字。在单击事件屏幕上选择第45个元素时,焦点将转到前10个元素。我希望屏幕焦点留在那里。 MainActivity.java
public class MainActivity extends Activity {
Dialog Number_Picker_Dialog;
Display display;
ArrayList<DataValues> dataValue = new ArrayList<DataValues>();
public ListView list;
int checkorient;
int widthOfScreen,heightOfScreen;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
display=((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
getActionBar().hide();
setContentView(R.layout.audiogram);
DataObjects();
display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
widthOfScreen = size.x;
heightOfScreen = size.y;
checkorient = getscrOrientation();
switch(checkorient){
case 0: Log.i("Orientation","Portrait "+checkorient);break;
case 1: Log.i("Orientation","Landscape "+checkorient);break;
default:Log.i("Orientation","Undefined"); break;}
update();//update method call
}
public void DataObjects() //Default Values
{
DataValues obj1= new DataValues();
obj1.setValue1(2500);
obj1.setValue2(25);
dataValue.add(obj1);
DataValues obj2= new DataValues();
obj2= new DataValues();
obj2.setValue1(2500);
obj2.setValue2(25);
dataValue.add(obj2);
DataValues obj3= new DataValues();
obj3= new DataValues();
obj3.setValue1(2500);
obj3.setValue2(25);
dataValue.add(obj3);
DataValues obj4= new DataValues();
obj4= new DataValues();
obj4.setValue1(2500);
obj4.setValue2(25);
dataValue.add(obj4);
DataValues obj5= new DataValues();
obj5= new DataValues();
obj5.setValue1(2500);
obj5.setValue2(25);
dataValue.add(obj5);
DataValues obj6= new DataValues();
obj6= new DataValues();
obj6.setValue1(2500);
obj6.setValue2(25);
dataValue.add(obj6);
DataValues obj7= new DataValues();
obj7= new DataValues();
obj7.setValue1(2500);
obj7.setValue2(25);
dataValue.add(obj7);
DataValues obj8= new DataValues();
obj8= new DataValues();
obj8.setValue1(2500);
obj8.setValue2(25);
dataValue.add(obj8);
DataValues obj9= new DataValues();
obj9= new DataValues();
obj9.setValue1(2500);
obj9.setValue2(25);
dataValue.add(obj9);
DataValues obj10= new DataValues();
obj10= new DataValues();
obj10.setValue1(2500);
obj10.setValue2(25);
dataValue.add(obj10);
DataValues obj11= new DataValues();
obj11= new DataValues();
obj11.setValue1(2500);
obj11.setValue2(25);
dataValue.add(obj11);
}
public void update(){
CustomBaseAdapter cBaseAdapter = new CustomBaseAdapter(getApplicationContext(), dataValue);
list=(ListView)findViewById(R.id.list);
list.setAdapter(cBaseAdapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
int[] acord;
acord = new int[2];
view.getLocationOnScreen(acord);
Log.i("adapter"," x "+acord[0]+" y "+acord[1]);
onDialogItemClick(position,acord);
}
});
}
public int getscrOrientation()
{
Display getOrient = getWindowManager().getDefaultDisplay();
int orientation = getOrient.getOrientation();
return orientation; // return value 0 is portrait and 1 is Landscape Mode
}
public void onDialogItemClick(final int position,int[] acord){
//Num picker dialog Properties
Number_Picker_Dialog=new Dialog(this);
Number_Picker_Dialog.setContentView(R.layout.num);
Number_Picker_Dialog.setTitle("Frequencies(value)");
Number_Picker_Dialog.setCanceledOnTouchOutside(true);//on background click check
final NumberPicker np1 = (NumberPicker) Number_Picker_Dialog.findViewById(R.id.pick);
int NUMBER_OF_VALUES = 51; //int gravity=position+1;
np1.setMaxValue(NUMBER_OF_VALUES-1);
np1.setMinValue(0);
np1.setBackgroundColor(1);
np1.setWrapSelectorWheel(true);
//===================================================================================================
Window window = Number_Picker_Dialog.getWindow();
window.setDimAmount(0f); // -- Set window dim level
//WindowManager.LayoutParams params = Number_Picker_Dialog.getWindow().getAttributes();
LayoutParams lp = Number_Picker_Dialog.getWindow().getAttributes();
switch(checkorient){
case 0: lp.width = getResources().getDimensionPixelSize(R.dimen.picker_width);
lp.height = getResources().getDimensionPixelSize(R.dimen.picker_height);
//Toast.makeText(getApplicationContext(), "Orientation"+checkorient, Toast.LENGTH_SHORT).show();
Number_Picker_Dialog.getWindow().setAttributes(lp);
Log.i("Numpicker","Portrait Desgin");break;
case 1: lp.width = getResources().getDimensionPixelSize(R.dimen.picker2_width);
lp.height = getResources().getDimensionPixelSize(R.dimen.picker2_height);
//Toast.makeText(getApplicationContext(), "Orientation"+checkorient, Toast.LENGTH_SHORT).show();
Number_Picker_Dialog.getWindow().setAttributes(lp);
Log.i("Numpicker","Landscape Design");break;
default:Log.i("Orientation","Undefined"); break;}
//--------------------Conditions-----------------------
//new algorithm required**16-12-13
int io = heightOfScreen/2;
int x=acord[0],y=acord[1];
int io2 = io/2;
while(y<=heightOfScreen){
if( y<=io)//Top to center click
{
window.setGravity(Gravity.BOTTOM);
Log.i("GsetN","Top Click"+"pos: "+position+" x "+x+" y "+y);
break;
}
//Center to Bottom click
if( y>io)
{
Log.i("GsetN","Center Click"+"pos: "+position+" x "+x+" y "+y);
window.setGravity(Gravity.TOP);
break;}
else {window.setGravity(Gravity.CENTER_VERTICAL);Log.v("GsetN","No Case detect for numpicker position");break;}
}
//======================================================================================================================
final String[] displayedValues = new String[NUMBER_OF_VALUES];
for(int i=0,j=0;i<NUMBER_OF_VALUES;i++,j+=5){
displayedValues[i] = Integer.toString(j);}
np1.setDisplayedValues(displayedValues);
//======================================================================================================================
//on value change listner for changing values
np1.setOnValueChangedListener( new NumberPicker.
OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int
oldVal, int newVal) {
newVal = Integer.parseInt(displayedValues[np1.getValue()]);
DataValues obj = dataValue.get(position);
obj.setValue2(newVal);//choosen value loaded in the object
update();//update method call-->Recursion
}
});
//=================================================================================================================
Number_Picker_Dialog.show();
} //onitem dialog click ends here
public void back(View back){
//Put here the Parent class name
//Intent bb=new Intent(getApplicationContext(),Parent.class);
//startActivity(bb);
finish();//currently set to close
}
}
//CustomBaseAdapter.java
public class CustomBaseAdapter extends BaseAdapter {
private Context context = null;
private ArrayList<DataValues> data = null;
private LayoutInflater inflater = null;
public CustomBaseAdapter(Context appContext, ArrayList<DataValues> dataValue)
{
context = appContext;
data = dataValue;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return data.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView= inflater.inflate(R.layout.list_single, null, true);
TextView txtView_1 = (TextView) rowView.findViewById(R.id.TV1);
TextView txtView_2 = (TextView) rowView.findViewById(R.id.TV2);
DataValues dataVal = data.get(position);
txtView_1.setText(""+dataVal.getValue1());
txtView_2.setText(""+dataVal.getValue2());
return rowView;//if row View is null may cause nExp
}
}
//-----------------DataValues.java-------//
public class DataValues {
private int value1;
private int value2;
public int getValue1() {
return value1;
}
public void setValue1(int value1) {
this.value1 = value1;
}
public int getValue2() {
return value2;
}
public void setValue2(int value2) {
this.value2 = value2;
}
}