将视图滑动到不同的视图

时间:2015-12-29 12:14:04

标签: android android-recyclerview swipe

我试图达到以下效果: swiping

我希望视图可以移动。在刷卡期间和之后,应在其下方显示另一个视图(我可以指定)。我尝试在RecyclerView之后使用ItemTouchHelper.SimpleCallbackthis tutorial,但这只允许在开始时指定RecyclerView.ViewHolder,然后使用更新的数据集更新它。我想要的是在调用onSwiped方法时为下面的视图完全指定新的RecyclerView.ViewHolder

如果您能指出我使用RecyclerView或其他方式完成此任务的更好方法,我将不胜感激。

我的代码如下所示。

MyActivity类:

public class MyActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_swipe_test);

        mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);

        // use this setting to improve performance if you know that changes
        // in content do not change the layout size of the RecyclerView
        mRecyclerView.setHasFixedSize(true);

        // use a linear layout manager
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);

        // specify an adapter (see also next example) -- useless at the moment
        String[] myDataset = {"a"};
        mAdapter = new MyAdapter(myDataset);
        mRecyclerView.setAdapter(mAdapter);

        ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.RIGHT) {
            /*
            No need to worry about this since we're not supporting drag and drop.
             */
            @Override
            public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
                return false;
            }

            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
                //Remove swiped item from list and notify the RecyclerView
                mAdapter.notifyDataSetChanged();
            }
        };

        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
        itemTouchHelper.attachToRecyclerView(mRecyclerView);
    }
}

MyAdapter类:

public class MyAdapter extends RecyclerView.Adapter<ChallengeAdapter.MyViewHolder> {
    // useless at the moment since we are only using one item
    private String[] mDataset;

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class MyViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public RelativeLayout mRvItem;
        public MyViewHolder(View v) {
            super(v);
            mRvItem = (RelativeLayout) v;
        }
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public ChallengeAdapter(String[] myDataset) {
        // useless at the moment
        mDataset = myDataset;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_item, parent, false);
        MyViewHolder vh = new MyViewHolder(v);
        return vh;

    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element
    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        // useless at the moment
        return mDataset.length;
    }
}

谢谢。

2 个答案:

答案 0 :(得分:0)

使用此库来完成您想要的功能 Macro.Env struct

答案 1 :(得分:0)

这个库看起来不错。本周发表在androidweekly上。 https://github.com/TUBB/SwipeMenuRecyclerView