Android列表视图在滚动时获得最中心视图位置

时间:2017-04-05 11:38:13

标签: android listview

我正在尝试获取listview的大多数中心元素视图位置。

listview center view

 public void getPos(final ListView listView) {
        int firstPos = listView.getFirstVisiblePosition();
        int lastPos = listView.getLastVisiblePosition();
        int count = lastPos - firstPos;
        int viewPosition = (count - ((int) count / 2));
        //int viewPosition = (count - ((int) count / 2)+1);
}

并尝试使用屏幕x,y

Display display = getWindowManager().getDefaultDisplay();
        final Point size = new Point();
        display.getSize(size);
        float height = size.y;
        float centerY=height/2;

    listView.setOnScrollListener(new AbsListView.OnScrollListener() {
        public void onScroll(final AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            // TODO Auto-generated method stub

        }

        public void onScrollStateChanged(AbsListView view, int scrollState) {
            //                Log.i("Yes1", "Here");
            if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                if (scrollState == SCROLL_STATE_IDLE) {
                    final ListView listView = (ListView) view;
                    for (int i = 0; i < listView.getLastVisiblePosition(); i++) {
                        final View firstView = listView.getChildAt(i);
                        if (firstView != null) {
                            Log.i("Height", centerY + " | " +   firstView.getY());
                        }
                    }
                }
            }
        }
    });

我已尝试过上述方法,但在向上和向下滚动时无法获得准确的结果。任何人都有解决方案如何在列表视图中获得最中心的视图位置?

1 个答案:

答案 0 :(得分:0)

试试这个:

             String[] values = new String[]{"Android List View",
                        "Adapter implementation",
                        "Simple List View In Android",
                        "Create List View Android",
                        "Android Example",
                        "List View Source Code",
                        "List View Array Adapter",
                        "Adapter implementation",
                        "Simple List View In Android",
                        "Create List View Android",
                        "Android Example",
                        "List View Source Code",
                        "List View Array Adapter",
                        "Adapter implementation",
                        "Simple List View In Android",
                        "Create List View Android",
                        "Android Example",
                        "List View Source Code",
                        "List View Array Adapter",
                        "Adapter implementation",
                        "Simple List View In Android",
                        "Create List View Android",
                        "Android Example",
                        "List View Source Code",
                        "List View Array Adapter",
                        "Adapter implementation",
                        "Simple List View In Android",
                        "Create List View Android",
                        "Android Example",
                        "List View Source Code",
                        "List View Array Adapter",
                        "Adapter implementation",
                        "Simple List View In Android",
                        "Create List View Android",
                        "Android Example",
                        "List View Source Code",
                        "List View Array Adapter",
                        "Adapter implementation",
                        "Simple List View In Android",
                        "Create List View Android",
                        "Android Example",
                        "List View Source Code",
                        "List View Array Adapter",
                        "Android Example List View"
                };



                final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                        R.layout.row_temp, R.id.label_tv, values);
                final ListView listView = new ListView(this);
                listView.setAdapter(adapter);
                setContentView(listView);

                listView.setOnScrollListener(new AbsListView.OnScrollListener() {
                    public void onScroll(final AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                        // TODO Auto-generated method stub

                    }

                    public void onScrollStateChanged(AbsListView view, int scrollState) {
                        if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                            for(int i=0;i<adapter.getCount();i++){
                                if(listView.getChildAt(i)  != null) {
                                    TextView childView = (TextView) listView.getChildAt(i).findViewById(R.id.label_tv);
                                    if (childView != null) {
                                        childView.setBackgroundColor(Color.GREEN);
                                    }
                                }
                            }

                            int centerItemPosition = ((listView.getLastVisiblePosition() - listView.getFirstVisiblePosition()) / 2);
                            TextView centerView =(TextView)listView.getChildAt(centerItemPosition).findViewById(R.id.label_tv);
                            centerView.setBackgroundColor(Color.RED);
                        }
                    }
                });

row_temp Xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
    android:layout_width="match_parent"
    android:id="@+id/label_tv"
    android:textColor="@android:color/white"
    android:text="skdjfksdf"
    android:gravity="center"
    android:padding="50dp"
    android:background="@color/colorPrimaryDark"
    android:layout_height="wrap_content" />

</LinearLayout>

Output