0

如果没有像 list.onLoadListener 之类的事件(列表完成数据填充)如何访问列表的第一行?

由于列表重复使用它的行来提高性能,所以这个监听器不存在,这是可以理解的。但是当至少有一个项目(第一项,位置 0)时,我需要访问列表的第一项。

将适配器设置为列表后

list.getChildAt(0)   

返回我为空。那么我需要延迟访问第一项吗?

我们可以使用列表项单击侦听器访问列表项(该项目中的视图)。当我可以确定列表的第一项已填满时,我想使用 item。

我正在使用 TextureView 播放列表项中的视频。因此,一旦列表中填满了它的项目,我想自动播放第一个项目的视频。(无需任何点击或用户交互)。

这是我的代码:-

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.home);

    list = (ListView) v.findViewById(R.id.list);
            videoListDatas = new ArrayList<VideoListData>();
            adapter = new MyVideoListAdapterNew(getActivity(), videoListDatas);

            list.setAdapter(adapter);

            getVideoList(); //Method which get data from server

}

这里是 getVideoList() 方法实现

private void getVideoList() {
    final MyProgressDialog progressDialog = new MyProgressDialog(context);
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {

                // Implementation goes here fill array with data

            } catch (Exception e) {
                e.printStackTrace();
            }
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    adapter.notifyDataSetChanged();
                    progressDialog.dismiss();

                }

            });
        }

    }).start();
}

这是我的适配器

   public class MyVideoListAdapterNew extends BaseAdapter {

Context context;
private LayoutInflater inflater;

ArrayList<VideoListData> videoListDatas;


public MyVideoListAdapterNew(FragmentActivity fragmentActivity,
        ArrayList<VideoListData> videoListDatas) {
    context = fragmentActivity;
    this.videoListDatas = videoListDatas;
    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return videoListDatas.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return videoListDatas.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) {
    ViewHolder holder;

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.myvideo_row, null);

        holder = new ViewHolder();


        holder.flVideo = (FrameLayout) convertView
                .findViewById(R.id.flVideo);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();



    }

    try {

        //Filling views by getting values from array

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

if(position == 0 ){
    //Code to play first video automatically
            }else{
                     }

    return convertView;
}

private static class ViewHolder {

    FrameLayout flVideo;

}

}

我知道代码不会有太大帮助,但我只是根据某些人的建议发布它。

谢谢。

4

4 回答 4

0

您可以使用Custom Adapter列表视图,然后您可以截取public View getView(int position, View convertView, ViewGroup parent)方法中任何特定位置的视图。在这里你可以检查

if(position==0){
 //do your stuff
}

查看自定义适配器的此答案

于 2013-10-30T10:05:47.653 回答
0

你什么时候填写列表?您希望何时访问第一个元素?如果您正在使用具有 onViewCreated 和 onCreateView 方法的片段,您可以在 onCreateView 方法中填充列表视图并在 onViewCreated 方法中访问它。与活动不同,您可以在 onCreate() 中填充列表,然后在 onStart() 或 onResume() 访问第一个元素。

于 2013-10-30T10:06:55.747 回答
0

不知道这是否是最好的方法,但您可以尝试一下。

创建一个看起来像这样的监听器

public interface ViewLoadedInterface {
    public void onFirstViewLoaded(/**add params if you like*/);
}

将其传递Interface给您的Adapter. 您仅在返回onFirstViewLoaded()第一个方法时调用该方法View,然后再为该实例再次调用它,否则在回收开始时您将遇到一些问题。

当它被调用时,您然后list.getChildAt(0)在接口的实现中执行。

所以最终实现看起来像这样

public class YourActivityClass extends Activity implements ViewLoadedInterface {
    ...

    @Override
    public void onFirstViewLoaded() {
        ...

        something = list.getChildAt(0);

        ...
    }
}
于 2013-10-30T10:23:39.937 回答
0

我从这个线程中找到了这种情况的解决方案:- https://stackoverflow.com/a/6944587/647014

我的要求是在列表完成数据加载后立即访问列表的第一项。这种方式列表等到其数据被刷新/加载。

谢谢。

于 2013-10-31T09:23:37.740 回答