mirror of
https://github.com/wismna/DoNext.git
synced 2025-10-03 15:40:14 -04:00
Add a listener on the RecycleView to listen to touches
Store the Task id in a hidden TextView area
This commit is contained in:
@@ -16,7 +16,7 @@ import java.util.List;
|
|||||||
* {@link RecyclerView.Adapter} that can display a {@link Task} and makes a call to the
|
* {@link RecyclerView.Adapter} that can display a {@link Task} and makes a call to the
|
||||||
* specified {@link OnListFragmentInteractionListener}.
|
* specified {@link OnListFragmentInteractionListener}.
|
||||||
*/
|
*/
|
||||||
public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.ViewHolder> implements View.OnClickListener {
|
public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.ViewHolder> {
|
||||||
|
|
||||||
private final List<Task> mValues;
|
private final List<Task> mValues;
|
||||||
private final OnListFragmentInteractionListener mListener;
|
private final OnListFragmentInteractionListener mListener;
|
||||||
@@ -32,13 +32,13 @@ public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.ViewHolder> im
|
|||||||
.inflate(R.layout.fragment_task, parent, false);
|
.inflate(R.layout.fragment_task, parent, false);
|
||||||
|
|
||||||
ViewHolder holder = new ViewHolder(view);
|
ViewHolder holder = new ViewHolder(view);
|
||||||
holder.mTitleView.setOnClickListener(TaskAdapter.this);
|
|
||||||
return holder;
|
return holder;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onBindViewHolder(final ViewHolder holder, int position) {
|
public void onBindViewHolder(final ViewHolder holder, int position) {
|
||||||
holder.mItem = mValues.get(position);
|
holder.mItem = mValues.get(position);
|
||||||
|
holder.mIdView.setText(String.valueOf(holder.mItem.getId()));
|
||||||
holder.mCycleView.setText(String.valueOf(holder.mItem.getCycle()));
|
holder.mCycleView.setText(String.valueOf(holder.mItem.getCycle()));
|
||||||
holder.mTitleView.setText(holder.mItem.getName());
|
holder.mTitleView.setText(holder.mItem.getName());
|
||||||
|
|
||||||
@@ -59,13 +59,6 @@ public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.ViewHolder> im
|
|||||||
return mValues.size();
|
return mValues.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onClick(View view) {
|
|
||||||
/*ViewHolder holder = (ViewHolder) view.getTag();
|
|
||||||
if (view.getId() == holder.mTitleView.getId()) {
|
|
||||||
Toast.makeText(view.getContext(), holder.mTitleView.getText(), Toast.LENGTH_SHORT).show();
|
|
||||||
}*/
|
|
||||||
}
|
|
||||||
public void add(Task item, int position) {
|
public void add(Task item, int position) {
|
||||||
mValues.add(position, item);
|
mValues.add(position, item);
|
||||||
notifyItemInserted(position);
|
notifyItemInserted(position);
|
||||||
@@ -79,6 +72,7 @@ public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.ViewHolder> im
|
|||||||
|
|
||||||
public class ViewHolder extends RecyclerView.ViewHolder {
|
public class ViewHolder extends RecyclerView.ViewHolder {
|
||||||
public final View mView;
|
public final View mView;
|
||||||
|
public final TextView mIdView;
|
||||||
public final TextView mCycleView;
|
public final TextView mCycleView;
|
||||||
public final TextView mTitleView;
|
public final TextView mTitleView;
|
||||||
public Task mItem;
|
public Task mItem;
|
||||||
@@ -86,6 +80,8 @@ public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.ViewHolder> im
|
|||||||
public ViewHolder(View view) {
|
public ViewHolder(View view) {
|
||||||
super(view);
|
super(view);
|
||||||
mView = view;
|
mView = view;
|
||||||
|
|
||||||
|
mIdView = (TextView) view.findViewById(R.id.task_id);
|
||||||
mCycleView = (TextView) view.findViewById(R.id.task_cycle);
|
mCycleView = (TextView) view.findViewById(R.id.task_cycle);
|
||||||
mTitleView = (TextView) view.findViewById(R.id.task_name);
|
mTitleView = (TextView) view.findViewById(R.id.task_name);
|
||||||
}
|
}
|
||||||
|
@@ -66,6 +66,16 @@ public class TaskDataAccess {
|
|||||||
return getAllTasksCursor();
|
return getAllTasksCursor();
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
|
public Task getTask(long id)
|
||||||
|
{
|
||||||
|
Cursor cursor = getTaskCursor(id);
|
||||||
|
|
||||||
|
cursor.moveToFirst();
|
||||||
|
Task task = cursorToTask(cursor);
|
||||||
|
cursor.close();
|
||||||
|
return task;
|
||||||
|
}
|
||||||
|
|
||||||
public List<Task> getAllTasks(long id) {
|
public List<Task> getAllTasks(long id) {
|
||||||
List<Task> tasks = new ArrayList<>();
|
List<Task> tasks = new ArrayList<>();
|
||||||
|
|
||||||
@@ -105,6 +115,10 @@ public class TaskDataAccess {
|
|||||||
return totalCycles;
|
return totalCycles;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Cursor getTaskCursor(long id) {
|
||||||
|
return database.query(DatabaseHelper.TASKS_TABLE_NAME,
|
||||||
|
taskColumns, DatabaseHelper.COLUMN_ID + " = " + id, null, null, null, null);
|
||||||
|
}
|
||||||
public Cursor getAllTasksCursor(long id) {
|
public Cursor getAllTasksCursor(long id) {
|
||||||
return database.query(DatabaseHelper.TASKS_TABLE_NAME,
|
return database.query(DatabaseHelper.TASKS_TABLE_NAME,
|
||||||
taskColumns, DatabaseHelper.TASKS_COLUMN_LIST + " = " + id, null, null, null, null);
|
taskColumns, DatabaseHelper.TASKS_COLUMN_LIST + " = " + id, null, null, null, null);
|
||||||
|
@@ -9,11 +9,13 @@ import android.view.LayoutInflater;
|
|||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
import com.wismna.geoffroy.donext.R;
|
import com.wismna.geoffroy.donext.R;
|
||||||
import com.wismna.geoffroy.donext.adapters.TaskAdapter;
|
import com.wismna.geoffroy.donext.adapters.TaskAdapter;
|
||||||
import com.wismna.geoffroy.donext.dao.Task;
|
import com.wismna.geoffroy.donext.dao.Task;
|
||||||
import com.wismna.geoffroy.donext.database.TaskDataAccess;
|
import com.wismna.geoffroy.donext.database.TaskDataAccess;
|
||||||
|
import com.wismna.geoffroy.donext.listeners.RecyclerItemClickListener;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A fragment representing a list of Items.
|
* A fragment representing a list of Items.
|
||||||
@@ -59,10 +61,22 @@ public class TasksFragment extends Fragment {
|
|||||||
View view = inflater.inflate(R.layout.fragment_task_list, container, false);
|
View view = inflater.inflate(R.layout.fragment_task_list, container, false);
|
||||||
|
|
||||||
// Set the adapter
|
// Set the adapter
|
||||||
Context context = view.getContext();
|
final Context context = view.getContext();
|
||||||
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.task_list_view);
|
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.task_list_view);
|
||||||
|
|
||||||
|
final Toast mToast = Toast.makeText(getActivity(), "", Toast.LENGTH_SHORT);
|
||||||
recyclerView.setLayoutManager(new LinearLayoutManager(context));
|
recyclerView.setLayoutManager(new LinearLayoutManager(context));
|
||||||
|
recyclerView.addOnItemTouchListener(
|
||||||
|
new RecyclerItemClickListener(context, new RecyclerItemClickListener.OnItemClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onItemClick(View view, int position) {
|
||||||
|
// TODO: implement on item click event
|
||||||
|
TextView editText = (TextView) view.findViewById(R.id.task_id);
|
||||||
|
mToast.setText("Item " + editText.getText() + " clicked!");
|
||||||
|
mToast.show();
|
||||||
|
}
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
taskDataAccess = new TaskDataAccess(view.getContext());
|
taskDataAccess = new TaskDataAccess(view.getContext());
|
||||||
taskDataAccess.open();
|
taskDataAccess.open();
|
||||||
|
@@ -0,0 +1,48 @@
|
|||||||
|
package com.wismna.geoffroy.donext.listeners;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.support.v7.widget.RecyclerView;
|
||||||
|
import android.view.GestureDetector;
|
||||||
|
import android.view.MotionEvent;
|
||||||
|
import android.view.View;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by geoffroy on 15-12-02.
|
||||||
|
*/
|
||||||
|
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
|
||||||
|
private OnItemClickListener mListener;
|
||||||
|
|
||||||
|
public interface OnItemClickListener {
|
||||||
|
void onItemClick(View view, int position);
|
||||||
|
}
|
||||||
|
|
||||||
|
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.getChildAdapterPosition(childView));
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@@ -3,13 +3,17 @@
|
|||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:orientation="horizontal" >
|
android:orientation="horizontal" >
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/task_id"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:visibility="gone" />
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/task_cycle"
|
android:id="@+id/task_cycle"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_margin="@dimen/text_margin"
|
android:layout_margin="@dimen/text_margin"
|
||||||
android:textAppearance="?attr/textAppearanceListItem" />
|
android:textAppearance="?attr/textAppearanceListItem" />
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/task_name"
|
android:id="@+id/task_name"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
|
Reference in New Issue
Block a user