在我的应用程序中,我在 Listview 中使用 Gallery 视图。而滚动 Listview 项目显示第一个位置。意味着在我的屏幕上 4 行可见 0,1,2,3。要查看第五行,我正在向上滚动。在这种情况下,第 5 个位置值显示为 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;
}
}
}