質問

I have to add pull-to-refresh functionality to refresh info on main screen. Here`s a scheme of my screen UI (red area should handle pulling):

enter image description here

I use ready solution for pull-to-refresh. Due to documentation, my red layout should be one of these classes:

ListView
ExpandableListView
GridView
WebView
ScrollView
HorizontalScrollView
ViewPager

But I have ListView on my screen, so I am not able to use ScrollView as red layout. And I`m stuck with this problem. It is possible to use UITableView into UIScrollView in iOS, but in Android one I have no idea what to do in such cases.

Any help will be appreciated. Thanks!

役に立ちましたか?

解決 3

I've solved this issue, but forgot to write about it :)

My layout.xml file looks like this:

   <com.handmark.pulltorefresh.library.PullToRefreshScrollView
        android:id="@+id/home_info_pulltorefresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true" >

        <LinearLayout
            android:id="@+id/home_info_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/white_background"
            android:baselineAligned="false"
            android:orientation="horizontal" >

            <LinearLayout
                android:id="@+id/charts_container"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:orientation="vertical"
                android:paddingRight="5dp" >

                <FrameLayout
                    android:id="@+id/frame_container_chart_top"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1" />

                <FrameLayout
                    android:id="@+id/frame_container_chart_bottom"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1" />
            </LinearLayout>

            <FrameLayout
                android:id="@+id/frame_container_right"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1" />
        </LinearLayout>
    </com.handmark.pulltorefresh.library.PullToRefreshScrollView> 

android:fillViewport="true" is used to stretch its content to fill the viewport (make height of scroll match_parent)

I add Fragment containing ListView programmatically (R.layout.frame_container_right Fragment resource ID)

Everything worked fine, but when I tried to scroll ListView down, my ScrollView began to scroll. Scrolling ListView up worked fine. Also I noticed that if I tap ListView, move finger left or right and then try to scroll down, touch events are not transmitted from ListView to ScrollView and I get expected behavior. So I've decided to emulate this situation programmatically:

mListViewReviews.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    long startTime = SystemClock.uptimeMillis();
                    long duration = 1;
                    MotionEvent e = MotionEvent.obtain(startTime, startTime + duration, MotionEvent.ACTION_MOVE, event
                            .getX(), event.getY() + 10, 0);
                    MotionEvent ev = MotionEvent.obtain(startTime + duration, startTime + duration * 2,
                            MotionEvent.ACTION_MOVE, event.getX(), event.getY() + 20, 0);

                    v.dispatchTouchEvent(e);
                    v.dispatchTouchEvent(ev);

                }
                return false;
            }
        });

As a result, I've got working ListView with proper cell reuse and working pull-to-refresh logic. Thanks!

他のヒント

Why not replace the listview by a LinearLayout and then you can use the ScrollView? You just need to create a layout for the items in the linearlayout and then adding them using something like this:

LinearLayout layout = (LinearLayout)findViewById(R.id.MyListLayout);
for (int i=0; i<list.size(); i++) {
  Item item = list.get(i);
  View view = inflater.inflate(R.layout.MyRowLayout, null);
  Textview myTextView = view.findViewById(R.id.MyTextView);
  myTextView.setText(item.getString());
  list.addView(vi);
}

Is the pull-to-refresh horizontal or vertical?

In case it's horizontal, use a HorizontalScrollView (a ViewPager would do, too), and then place a Table?Layout inside it.

In case it's horizontal, well, I don't think I like that design (the pull-to-refresh area should just be the ListView), but I believe there is some way to use the ListView without its internal scrolling so that you can rely on a parent ScrollView to do the scrolling, but I'd need to check the code of an old problem to "remember" how to do that.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top