1

カスタム ArrayAdapter で ListView を使用しています。リストはつぶやきの無限スクロールです。リストの更新は上から挿入されます。Twitterアプリとしての効果を得たい。「スクロールして更新」について話しているのではなく、更新後の位置を維持することです。

そのように機能するコードをいくつか実装しました。ここにあります:

        // get the position of the first visible tweet.
        // pausedCounter traces the number of tweets in the waiting line
        final int idx = listView.getFirstVisiblePosition() + pausedCounter;
        View first = listView.getChildAt(0);
        int position = 0;


        if (first != null)
            position = first.getTop();

        // here I update the listView with the new elements
        for (Tweet[] tweets1 : pausedTweets)
            super.updateTweets(tweets1);


        final int finalPosition = position;

        // this code maintain the position
        listView.post(new Runnable() {
            @Override
            public void run() {
                listView.setSelectionFromTop(idx, finalPosition);
            }
        });

このコードの問題は、一瞬 listView がリストの最初の要素に移動し、次にsetSelectionFromTopを開始して正しい位置に移動することです。

このような「ちらつき」が気になるので、消したいです。

4

1 に答える 1

1

私はこの解決策だけを見つけました:

        // add the new elements to the current ArrayAdapter
        for (Tweet[] tweets1 : pausedTweets)
            super.updateTweets(tweets1);

        // create a NEW ArrayAdapter using the data of the current used ArrayAdapter
        // (this is a custom constructor, creates an ArrayAdapter using the data from the passed)
        TweetArrayAdapter newTweetArrayAdapter =
                new TweetArrayAdapter(context, R.layout.tweet_linearlayout, (TweetArrayAdapter)listView.getAdapter());

        // change the ArrayAdapter of the listView with the NEW ArrayAdapter
        listView.setAdapter(newTweetArrayAdapter);

        // set the position. Remember to add as offset the number of new elements inserted
        listView.setSelectionFromTop(idx, position);

これで「ちらつき」が全くなくなりました!

于 2013-08-19T10:16:11.557 に答える