2017-01-05 34 views
0

我正在製作一個應用程序,其中一個活動顯示了一個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(); 
    } 
} 
+0

哪裏是你的烤麪包的消息在你的代碼? –

+0

我刪除它。沒有我嘗試過的。 getApplicationContext或getBaseContext無效。使用帶「this」的活動不起作用 –

+1

只需將Context添加爲您類的構造函數之一,並且當您從Activity中初始化類時,傳遞「this」並且您將獲得上下文。 –

回答

4

使用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

另外,爲什麼你需要在onCreateViewHolder()裏面設置監聽器,只需在視圖持有者的構造函數中完成。 –

+1

對「LENGHT.SHORT」以上的小編輯:改爲使用「Toast.LENGTH_SHORT」。 – AJW

0

只需在您的ViewHolder內設置您的OnClickListener

公共類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(); 
} 
} 
+0

上面的「LENGHT.SHORT」的小修改:改爲使用「Toast.LENGTH_SHORT」。 – AJW

0

這可以幫助你:

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(); 
} 
}