我正在开发一个简单的记事本应用程序,我首先使用listView来显示所有注释。但现在我正在使用RecyclerView。当我使用listview时,我使用OnItemClickListener将数据传递给另一个活动来编辑注释 现在我很困惑在Recylerview做什么
对于listView我正在使用此
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
name = filenames.get(position).getName();
note = filenames.get(position).getShorttext();
Alert(); // this method is in main activity
}
});*/
public void Alert()
{
final AlertDialog dialog;
View mview = getLayoutInflater().inflate(R.layout.dialog_pass,null);
final EditText mEdittext = (EditText) mview.findViewById(R.id.Epass);
AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
mBuilder.setView(mview);
mBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String col = mEdittext.getText().toString();
String password = dBhelper.searchpass(col);
if (col.equals(password)) {
Intent intent = new Intent(MainActivity.this,Note2.class);
intent.putExtra("Name",name);
intent.putExtra("Note",note);
startActivity(intent);
} else {
Toast temp = Toast.makeText(MainActivity.this, "Password does not match", Toast.LENGTH_SHORT);
temp.show();
}
}
});
mBuilder.setNegativeButton("Cancel",null);
mBuilder.setCancelable(false);
dialog = mBuilder.create();
dialog.show();
}
现在我该如何处理回收查看请帮助
public class RecycleViewAdapter extends
RecyclerView.Adapter<RecycleViewHolder> {// Recyclerview will extend to
private List<FileName> fileNames;
private Context context;
public RecycleViewAdapter(Context context,List<FileName> fileNames) {
this.context = context;
this.fileNames = fileNames;
}
@Override
public RecycleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater mInflater = LayoutInflater.from(parent.getContext());
ViewGroup mainGroup = (ViewGroup) mInflater.inflate(
R.layout.grid_item, parent, false);
RecycleViewHolder listHolder = new RecycleViewHolder(mainGroup);
return listHolder;
}
@Override
public void onBindViewHolder(RecycleViewHolder holder, final int position) {
final FileName model = fileNames.get(position);
RecycleViewHolder mainHolder = (RecycleViewHolder) holder;// holder
mainHolder.title.setText(model.getName());
mainHolder.note.setText(model.getShorttext());
}
@Override
public int getItemCount() {
return (null != fileNames ? fileNames.size() : 0);
}
答案 0 :(得分:21)
您可以使用两种方式处理此问题
1)。手势触摸 https://www.google.co.in/amp/sapandiwakar.in/recycler-view-item-click-handler/amp/
2)。使用适配器中的接口 https://antonioleiva.com/recyclerview-listener/
我建议使用界面
的第二种方式如何使用recycleleritemclick 的界面
public class RecycleViewAdapter extends
RecyclerView.Adapter<RecycleViewHolder> {// Recyclerview will extend to
private List<FileName> fileNames;
private Context context;
//declare interface
private OnItemClicked onClick;
//make interface like this
public interface OnItemClicked {
void onItemClick(int position);
}
public RecycleViewAdapter(Context context,List<FileName> fileNames) {
this.context = context;
this.fileNames = fileNames;
现在指定点击界面
@Override
public void onBindViewHolder(RecycleViewHolder holder, final int position) {
//............//
holder.title.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onClick.onItemClick(position);
}
});
}
在适配器类的最后,从完成括号开始,使用一种方法将itemclick分配给接口
public void setOnClick(OnItemClicked onClick)
{
this.onClick=onClick;
}
在MainActivity.java中 使用适配器
绑定项目public class MainActivity extends Activity implements OnItemClicked {
private RecyclerView mRecyclerView;
private CityAdapter mAdapter;
private List<City> cities;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_city);
mRecyclerView = (RecyclerView)findViewById(R.id.list);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mAdapter = new CityAdapter(cities, R.layout.row_city, this);
mRecyclerView.setAdapter(mAdapter);
mAdapter.setOnClick(MainActivity.this); // Bind the listener
}
@Override
public void onItemClick(int position) {
// The onClick implementation of the RecyclerView item click
//ur intent code here
}
希望这有助于你,如果你需要任何帮助,你可以问
答案 1 :(得分:8)
最简单的解决方案是访问holder类的“itemView”公共变量并在其上设置onClickListener。
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, NewActivity.class);
intent.putExtra("FileName", list.get(position));
context.startActivity(intent);
}
});
}
答案 2 :(得分:1)
将适配器更改为此
public class RecycleViewAdapter extends
RecyclerView.Adapter<RecycleViewHolder> {// Recyclerview will extend to
private List<FileName> fileNames;
private Context context;
private OnItemClicked listener;
public RecycleViewAdapter(Context context, List<FileName> fileNames, OnItemClicked listener) {
this.context = context;
this.fileNames = fileNames;
this.listener = listener;
}
public BookingHistoryFragment() {
// Required empty public constructor
}
@Override
public RecycleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater mInflater = LayoutInflater.from(parent.getContext());
ViewGroup mainGroup = (ViewGroup) mInflater.inflate(
R.layout.grid_item, parent, false);
RecycleViewHolder listHolder = new RecycleViewHolder(mainGroup);
return listHolder;
}
@Override
public void onBindViewHolder(RecycleViewHolder holder, final int position) {
final FileName model = fileNames.get(position);
RecycleViewHolder mainHolder = (RecycleViewHolder) holder;// holder
mainHolder.title.setText(model.getName());
mainHolder.note.setText(model.getShorttext());
// Add click listener for root view
view.getRootView().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
listener.onItemClick(view, position)
}
});
}
@Override
public int getItemCount() {
return (null != fileNames ? fileNames.size() : 0);
}
public interface OnItemClicked {
void onItemClick(View view, int position);
}
}
现在将ItemClickInterface传递给适配器构造函数,同时从activity或fragment初始化。 而不是:
RecycleViewAdapter adapter = new RecycleViewAdapter(this, filenames);
使用此:
RecycleViewAdapter adapter = new RecycleViewAdapter(this, filenames, new RecycleViewAdapter.OnItemClicked () {
@Override
public void onItemClick(View view, Position position) {
}
});
答案 3 :(得分:1)
首先将此类添加到您的包
public class MyTouchListener implements RecyclerView.OnItemTouchListener {
/*Change these as per your need*/
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private static final int SWIPE_MAX_OFF_PATH = 250;
private OnTouchActionListener mOnTouchActionListener;
private GestureDetectorCompat mGestureDetector;
public static interface OnTouchActionListener {
public void onLeftSwipe(View view, int position);
public void onRightSwipe(View view, int position);
public void onClick(View view, int position);
}
public MyTouchListener(Context context, final RecyclerView recyclerView,
OnTouchActionListener onTouchActionListener){
mOnTouchActionListener = onTouchActionListener;
mGestureDetector = new GestureDetectorCompat(context,new GestureDetector.SimpleOnGestureListener(){
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
// Find the item view that was swiped based on the coordinates
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
int childPosition = recyclerView.getChildPosition(child);
mOnTouchActionListener.onClick(child, childPosition);
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2,
float velocityX, float velocityY) {
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {
return false;
}
// Find the item view that was swiped based on the coordinates
View child = recyclerView.findChildViewUnder(e1.getX(), e1.getY());
int childPosition = recyclerView.getChildPosition(child);
// right to left swipe
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
if (mOnTouchActionListener != null && child != null) {
mOnTouchActionListener.onLeftSwipe(child, childPosition);
}
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
if (mOnTouchActionListener != null && child != null) {
mOnTouchActionListener.onRightSwipe(child, childPosition);
}
}
} catch (Exception e) {
// nothing
}
return false;
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
mGestureDetector.onTouchEvent(e);
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
// do nothing
}}
然后addOnItemTouchListener像这样:
myRecyclerView.addOnItemTouchListener(new MyTouchListener(mContext,
myRecyclerView,
new MyTouchListener.OnTouchActionListener() {
@Override
public void onLeftSwipe(View view, int position) {//code as per your need
}
@Override
public void onRightSwipe(View view, int position) {//code as per your need
}
@Override
public void onClick(View view, int position) {//code as per your need
}
}));
答案 4 :(得分:0)
在RecyclerView
适配器
private OnItemClicked mListener;
public RecycleViewAdapter(Context context, OnItemClicked listener, List<FileName> fileNames) {
//............//
this.mListener = listener;
}
@Override
public void onBindViewHolder(RecycleViewHolder holder, final int position) {
//............//
mainHolder.title.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onItemClick(position);
}
});
}
public interface OnItemClicked {
void onItemClick(int position);
}
将您的活动/片段实施到RecyclerViewAdapter.OnItemClicked
要通过Intent传递,您可以在Activity / Fragment
中编写以下代码 @Override
public void onItemClick(int position) {
Intent intent = new Intent(mContext, YourActivity.class);
intent.putExtra(YourActivity.ARG_FILE, filnames.get(position));
startActivity(intent);
}
答案 5 :(得分:0)
所以,不确定是否有人感兴趣或者我的方法是否有问题,但我想出了一个更简单的方法供我个人使用。
项目的Xml:
pandoc
在onBindViewHolder中,将标记设置为您需要的任何信息。显示的名称,位置,文字,图片显示,等等。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeightLarge"
android:background="@drawable/recycborder">
<TextView
android:id="@+id/recycText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|center"
android:onClick="recycClick"
android:textColor="@color/black"
android:textSize="16sp" />
</RelativeLayout>
在您要处理项目的方法中,请单击:
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
holder.recycText.setText(lstAllInfo.get(position));
holder.recycText.setTag(lstAllInfo.get(position));
}
我认为这有限制。标签可能无法保存您需要的数据,或者回收视图可能无法访问点击的正确活动。就个人而言,我将它用于Android Wear并且效果很好,所以我认为我发布以防万一,因为它比其他方法少得多。
答案 6 :(得分:0)
尝试将此代码放入您的holder类。 它会工作并且易于使用:
<input name="txtPhone" id="txtPhone" class="form-control" value="" aria-required="true" pattern="0[0-9]{9}" title="Your telephone number" type="tel" spellcheck="false" maxlength="10" required>
答案 7 :(得分:0)
在适配器类上创建一个接口。
public interface SelectedUser{
void selectedUser(UserModel userModel);
}
在适配器视图持有者类中,监听click事件。
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
selectedUser.selectedUser(userModelList.get(getAdapterPosition()));
}
});
最终在您的活动中实现您的界面并覆盖该方法。
@Override
public void selectedUser(UserModel userModel) {
startActivity(new Intent(MainActivity.this, SelectedUserActivity.class).putExtra("data",userModel));
}
完整的教程和源代码使用下面的链接。 Recyclerview with click listener