我正在创建一个活动显示回收者视图的应用。我点击列表中的项目时试图祝酒。我已经在StackOverflow上看到了各种解决方案,但他们没有帮助。
BasicsAdapter.java
public class BasicsAdapter extends RecyclerView.Adapter<BasicsAdapter.MyViewHolder> {
private List<Basic> basicsList;
public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView title;
public MyViewHolder(View view){
super(view);
title = (TextView)view.findViewById(R.id.title);
}
@Override
public void onClick(View v) {
}
}
public BasicsAdapter(List<Basic> basicsList){
this.basicsList = basicsList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.basic_list_row, parent, false);
itemView.setClickable(true);
itemView.setOnClickListener(true);
//itemView.setFocusableInTouchMode(true); //dont enable this
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Basic basic = basicsList.get(position);
holder.title.setText(basic.getTitle());
}
@Override
public int getItemCount() {
return basicsList.size();
}
}
basics_list.java
public class basics_list extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private List<Basic> basicList = new ArrayList<>();
private RecyclerView recyclerView;
private BasicsAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle(R.string.basics_list_name);
setContentView(R.layout.activity_basics_list);
// Adding Toolbar to Main screen
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
/*ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager); */
// set recycler view
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mAdapter = new BasicsAdapter(basicList);
final RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
prepareBasicsData();
// Create Navigation drawer and inflate layout
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
// Adding menu icon to Toolbar
ActionBar supportActionBar = getSupportActionBar();
if (supportActionBar != null) {
supportActionBar.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
supportActionBar.setDisplayHomeAsUpEnabled(true);
}
// Set behavior of Navigation drawer
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
// This method will trigger on item Click of navigation menu
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
// Set item in checked state
menuItem.setChecked(true);
// TODO: handle navigation
// Closing drawer on item click
mDrawerLayout.closeDrawers();
return true;
}
});
}
private void prepareBasicsData() {
Basic basic = new Basic("Introduction to Linux");
basicList.add(basic);
basic = new Basic("What is The Terminal");
basicList.add(basic);
basic = new Basic("Programmers guide");
basicList.add(basic);
mAdapter.notifyDataSetChanged();
}
}
答案 0 :(得分:10)
使用itemView.setOnClickListener(new View.OnClickListener() {})
代替itemView.setOnClickListener(true)
。
这样您可以直接应用OnClickListener
。它看起来与此类似:
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void OnClick(View view) {
Toast.makeText(view.getContext(), "Item is clicked", Toast.LENGTH_SHORT).show();
}
});
你可以将它放入构造函数中。
答案 1 :(得分:1)
这可能会对您有所帮助:
public class BasicsAdapter extends RecyclerView.Adapter<BasicsAdapter.MyViewHolder> {
private List<Basic> basicsList;
public static class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title;
Context context;
public MyViewHolder(View view){
super(view);
title = (TextView)view.findViewById(R.id.title);
}
}
public BasicsAdapter(Context context,List<Basic> basicsList){
this.context=context;
this.basicsList = basicsList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.basic_list_row, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Basic basic = basicsList.get(position);
holder.title.setText(basic.getTitle());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context,"Show your text here",Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return basicsList.size();
}
}
答案 2 :(得分:0)
我希望这对你有所帮助。 他曾使用界面来收听ViewHolder中视图的点击。 https://www.youtube.com/watch?v=wCSePoE3lqA
答案 3 :(得分:0)
只需在OnClickListener
内设置ViewHolder
。
public class BasicsAdapter扩展了RecyclerView.Adapter {
private List<Basic> basicsList;
public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView title;
public MyViewHolder(View view){
super(view);
view.setOnClickListener(new View.OnClickListener() {
Toast.makeText(view.getContext(), "Item is clicked", LENGHT.SHORT).show();
});
title = (TextView)view.findViewById(R.id.title);
}
}
public BasicsAdapter(List<Basic> basicsList){
this.basicsList = basicsList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.basic_list_row, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Basic basic = basicsList.get(position);
holder.title.setText(basic.getTitle());
}
@Override
public int getItemCount() {
return basicsList.size();
}
}