如何使用动画交换ListView项目?

时间:2014-10-11 10:34:53

标签: android android-listview android-animation

我想用XE货币应用程序中的动画实现交换ListView项目

XE animation

当用户点击英镑 - 英镑时,该排将上升并适合INR - 印度卢比的动画和行INR - 印度卢比将取代英镑 - 英镑

我在listview中尝试过一个动画(在listview中使用过标题)然后它工作正常,但问题是标题也是用listview上下滚动的,我希望标题视图(或任何视图)固定在它的顶部和下面列表可以滚动

我在相对布局的顶部尝试了一个修复视图,并将listview保持在顶视图下方,此时动画已经完成,但仅在listview中不在listview之外

我们如何在android中实现它?

2 个答案:

答案 0 :(得分:2)

为什么重新发明轮子?有一些记录良好且结构良好的库用于处理ListView。例如,ListViewAnimations是最好的,IMO。

功能

  • ListViewsGridViews,其他AbsListViews中的项目的外观动画
    • 内置动画包括AlphaSwingRightInSwingLeftInSwingBottomInSwingRightInScaleIn
    • 可以轻松添加其他动画
    • 支持
    • StickyListHeaders,可以轻松添加其他实现。
  • 使用上下文撤消
  • 轻扫到关闭,滑动到关闭
  • 拖放重新排序 This is what you need
  • 动画添加项目
  • 平滑扩展您的商品以显示更多内容

enter image description here

答案 1 :(得分:1)

前一段时间我有类似的问题。我需要一个页面,我可以在那里重新安排条目(音乐曲目)。所以这是我的实施:

我的MessagescksFragment,允许重新排序曲目

public class AllTracksFragment extends SupportFragmentBase {

    DynamicListView allTracksListView;

    private ArrayList<Track> allTracksList = new ArrayList<>();
    TracksListViewAdapter allTracksAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_all_tracks, container, false);
        setHasOptionsMenu(true);

        allTracksListView = (DynamicListView)rootView .findViewById(R.id.allTracksListView);

        Track track1 = new Track();  // Track is simple model class
        track1.trackName = "Winter\'s Coming (Acoustic) 1";
        track1.trackId = "47580057";

        Track track2 = new Track();
        track2.trackName = "Winter\'s Coming (Acoustic) 2";
        track2.trackId = "47580057";

        Track track3 = new Track();
        track3.trackName = "Winter\'s Coming (Acoustic) 3";
        track3.trackId = "47580057";

        allTracksList.add(track1);
        allTracksList.add(track2);
        allTracksList.add(track3);

        allTracksAdapter = new TracksListViewAdapter(allTracksList, eventBus);
        allTracksListView.setTracksList(allTracksList); //SEE DynamicListView class
        allTracksListView.setAdapter(allTracksAdapter);
        allTracksListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

        return rootView;
    }
}

和ConsucksFragment布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.myapp.views.DynamicListView
        android:id="@+id/allTracksListView"
        android:layout_marginTop="12dp"
        android:scrollbars="none"
        android:divider="@null"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</RelativeLayout>

TracksListViewAdapter (如果需要):

public final class TracksListViewAdapter extends BaseListViewArrayAdapter<PlayTrackView, Track> {  // extended version of simple BaseAdapter

    final int INVALID_ID = -1;

    public TracksListViewAdapter(final List<Track> tracks) {
        super(tracks == null ? new ArrayList<Track>(0) : tracks);
        if (tracks != null) {
            for (int i = 0; i < tracks.size(); ++i) {
                mIdMap.put(tracks.get(i), i);
            }
        }
    }

    public PlayTrackView createNewView(final Context context, final int position) {
        return new PlayTrackView(context);  // PlayTrackView - is an extension of FrameLayout
    }

    HashMap<Track, Integer> mIdMap = new HashMap<>();

    @Override
    public long getItemId(int position) {
        if (position < 0 || position >= mIdMap.size()) {
            return INVALID_ID;
        }

        Track item = (Track) getItem(position);
        return mIdMap.get(item);
    }

    @Override
    public boolean hasStableIds()
    {
        return android.os.Build.VERSION.SDK_INT < 20;
    }
}

PlayTrackView .java

public class PlayTrackView extends FrameLayout implements IItemDisplayer<Track> {

    public PlayTrackView(Context context) {
        super(context);
        LayoutInflater.from(context).inflate(R.layout.play_track_view, this);
    }

    public PlayTrackView(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.play_track_view, this);
    }

    public PlayTrackView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.play_track_view, this);
    }

    @Override
    public void displayItem(Track track) {

    }
}

<强>轨道的.java

public class Track {
    public String trackId;
    public String trackName;
}

IItemDisplayer 界面

public interface IItemDisplayer<TItem> {
    public void displayItem(TItem item);
}

<强> BaseListViewAdapter

<强> BaseListViewArrayAdapter