在android中加载列表视图时访问列表的第一项

时间:2013-10-30 09:51:19

标签: android listview

如果没有像list.onLoadListener这样的事件(列表是否填充了数据)如何访问列表的第一行?

当列表重用其行以获得性能时,此侦听器不可理解。但是,当至少有一个项目(第一个项目,位置0)时,我需要访问列表的第一个项目。

将适配器设置为列表

list.getChildAt(0)   

返回null。那么我是否需要延迟访问第一项?

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

我正在使用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 个答案:

答案 0 :(得分:0)

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

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

查看此answer以获取自定义适配器

答案 1 :(得分:0)

您何时填充列表?您何时想要访问第一个元素?如果您正在使用onViewCreated,onCreateView方法的片段,则可以在onCreateView方法中填充listview并在onViewCreated方法中访问它。对于不同的活动,您可以在onCreate()中填充列表,然后在onStart()或onResume()访问第一个元素。

答案 2 :(得分: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);

        ...
    }
}

答案 3 :(得分:0)

我从这个帖子找到了解决这种情况的方法: - https://stackoverflow.com/a/6944587/647014

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

感谢。