从屏幕外的 listView 获取项目

Getting item from listView that is offscreen

当 ListView 不在屏幕上时,我无法从中获取项目。如果我向下滚动并点击下一个,则顶部项目 return 为 null。如果我向上滚动,底部的项目 return 为空。

我知道 ListView 会动态创建视图,所以是否可以将项目移出屏幕?

public void onNextClick(View w){
    Intent i = new Intent(this, homeActivity.class);

        for(int position = 0; position < expandableLayoutListView.getAdapter().getCount(); position++)
        {

            try {
                Hub temp = ((Hub) expandableLayoutListView.getAdapter().getItem(position));
                System.out.println("Position " + position);
                System.out.println("Other position " + (position - expandableLayoutListView.getFirstVisiblePosition()));

                ExpandableLayoutItem expandableLayout = (ExpandableLayoutItem)
                        expandableLayoutListView.getChildAt(position - expandableLayoutListView.getFirstVisiblePosition())
                                .findViewWithTag(ExpandableLayoutItem.class.getName());


                JoinedHub hub = new JoinedHub(temp.name);

                if(((CheckBox) expandableLayout.findViewById(R.id.checkBox1)).isChecked())
                    hub.addToSubHubs( ((CheckBox) expandableLayout.findViewById(R.id.checkBox1)).getText().toString()  );
                if(((CheckBox) expandableLayout.findViewById(R.id.checkBox2)).isChecked())
                    hub.addToSubHubs(((CheckBox) expandableLayout.findViewById(R.id.checkBox2)).getText().toString());
                if(((CheckBox) expandableLayout.findViewById(R.id.checkBox3)).isChecked())
                    hub.addToSubHubs(((CheckBox) expandableLayout.findViewById(R.id.checkBox3)).getText().toString());

                Log.d("Debugging position", temp.getName() + " " + position);

                if(!hub.subHubs.isEmpty()){
                    joinedHubs.add(hub);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    Log.d("Debugginggg", joinedHubs.toString());
    startActivity(i);

}

您无法获取屏幕外的项目,它们根本不存在(出于性能原因,这些视图会重新用于显示其他项目)。

This问题可能会帮助您实现您想要的。