我正在製作一個應用程序,其中一個活動顯示了一個recyclerview。當列表中的項目被點擊時,我正試圖獲得敬酒。我已經看到了StackOverflow上的各種解決方案,他們沒有幫助。toclick recyclerview
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();
}
}
哪裏是你的烤麪包的消息在你的代碼? –
我刪除它。沒有我嘗試過的。 getApplicationContext或getBaseContext無效。使用帶「this」的活動不起作用 –
只需將Context添加爲您類的構造函數之一,並且當您從Activity中初始化類時,傳遞「this」並且您將獲得上下文。 –