Java tutorial
/* * The MIT License (MIT) * * Copyright (c) 2015 baoyongzhang <baoyz94@gmail.com> * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ package com.tubb.smrv.demo; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.support.annotation.DrawableRes; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.StaggeredGridLayoutManager; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.animation.BounceInterpolator; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import com.tubb.smrv.SwipeMenuLayout; import com.tubb.smrv.SwipeMenuRecyclerView; import java.util.ArrayList; import java.util.List; import java.util.Random; public class StaggeredGridRvActivity extends Activity { private Context mContext; private List<User> users; private AppAdapter mAdapter; private SwipeMenuRecyclerView mRecyclerView; private SwipeRefreshLayout swipeRefreshLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list); mContext = this; users = getUsers(); swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout); swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { Toast.makeText(mContext, "Refresh success", Toast.LENGTH_LONG).show(); swipeRefreshLayout.setRefreshing(false); } }); mRecyclerView = (SwipeMenuRecyclerView) findViewById(R.id.listView); mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)); mRecyclerView.addItemDecoration(new StaggeredSpaceItemDecoration(15, 0, 15, 45)); // interpolator setting mRecyclerView.setOpenInterpolator(new BounceInterpolator()); mRecyclerView.setCloseInterpolator(new BounceInterpolator()); mAdapter = new AppAdapter(this, users); mRecyclerView.setAdapter(mAdapter); } private List<User> getUsers() { List<User> userList = new ArrayList<>(); Random random = new Random(); for (int i = 0; i < 100; i++) { User user = new User(); user.userId = i + 1000; user.userName = "Pobi " + (i + 1); int num = random.nextInt(4); if (num == 0) { user.photoRes = R.drawable.one; } else if (num == 1) { user.photoRes = R.drawable.two; } else if (num == 2) { user.photoRes = R.drawable.three; } else if (num == 3) { user.photoRes = R.drawable.four; } userList.add(user); } return userList; } class AppAdapter extends RecyclerView.Adapter { private static final int VIEW_TYPE_ENABLE = 0; private static final int VIEW_TYPE_DISABLE = 1; List<User> users; public AppAdapter(Context context, List<User> users) { this.users = users; } @Override public long getItemId(int position) { return position; } @Override public int getItemCount() { return users.size(); } @Override public int getItemViewType(int position) { User user = users.get(position); if (user.userId % 2 == 0) { return VIEW_TYPE_DISABLE; } else { return VIEW_TYPE_ENABLE; } } public boolean swipeEnableByViewType(int viewType) { if (viewType == VIEW_TYPE_ENABLE) return true; else if (viewType == VIEW_TYPE_DISABLE) return false; else return true; // default } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_staggered, parent, false); return new MyViewHolder(itemView); } @Override public void onBindViewHolder(final RecyclerView.ViewHolder vh, final int position) { final User user = users.get(position); final MyViewHolder myViewHolder = (MyViewHolder) vh; SwipeMenuLayout itemView = (SwipeMenuLayout) myViewHolder.itemView; itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(mContext, "Hi " + user.userName, Toast.LENGTH_SHORT).show(); } }); myViewHolder.btOpen.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(mContext, "Open " + user.userName, Toast.LENGTH_SHORT).show(); } }); myViewHolder.btDelete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { users.remove(vh.getAdapterPosition()); mAdapter.notifyItemRemoved(vh.getAdapterPosition()); } }); myViewHolder.tvName.setText(user.userName); myViewHolder.ivPhoto.setImageResource(user.photoRes); /** * optional */ itemView.setOpenInterpolator(mRecyclerView.getOpenInterpolator()); itemView.setCloseInterpolator(mRecyclerView.getCloseInterpolator()); } } public static class MyViewHolder extends RecyclerView.ViewHolder { TextView tvName; ImageView ivPhoto; View btOpen; View btDelete; public MyViewHolder(View itemView) { super(itemView); tvName = (TextView) itemView.findViewById(R.id.tvName); ivPhoto = (ImageView) itemView.findViewById(R.id.ivPhoto); btOpen = itemView.findViewById(R.id.btOpen); btDelete = itemView.findViewById(R.id.btDelete); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_left) { mRecyclerView.setSwipeDirection(SwipeMenuRecyclerView.DIRECTION_LEFT); return true; } if (id == R.id.action_right) { mRecyclerView.setSwipeDirection(SwipeMenuRecyclerView.DIRECTION_RIGHT); return true; } return super.onOptionsItemSelected(item); } class User { public int userId; public String userName; @DrawableRes public int photoRes; } }