在這方面,我已經添加SetoNclicklistener爲recyclerview本身,而是當我點擊回收站視圖沒有被觸發..please幫助觸摸事件(onClickListener)不觸發recyclerView
主要活動
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.view);
getOverflowMenu();
android.support.v4.app.LoaderManager loaderManager = getSupportLoaderManager();
android.support.v4.content.Loader<Cursor> loader=loaderManager.getLoader(loader_id);
if(loader!=null) {
loaderManager.initLoader(loader_id, null, this);
}
else
{
loaderManager.restartLoader(loader_id,null,this);
}
recyclerView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"touched",Toast.LENGTH_SHORT).show();;
}
});
}
回收站適配器 `公共類RecycleAdapter擴展RecyclerView.Adapter {
private int total_items=0;
private Context context;
private Cursor lcursor;
public RecycleAdapter(Context mcontext,Cursor cursor) {
context=mcontext;
lcursor=cursor;
}
@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(context).inflate(R.layout.item_view, parent, false);
RecyclerViewHolder recyclerViewHolder=new RecyclerViewHolder((view));
return recyclerViewHolder;
}
@Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
if(!lcursor.moveToPosition(position)) {
return;
}
String todo=(lcursor.getString(lcursor.getColumnIndex(DatabaseContract.Column.COLUMN_TODO_NAME)))+" "+
(lcursor.getString(lcursor.getColumnIndex(DatabaseContract.Column.COLUMN_TODO)));
String time=lcursor.getString(lcursor.getColumnIndex(DatabaseContract.Column.COLUMN_TODO_TIME));
holder.item_desc.setText(todo);
holder.item_date.setText(time);
holder.itemView.setTag(lcursor.getColumnIndex(DatabaseContract.Column._ID));
}
@Override
public int getItemCount() {
return lcursor.getCount();
}
public class RecyclerViewHolder extends RecyclerView.ViewHolder
{
private TextView item_desc,item_date;
public RecyclerViewHolder(View itemView) {
super(itemView);
item_desc=(TextView)itemView.findViewById(R.id.item_desc);
item_date=(TextView)itemView.findViewById(R.id.item_date);
}
}
}
W你想實施的帽子? –
您正在點擊recycelrview中的項目而不是recyclerView,請爲項目設置'onClickListener()'。 –
爲什麼要爲整個視圖設置OnClickListener?大多數應用程序會在RecyclerView中爲單個視圖設置偵聽器。 –