线性布局onclick侦听器不使用循环视图

时间:2016-07-26 11:06:57

标签: android android-recyclerview android-linearlayout

我在linearLayout中遇到了Recycleview的问题。在下面的代码当我点击聊天部分(线性布局)时,当我将LayoutManager设置为RecycleView时,Toast不可见。当我点击线性布局时,请帮助我看到烤面包。

活动代码:

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>

1 个答案:

答案 0 :(得分:2)

您可以按照本教程操作。接下来的过程是创建一个单独的itemtouchlistener类,从中确定选择

http://sapandiwakar.in/recycler-view-item-click-handler/