【问题标题】:Android smooth transition on list items removed移除列表项上的 Android 平滑过渡
【发布时间】:2017-06-03 23:45:06
【问题描述】:

这个 gif 说明了我想要完成的工作:

ListView 的所有项目都被删除,除了被点击的项目,它平滑地过渡到 ListView 的中心,现在大小为 1。

问题是,每当适配器的 ArrayList 的 front 中的项目被删除时,换句话说,当单击除第一个项目之外的任何其他项目时,我都会得到这个不连续的过渡,其中单击的项目及其文本在转换之前跳转到之前第一项的位置:

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 有关。有什么好的方法可以在不跳转的情况下达到预期的效果?

【问题讨论】:

    标签: android listadapter android-transitions


    【解决方案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
    

    如果你足够绝望,这可能会给你想要的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-05
      相关资源
      最近更新 更多