已删除列表项的Android平滑过渡

时间:2017-06-03 23:45:06

标签: android listadapter android-transitions

这个gif说明了我想要实现的目标:

ideal behavior

除了单击的项目之外,所有ListView的项目都被删除,并且它平滑地过渡到ListView的中心,现在大小为1.

问题在于,无论何时移除适配器的ArrayList的前面上的项目,换句话说,当点击第一个项目之外的任何其他项目时,我得到这个不连续的过渡,其中点击的项目及其文本在转换之前跳转到先前第一个项目的位置: unwanted behavior

final String[] items = {"foo", "bar", "The Quick Brown Fox", "qwerty"};
    final ArrayList<String> alItems = new ArrayList<>(Arrays.asList(items));

    final ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.tv1, alItems);
    final ViewGroup main = (ViewGroup) findViewById(R.id.main);
    final ListView lv = (ListView)findViewById(R.id.theListView);
    lv.setAdapter(adapter);
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // Remove all items except for the chosen one

            TransitionManager.beginDelayedTransition(main);
            alItems.subList(0, position).clear();  // Remove items before clicked item
            alItems.subList(1, alItems.size()).clear();  // Remove items after clicked item
            adapter.notifyDataSetChanged();

        }
    });

我怀疑它与在后台设置为新的ArrayList的ArrayList有关。没有跳跃就能达到预期效果的好方法是什么?

1 个答案:

答案 0 :(得分:1)

黑客警报!!!

    TransitionManager.beginDelayedTransition(main);
    // set all but selected to empty string
    //   this leaves the list intact but only displaying selected item
    for (int i=0; i<alItems.size(); i++) {
        if(i != position) {
            alItems.set(i, "");
        }
    }
    adapter.notifyDataSetChanged();

    // now wait a while & clear the list
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            alItems.subList(0, position).clear();  // Remove items before clicked item
            alItems.subList(1, alItems.size()).clear();  // Remove items after clicked item
            adapter.notifyDataSetChanged();
        }
    };
    Handler handler = new Handler();
    handler.postDelayed(runnable, 200);     // 200ms or whatever delay looks good

如果你非常绝望,这可能会给你你想要的东西。