我尝试向我的RecyclerView
List
添加向左或向右滑动,但我不知道自己做错了什么。我尝试了很多有效的解决方案,但他们不适合我。
这是我的Fragment
:
import android.app.Fragment;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.LinearSnapHelper;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import java.util.ArrayList;
import de.christian_heinisch.studenttodo.adapters.ToDoListRecyclerViewAdapter;
import de.christian_heinisch.studenttodo.database.ToDo;
import de.christian_heinisch.studenttodo.database.ToDoDataSource;
/**
* A simple {@link Fragment} subclass.
*/
public class ToDoFragment_RV extends Fragment {
View rootview;
private ToDoDataSource dataSource_todo;
private RecyclerView mRecyclerView;
private RecyclerView new_mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private RecyclerView.LayoutManager new_mLayoutManager;
private static String LOG_TAG = "RecyclerViewActivity";
public ToDoFragment_RV() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootview = inflater.inflate(R.layout.fragment_to_do_rv, container, false);
mRecyclerView = (RecyclerView) rootview.findViewById(R.id.rv);
mRecyclerView.setHasFixedSize(true);
LinearSnapHelper snapHelper = new LinearSnapHelper();
snapHelper.attachToRecyclerView(mRecyclerView);
mLayoutManager = new LinearLayoutManager(getContext());
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new ToDoListRecyclerViewAdapter(getContext(), getDataSet());
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setOnFlingListener(snapHelper);
RecyclerView.ItemDecoration itemDecoration =
new DividerItemDecoration(getContext(), LinearLayoutManager.VERTICAL);
mRecyclerView.addItemDecoration(itemDecoration);
new_mRecyclerView = (RecyclerView) rootview.findViewById(R.id.rv_checked);
new_mRecyclerView.setHasFixedSize(true);
new_mLayoutManager = new LinearLayoutManager(getContext());
new_mRecyclerView.setLayoutManager(new_mLayoutManager);
mAdapter = new ToDoListRecyclerViewAdapter(getContext(), getDataSet_checked());
new_mRecyclerView.setAdapter(mAdapter);
RecyclerView.ItemDecoration newitemDecoration =
new DividerItemDecoration(getContext(), LinearLayoutManager.VERTICAL);
new_mRecyclerView.addItemDecoration(newitemDecoration);
// setHasOptionsMenu(true);
FloatingActionButton fab = (FloatingActionButton) rootview.findViewById(R.id.fbToDoAdd);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
((StartActivity)getContext()).DialogAddToDO();
}
});
return rootview;
}
private ArrayList<ToDo> getDataSet() {
dataSource_todo = new ToDoDataSource(getContext());
dataSource_todo.open();
ArrayList<ToDo> arrayOfToDo = null;
arrayOfToDo = dataSource_todo.getToDoForList("false");
dataSource_todo.close();
return arrayOfToDo;
}
private ArrayList<ToDo> getDataSet_checked() {
dataSource_todo = new ToDoDataSource(getContext());
dataSource_todo.open();
ArrayList<ToDo> arrayOfToDo = null;
arrayOfToDo = dataSource_todo.getToDoForList("true");
dataSource_todo.close();
return arrayOfToDo;
}
}
这是我的RecyclerView
Adapter
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Locale;
import de.christian_heinisch.studenttodo.R;
import de.christian_heinisch.studenttodo.StartActivity;
import de.christian_heinisch.studenttodo.ToDoFragment_RV;
import de.christian_heinisch.studenttodo.database.ToDo;
import de.christian_heinisch.studenttodo.database.ToDoDataSource;
/**
* Created by chris on 16.06.2017.
*/
public class ToDoListRecyclerViewAdapter extends RecyclerView
.Adapter<ToDoListRecyclerViewAdapter
.DataObjectHolder> {
private ArrayList<ToDo> mDataset;
private static MyClickListener myClickListener;
private Context mContext;
ToDoDataSource dataSource = new ToDoDataSource(mContext);
public static class DataObjectHolder extends RecyclerView.ViewHolder
implements View
.OnClickListener {
TextView label;
TextView dateTime;
CheckBox checked;
public DataObjectHolder(View itemView) {
super(itemView);
label = (TextView) itemView.findViewById(R.id.textView_item_todolist_content);
dateTime = (TextView) itemView.findViewById(R.id.textView_item_todolist_Date);
checked = (CheckBox) itemView.findViewById(R.id.cbToDo);
//itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
myClickListener.onItemClick(getPosition(), v);
}
}
public void setOnItemClickListener(MyClickListener myClickListener) {
this.myClickListener = myClickListener;
}
public ToDoListRecyclerViewAdapter(Context context, ArrayList<ToDo> myDataset) {
mDataset = myDataset;
mContext = context;
}
@Override
public DataObjectHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_todolist, parent, false);
DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
return dataObjectHolder;
}
@Override
public void onBindViewHolder(final DataObjectHolder holder, final int position) {
holder.label.setText(mDataset.get(position).getToDo());
holder.dateTime.setText(getDate(mDataset.get(position).isDate()));
if(mDataset.get(position).isChecked().equalsIgnoreCase("true")){
holder.checked.toggle();
}
holder.itemView.setOnTouchListener(new View.OnTouchListener() {
float _xSwipe1;
float _xSwipe2;
@Override
public boolean onTouch(View v, MotionEvent event)
{
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
_xSwipe1 = event.getX();
break;
case MotionEvent.ACTION_UP:
_xSwipe2 = event.getX();
float deltaX = _xSwipe2 - _xSwipe1;
if (deltaX < 0)
{
Log.e("SWIPE", "Right to Left swipe");
}
else if (deltaX >0)
{
Log.e("SWIPE", "Left to right swipe");
}
break;
}
return false;
}
});
// Set onClicklistener
holder.label.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View view) {
System.out.println("Position " + position);
edit(mDataset.get(position).getId());
//notifyDataSetChanged();
return false;
}
});
holder.dateTime.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View view) {
edit(mDataset.get(position).getId());
return false;
}
});
holder.checked.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dataSource = new ToDoDataSource(mContext);
dataSource.open();
// TODO Auto-generated method stub
if(holder.checked.isChecked()){
System.out.println("Checked");
dataSource.updateToDo(mDataset.get(position).getId(), mDataset.get(position).getToDo(), "true", mDataset.get(position).isDate());
}else{
System.out.println("un-Checked");
dataSource.updateToDo(mDataset.get(position).getId(), mDataset.get(position).getToDo(), "false", mDataset.get(position).isDate());
}
dataSource.close();
}
});
}
public void addItem(ToDo dataObj, int index) {
mDataset.add(dataObj);
notifyItemInserted(index);
}
public void deleteItem(int index) {
mDataset.remove(index);
notifyItemRemoved(index);
}
@Override
public int getItemCount() {
return mDataset.size();
}
public interface MyClickListener {
public void onItemClick(int position, View v);
}
private String getDate(long time) {
Calendar cal = Calendar.getInstance(Locale.GERMAN);
cal.setTimeInMillis(time);
String date = DateFormat.format("dd.MM.yyyy", cal).toString();
return date;
}
private void edit(long l){
((StartActivity)mContext).DialogEditToDO(l);
}
}
答案 0 :(得分:2)
关于案例true
的{{1}}可以解决您的问题。