如何从android中的子适配器获取父位置?

时间:2015-01-21 07:33:19

标签: android listview position

大家好! 我的问题是:父适配器是ListAdapter,子适配器是GalleryAdapter。需要点击项目库,显示(真实)图像,但我无法获得正确的lPosition,即ListAdapter(父适配器)的位置。

public class PostListAdapter ex...{
     int lPosition;
     int lPosition=??? 
     .....
     public View getView..{
          ...
          gallery.setOnItemClickListener(new OnItemClickListener() {
               @Override
               public void onItemClick(AdapterView<?> parent, View view, int gPosition, long id) {

               activity.mainFragment.startImagePagerActivity(gPosition,lPosition);
               }
          });
     }
}

1 个答案:

答案 0 :(得分:1)

使用setTag()将lPosition添加到您正在创建(或回收)的每个视图中,然后在onItemClick调用中检索

public class PostListAdapter ex...{
     int lPosition;
     int lPosition=??? 
     .....
     public View getView..{
          ...
          gallery.setTag(lPosition);
          gallery.setOnItemClickListener(new OnItemClickListener() {
               @Override
               public void onItemClick(AdapterView<?> parent, View view, int gPosition, long id) {
               int lPos = (int)parent.getTag();
               activity.mainFragment.startImagePagerActivity(gPosition,lPos);
         }
     });
     }
}