android - listview 按位置获取项目视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24811536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:47:48  来源:igfitidea点击:

android - listview get item view by position

androidandroid-listviewandroid-viewandroid-adapter

提问by alashow

I have listview with custom adapter (base adapter). I want to get view from listview by position. I tried mListView.getChildAt(position), but it is not working. How can i get item view by position?

我有带有自定义适配器(基本适配器)的列表视图。我想按位置从列表视图中获取视图。我试过了mListView.getChildAt(position),但它不起作用。如何按位置获取项目视图?

回答by VVB

Use this :

用这个 :

public View getViewByPosition(int pos, ListView listView) {
    final int firstListItemPosition = listView.getFirstVisiblePosition();
    final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;

    if (pos < firstListItemPosition || pos > lastListItemPosition ) {
        return listView.getAdapter().getView(pos, null, listView);
    } else {
        final int childIndex = pos - firstListItemPosition;
        return listView.getChildAt(childIndex);
    }
}

回答by Matt Twig

You can get only visible View from ListView because row views in ListView are reuseable. If you use mListView.getChildAt(0)you get first visible view. This view is associated with item from adapter at position mListView.getFirstVisiblePosition().

您只能从 ListView 获取可见视图,因为 ListView 中的行视图是可重用的。如果您使用,mListView.getChildAt(0)您将获得第一个可见视图。此视图与来自适配器位置的项目相关联mListView.getFirstVisiblePosition()

回答by C0D3LIC1OU5

Preferred way to change the appearance/whatever of row views once the ListView is drawn is to change something in the data ListView draws from (the array of objects that is passed into your Adapter) and make sure to account for that in your getView() function, then redraw the ListView by calling

绘制 ListView 后更改外观/任何行视图的首选方法是更改​​ ListView 从(传递到您的适配器的对象数组)绘制的数据中的某些内容,并确保在您的 getView() 中考虑到这一点函数,然后通过调用重绘 ListView

notifyDataSetChanged();

EDIT: while there is a way to do this, if you need to do this chances are doing something wrong. While are few edge cases I can think about, generally using notifyDataSetChanged()and other built in mechanisms is a way to go.

编辑:虽然有一种方法可以做到这一点,但如果您需要这样做,很可能会做错事。虽然我能想到的边缘情况很少,但通常使用notifyDataSetChanged()和其他内置机制是一种方法。

EDIT 2: One of the common mistakes people make is trying to come up with their own way to respond to user clicking/selecting a row in the ListView, as in one of the comments to this post. There is an existing way to do this. Here's how:

编辑 2:人们常犯的错误之一是试图想出自己的方式来响应用户在 ListView 中单击/选择一行,如对这篇文章的评论之一。有一种现有的方法可以做到这一点。就是这样:

mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    /* Parameters
    parent:     The AdapterView where the click happened.
    view:       The view within the AdapterView that was clicked (this will be a view provided by the adapter)
    position:   The position of the view in the adapter.
    id:         The row id of the item that was clicked. */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //your code here
    }
});

ListViewhas a lot of build-in functionality and there is no need to reinvent the wheel for simpler cases. Since ListView extends AdapterView, you can set the same Listeners, such as OnItemClickListeneras in the example above.

ListView有很多内置功能,无需为更简单的情况重新发明轮子。由于 ListView 扩展了AdapterView,您可以设置相同的侦听器,例如上面示例中的OnItemClickListener

回答by Mohamed Ayed

workignHoursListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent,View view, int position, long id) {
        viewtype yourview=yourListViewId.getChildAt(position).findViewById(R.id.viewid);
    }
});

回答by arjun

Listview lv = (ListView) findViewById(R.id.previewlist);

    final BaseAdapter adapter = new PreviewAdapter(this, name, age);

    confirm.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub


            View view = null;

            String value;
            for (int i = 0; i < adapter.getCount(); i++) {

                view = adapter.getView(i, view, lv);

                Textview et = (TextView) view.findViewById(R.id.passfare);


                value=et.getText().toString();

                 Toast.makeText(getApplicationContext(), value,
                 Toast.LENGTH_SHORT).show();
            }



        }
    });