在我的应用程序中,我在Listview中使用Gallery视图。滚动Listview项目显示第一个位置。在我的屏幕中意味着4行可见0,1,2,3。要查看我正在向上滚动的第五行。在这种情况下,第五个位置值显示为0而不是5.提前感谢任何建议。
public class TestprojectActivity extends Activity {
ListView listView;
LayoutInflater inflater;
int gloppos;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView=(ListView)findViewById(R.id.listView);
ladapter=new ListViewAdapter(this);
listView.setAdapter(ladapter);
}
private class ListViewAdapter extends BaseAdapter{
Context context;
public ListViewAdapter(Context context){
this.context=context;
inflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 30;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Gallery mygallery;
if(convertView==null){
convertView=inflater.inflate(R.layout.mygallary,null);
mygallery=(Gallery)convertView.findViewById(R.id.listgallary);
convertView.setTag(mygallery);
}else{
mygallery=(Gallery)convertView.getTag();
}
Toast.makeText(TestprojectActivity.this,"List pos "+position, Toast.LENGTH_SHORT ).show();
gloppos=position;
GallaryAdapter adpt=new GallaryAdapter(position);
mygallery.setAdapter(adpt);
return convertView;
}
}
private class GallaryAdapter extends BaseAdapter{
int listpos;
public GallaryAdapter(int pos){
listpos=pos;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 10;
}
@Override
public Object getItem(int c) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
convertView=inflater.inflate(R.layout.gallaryitems,null);
TextView text=(TextView)convertView.findViewById(R.id.txt);
text.setText("GALLARYPOS "+position);
TextView num=(TextView)convertView.findViewById(R.id.num);
num.setText("LSTVIEWPOS "+listpos);
}
Toast.makeText(TestprojectActivity.this,"calledin gallery "+gloppos, Toast.LENGTH_SHORT ).show();
return convertView;
}
}
}