简体   繁体   English

如何将单击侦听器添加到 Recycler View 的子级?

[英]How can i Add the click listeners to the child of Recycler View?

Here is my discussion.java which contains the RecyclerView这里是我的discussion.java其中包含RecyclerView

public class Discussion extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_discussion);
        RecyclerView mRecycleview =(RecyclerView)findViewById(R.id.cycleview);
        mRecycleview.setLayoutManager(new LinearLayoutManager(this));
        String[] use ={"a","b"};
        String[] lo={"be","ndls"};
        String[] ti={"12:00","11:00"};
        String[] staus={"Today","Tommmorow"};
        int[] posimg={R.drawable.img_post1,R.drawable.img_post2};

        mRecycleview.setAdapter(new FeedAdapter(use,lo,ti,staus,posimg));
        @NonNull
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Open Activity for posting", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        // Functionality for poll
    }
}

Here is my FeedAdapter for the RecyclerView which is taking from resource layout file content.xml .这是我用于RecyclerView FeedAdapter ,它取自资源布局文件content.xml

public class FeedAdapter extends 
    RecyclerView.Adapter<FeedAdapter.FeedViewHolder> {

    private  String[] userdata;
    private  String[] locdata;
    private  String[] timedata;
    private  int[]   postdata;
    private  String[] statusdata;

    public  FeedAdapter(String[] userdata,String[] locdata,String[] timedata,String[] statusdata,int[] postdata){
        this.userdata=userdata;
        this.locdata=locdata;
        this.postdata=postdata;
        this.statusdata=statusdata;
        this.timedata=timedata;
    }

    @Override
    public FeedViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater=LayoutInflater.from(parent.getContext());
        View view =inflater.inflate(R.layout.backupofcontent,parent,false);
        return new FeedViewHolder(view);
    }

    @Override
    public void onBindViewHolder( FeedAdapter.FeedViewHolder holder, int position) {
        String user=userdata[position];
        String loc=locdata[position];
        String time=timedata[position];
        String status=statusdata[position];
        int post=postdata[position];
        holder.username.setText(user);
        holder.locationa.setText(loc);
        holder.timea.setText(time);
        holder.statusa.setText(status);
        if(post==0)
            holder.postimg.setVisibility(View.GONE);
        else
            holder.postimg.setImageResource(post);
    }

    @Override
    public int getItemCount() {
        return userdata.length;
    }

    public class FeedViewHolder extends RecyclerView.ViewHolder{

        ImageView postimg;
        TextView username;
        TextView locationa;
        TextView timea;
        TextView statusa;

        public FeedViewHolder(View itemView) {
            super(itemView);
            postimg=itemView.findViewById(R.id.imgView_postPic);
            username=itemView.findViewById(R.id.user_name);
            locationa=itemView.findViewById(R.id.Location);
            timea=itemView.findViewById(R.id.Time);
            statusa=itemView.findViewById(R.id.post_text);
        }
    }
}

Here is my content.xml from which adapter is taking the feed.这是我的content.xml ,适配器从中获取提要。 I want to set up the onClickListner on the view id:likelayout" or like so that when it is clicked, some actions are to be performed. For now, a toast would be enough.我想成立onClickListner的观点id:likelayout"like 。使被点击时,一些动作将被执行现在,敬酒就足够了。

Please help me out how to do it.请帮我看看怎么做。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout

    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:orientation="vertical"
    tools:showIn="@layout/app_bar_main"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/user_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center|center_vertical"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="4dp"
        android:text="User"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/Time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/user_name"
        android:layout_gravity="center|center_vertical"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="4dp"
        android:text="Time"
        android:textSize="10sp" />

    <TextView
        android:id="@+id/Location"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/Time"
        android:layout_gravity="center|center_vertical"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="4dp"
        android:text="Location"
        android:textSize="10sp" />

    <LinearLayout
        android:id="@+id/layout_post"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/Location"
        android:layout_marginTop="10dp"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/imgView_postPic"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:scaleType="fitCenter"
            android:src="@drawable/img_post1" />

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="55dp"
            android:layout_weight="1">

            <TextView
                android:id="@+id/post_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Status " />
        </ScrollView>

    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:layout_marginBottom="10dp"
        android:gravity="center_vertical"
        android:layout_above="@id/bottomnav">

        <TextView
            android:id="@+id/likecount"
            android:layout_marginLeft="20dp"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="0"
            android:gravity="center" />
        <TextView
            android:id="@+id/likecounttext"
            android:layout_toRightOf="@id/likecount"
            android:layout_marginLeft="10dp"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="Liked"/>
        <TextView
            android:layout_toRightOf="@id/likecounttext"
            android:layout_marginLeft="15dp"
            android:id="@+id/countsepartor"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="|"/>
        <TextView
            android:id="@+id/commentcount"
            android:layout_marginLeft="15dp"
            android:layout_toRightOf="@id/countsepartor"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0"/>
        <TextView
            android:layout_marginLeft="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/commentcount"
            android:text="Comments"/>

    </RelativeLayout>
    <LinearLayout
        android:id="@+id/bottomnav"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:weightSum="3"
        android:layout_marginBottom="5dp"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal">

        <LinearLayout
            android:id="@+id/likelayout"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_marginLeft="5dp"
            android:layout_gravity="center_vertical"
            android:layout_height="wrap_content">
            <ImageView
                android:layout_width="25dp"
                android:layout_height="wrap_content"
                android:src="@drawable/like"/>
            <TextView
                android:id="@+id/like"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Like"
                android:layout_marginLeft="10dp"
                android:layout_gravity="center_vertical"
                android:textSize="15sp"/>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/commentlayout"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_gravity="center_vertical"
            android:layout_height="wrap_content">
            <ImageView
                android:layout_width="25dp"
                android:layout_height="wrap_content"
                android:src="@drawable/comment"/>
            <TextView
                android:id="@+id/comment"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Comment"
                android:layout_marginLeft="10dp"
                android:layout_gravity="center_vertical"
                android:textSize="15sp"/>
        </LinearLayout>
        <LinearLayout
            android:id="@+id/polllayout"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:gravity="center"
            android:layout_gravity="center_vertical"
            android:layout_height="wrap_content">
            <ImageView
                android:layout_width="25dp"
                android:layout_height="wrap_content"
                android:src="@drawable/poll"/>
            <TextView
                android:id="@+id/poll"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Poll"
                android:layout_marginLeft="10dp"
                android:layout_gravity="center_vertical"
                android:textSize="15sp"/>
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

In your onBindViewHolder function, just add a click listener like the following.在您的onBindViewHolder函数中,只需添加一个单击侦听器,如下所示。

@Override
public void onBindViewHolder( FeedAdapter.FeedViewHolder holder, int position) {
    String user=userdata[position];
    String loc=locdata[position];
    String time=timedata[position];
    String status=statusdata[position];
    int post=postdata[position];
    holder.username.setText(user);
    holder.locationa.setText(loc);
    holder.timea.setText(time);
    holder.statusa.setText(status);

    if(post==0)
        holder.postimg.setVisibility(View.GONE);
    else
        holder.postimg.setImageResource(post);

    holder.username.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            // Do something. 
        }
    });
}

If you want to set up the click listener to the other UI elements in your list item, then just declare them like the way you have declared username , timea , statusa etc. and then add the listener to it.如果您想为列表项中的其他 UI 元素设置点击侦听器,只需像您声明usernametimeastatusa等的方式声明它们,然后向其中添加侦听器。

Please keep in mind that, in order to make an ImageView clickable you need to add the clickable attribute to true in your layout file.请记住,为了使ImageView可点击,您需要在布局文件中将clickable属性添加为true For example, if you need to make your like image clickable, you need to add the clickable attribute like the following.例如,如果您需要使喜欢的图像可点击,则需要添加如下所示的clickable属性。

<ImageView
    android:id="@+id/like_image"
    android:layout_width="25dp"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:src="@drawable/like"/>

In order to show a Toast on clicking the item, you need to have the Context of the Discussion activity while you are creating the adapter.为了在单击项目时显示Toast ,您需要在创建适配器时拥有Discussion活动的Context So I would like to suggest you modify the constructor of the adapter like the following.所以我建议你像下面这样修改适配器的构造函数。

public class FeedAdapter extends 
    RecyclerView.Adapter<FeedAdapter.FeedViewHolder> {

    private  String[] userdata;
    private  String[] locdata;
    private  String[] timedata;
    private  int[]   postdata;
    private  String[] statusdata;

    // Add another extra variable to store context
    // The context is necessary for showing toast
    private Context context; 

    public  FeedAdapter(String[] userdata, String[] locdata,String[] timedata, String[] statusdata, int[] postdata, Context context){
        this.userdata = userdata;
        this.locdata = locdata;
        this.postdata = postdata;
        this.statusdata = statusdata;
        this.timedata = timedata;
        this.context = context; // Initialize it here
    }

    @Override
    public FeedViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater=LayoutInflater.from(parent.getContext());
        View view =inflater.inflate(R.layout.backupofcontent,parent,false);
        return new FeedViewHolder(view);
    }

    @Override
    public void onBindViewHolder( FeedAdapter.FeedViewHolder holder, int position) {
        String user=userdata[position];
        String loc=locdata[position];
        String time=timedata[position];
        String status=statusdata[position];
        int post=postdata[position];
        holder.username.setText(user);
        holder.locationa.setText(loc);
        holder.timea.setText(time);
        holder.statusa.setText(status);

        if(post==0)
            holder.postimg.setVisibility(View.GONE);
        else
            holder.postimg.setImageResource(post);

        // Use the context here to show the Toast 
        holder.username.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context, "Something", Toast.LENGTH_LOGN).show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return userdata.length;
    }

    public class FeedViewHolder extends RecyclerView.ViewHolder{

        ImageView postimg;
        TextView username;
        TextView locationa;
        TextView timea;
        TextView statusa;

        public FeedViewHolder(View itemView) {
            super(itemView);
            postimg=itemView.findViewById(R.id.imgView_postPic);
            username=itemView.findViewById(R.id.user_name);
            locationa=itemView.findViewById(R.id.Location);
            timea=itemView.findViewById(R.id.Time);
            statusa=itemView.findViewById(R.id.post_text);
        }
    }
}

And to adopt the changes of the modified constructor, the adapter needs to be initialized like the following from your Discussion class.为了采用修改后的构造函数的更改,需要从您的Discussion类中初始化适配器,如下所示。

mRecycleview.setAdapter(new FeedAdapter(use, lo, ti, staus, posimg, this));

Hope that helps!希望有帮助!

same as the other views, in the FeedViewHolder class add a LinearLayout variable:与其他视图一样,在FeedViewHolder类中添加一个LinearLayout变量:

LinearLayout like;

then然后

like = itemView.findViewById(R.id.likelayout);

then in onBindViewHolder use :然后在onBindViewHolder使用:

holder.like.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                // Like Layout was clicked
            }
        });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何点击 Recycler View 并使用 equals 进行扫描? - Java - How can I make a click for Recycler View and scan with equals? - Java 如何在同一活动中添加回收站视图和抽屉布局? - How can I add recycler view and drawer layout in same activity? 如何在屏幕底部添加具有回收器视图的按钮。 这样按钮就不会隐藏回收器视图的最后一个元素 - How can I add button at the bottom of the screen having a recycler View. so that the button don't hide the last element of recycler view 如果我在回收器视图中有数千个项目,那么如何在每个项目上设置项目点击侦听器 - If I Have thousands of items inside the recycler view then How can I set the item click listener on each item 在项目上单击RecyclerView的监听器 - 如何在回收站视图中使用项目的位置来平滑滚动到另一个列表视图? - On Item Click Listener for RecyclerView - How can I use the position of an item in a recycler view to smoothscroll to another listview? 当我单击另一个recyclerview中的项目时如何显示回收者视图 - How to show recycler view when i click an item in another recyclerview 如何将回收器视图添加到片段 - How to add a recycler view to fragment 使用 espresso 在回收站视图中单击孩子的按钮 - Performing click on a button of a child in recycler view using espresso 如何通过 android 中的回收器视图运行 for 循环? - How can I run a for loop through a recycler view in android? 单击按钮时如何在回收器视图中更新textview? - How to update textview in recycler view on button click?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM