Skip to content
This repository has been archived by the owner on Nov 1, 2018. It is now read-only.

API 10 support (using NineOldAndroids) #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added app/libs/nineoldandroids-2.4.0.jar
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*
* Copyright 2013 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.android.swipedismiss;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {

private static final int ITEMS_COUNT = 20;
private List<String> mItems;

private RecyclerView mRecyclerView;
private RecyclerView.LayoutManager mLayoutManager;
private RecyclerView.Adapter<CustomViewHolder> mAdapter;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

initData();
initRecyclerView();
}

private void initData() {
mItems = new ArrayList<>();
for (int i = 0; i < ITEMS_COUNT; i++) {
mItems.add("Item " + (i + 1));
}
}

private void initRecyclerView() {
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
mLayoutManager = new LinearLayoutManager(this);
// mLayoutManager = new GridLayoutManager(this, 2);
// mLayoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(mLayoutManager);

mAdapter = new RecyclerView.Adapter<CustomViewHolder>() {
@Override
public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(android.R.layout.simple_list_item_1
, viewGroup, false);
return new CustomViewHolder(view);
}

@Override
public void onBindViewHolder(CustomViewHolder viewHolder, int i) {
// TODO: this is temp solution for preventing blinking item onDismiss, you should setVisibility for root view of item
viewHolder.mTextView.setVisibility(View.VISIBLE);

viewHolder.mTextView.setText(mItems.get(i));
}

@Override
public int getItemCount() {
return mItems.size();
}
};
mRecyclerView.setAdapter(mAdapter);

SwipeDismissRecyclerViewTouchListener touchListener =
new SwipeDismissRecyclerViewTouchListener(
mRecyclerView,
new SwipeDismissRecyclerViewTouchListener.OnDismissCallback() {
@Override
public void onDismiss(RecyclerView recyclerView, int[] reverseSortedPositions) {
for (int position : reverseSortedPositions) {

// TODO: this is temp solution for preventing blinking item onDismiss
mLayoutManager.findViewByPosition(position).setVisibility(View.GONE);

mItems.remove(position);
mAdapter.notifyItemRemoved(position);
}
}
});
mRecyclerView.setOnTouchListener(touchListener);
// Setting this scroll listener is required to ensure that during ListView scrolling,
// we don't look for swipes.
mRecyclerView.setOnScrollListener(touchListener.makeScrollListener());
mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(this,
new OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Toast.makeText(MainActivity.this, "Clicked " + mItems.get(position), Toast.LENGTH_SHORT).show();
}
}));
}

private class CustomViewHolder extends RecyclerView.ViewHolder {

private TextView mTextView;

public CustomViewHolder(View itemView) {
super(itemView);

mTextView = (TextView) itemView.findViewById(android.R.id.text1);
}
}

public interface OnItemClickListener {
public void onItemClick(View view, int position);
}

public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;

GestureDetector mGestureDetector;

public RecyclerItemClickListener(Context context, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
});
}

@Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildPosition(childView));
}
return false;
}

@Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
}
}
}
Loading