2016-07-26 180 views
1

我在線性佈局中的Recycleview存在問題。在下面的代碼當我點擊聊天部分(線性佈局)時,我將LayoutManager設置爲RecycleView時Toast不可見。當我點擊線性佈局時,請幫助我敬酒。線性佈局onclick監聽器不能用於回收視圖

活動代碼:

public class PracticeActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_practice); 
     setLayoutViews(); 
    } 

    private void setLayoutViews(){ 

     // setting up messages list view 
     RecyclerView mMessagesView = (RecyclerView) findViewById(R.id.messages); 

     // if i uncomment below line Toast will not be visible. 
     //mMessagesView.setLayoutManager(new LinearLayoutManager(this)); 

     (findViewById(R.id.chat_section)).setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Toast.makeText(PracticeActivity.this, " Chat section was clicked", Toast.LENGTH_SHORT).show(); 
      } 
     }); 
    } 

activity_practice.xml

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:id="@+id/activity_main"> 
     <LinearLayout 
      android:id="@+id/chat_section" 
      android:layout_width="match_parent" 
      android:layout_height="320dp" 
      android:background="@color/chat_back_ground"> 
      <android.support.v7.widget.RecyclerView 
       android:id="@+id/messages" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:paddingLeft="@dimen/activity_horizontal_margin" 
       android:paddingRight="@dimen/activity_horizontal_margin" 
       android:scrollbars="vertical" 
       android:scrollbarStyle="outsideOverlay"/> 
     </LinearLayout> 

     <TextView 
      android:layout_below="@id/chat_section" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Just text"/> 

</RelativeLayout> 
+0

itemView.setOnClickListener(本)的單獨itemtouchlistener類;在你的適配器中使用viewHolder類 – vinoth12594

+0

你需要ViewHolder檢查這個鏈接http://stackoverflow.com/questions/31790971/no-toast-shown-when-item-clicked-recyclerview –

回答