我正在使用this教程使用RecyclerView实现抽屉导航,但是我不想应用整个教程,我只想处理用户的点击。我已经在onClickListener
类的ViewHolder
中实现了MyAdapter
:
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener{
int Holderid; ImageView imageView, profile;
TextView textView, Name, email, drawerTitle;
private ClickListener listener;
// ViewHolder Constructor with View and viewType as a parameter
public ViewHolder(View itemView, int ViewType, ClickListener listener) {
super(itemView);
if (ViewType == TYPE_HEADER){
itemView.setClickable(false);
Name = (TextView) itemView.findViewById(R.id.user_name);
email = (TextView) itemView.findViewById(R.id.user_email);
profile = (ImageView) itemView.findViewById(R.id.circleView);
Holderid = 0;
}
if(ViewType == TYPE_ITEM) {
itemView.setClickable(true);
textView = (TextView) itemView.findViewById(R.id.rowText);
imageView = (ImageView) itemView.findViewById(R.id.rowIcon);
Holderid = 1;
// Handle item click and set the selection
}
if(ViewType == TYPE_SEPARATOR){
itemView.setClickable(false);
drawerTitle = (TextView) itemView.findViewById(R.id.drawerTitle);
Holderid = 2;
}
this.listener = listener;
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
}
int pos;
@Override
public void onClick(View v) {
if (listener != null) {
listener.onItemClicked(getPosition());
pos= getPosition();
Log.d("TAG", "Item clicked at position " + getPosition());
}
}
@Override
public boolean onLongClick(View v) {
if (listener != null) {
return listener.onItemLongClicked(getPosition());
}
return false;
}
public interface ClickListener {
public void onItemClicked(int position);
public boolean onItemLongClicked(int position);
}
}
我不明白的是如何将
ClickListener
应用于MyAdapter
对象: mAdapter = new MyAdapter(dataList, mSelectedPositions, ...?);
这样,当用户单击一项时,我就可以处理用户对
MainActivity
的单击。我以这种方式创建了适配器(如本教程中概述):
整个
MyAdapter
类:http://pastie.org/private/cibcsj3zieorlkcgh2rm1g这是我的MainActivity类的
onCreate
:@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* Assigning the toolbar object ot the view
and setting the the Action bar to our toolbar */
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
// enable ActionBar app icon to behave as action to toggle nav drawer
getSupportActionBar().setDisplayHomeAsUpEnabled(true); //pulsante drawer
getSupportActionBar().setHomeButtonEnabled(true); //pulsante dietro
//Initializing
mTitle = mDrawerTitle = getTitle();
SharedPreferences usrData = getSharedPreferences(
usr_loggedin,
MODE_PRIVATE);
AVATARresID = R.mipmap.aka;
// Add Drawer Item to dataList
dataList = new ArrayList<>();
dataList = prepareDatalist(dataList, NAME, EMAIL, AVATARresID);
mRecyclerView = (RecyclerView) findViewById(R.id.RecyclerView);
mRecyclerView.setHasFixedSize(true);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mAdapter = new MyAdapter(dataList, mSelectedPositions); //**ERROR HERE**
mRecyclerView.setAdapter(mAdapter);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
// Drawer Listener set to the Drawer toggle
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState(); // Finally we set the drawer toggle sync State
}
如何将
onClick
类中存在的MyAdapter
的结果拉回到我的MainActivity
, int pos;
@Override
public void onClick(View v) {
if (listener != null) {
listener.onItemClicked(getPosition());
pos= getPosition();
Log.d("TAG", "Item clicked at position " + getPosition());
}
}
这样我就可以根据所单击项目的位置来处理操作?
最佳答案
MyAdapter
的构造函数还接受实现ClickListener
的类的对象。例如,您可以让Activity
/Fragment
实现该接口(interface),并使用this
作为参数
public class MainActivity extends Activity implements ViewHolder.ClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ....
mAdapter = new MyAdapter(dataList, mSelectedPositions, this);
// ....
}