繁体   English   中英

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

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

这里是我的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
    }
}

这是我用于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);
        }
    }
}

这是我的content.xml ,适配器从中获取提要。 我想成立onClickListner的观点id:likelayout"like 。使被点击时,一些动作将被执行现在,敬酒就足够了。

请帮我看看怎么做。

<?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>

在您的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. 
        }
    });
}

如果您想为列表项中的其他 UI 元素设置点击侦听器,只需像您声明usernametimeastatusa等的方式声明它们,然后向其中添加侦听器。

请记住,为了使ImageView可点击,您需要在布局文件中将clickable属性添加为true 例如,如果您需要使喜欢的图像可点击,则需要添加如下所示的clickable属性。

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

为了在单击项目时显示Toast ,您需要在创建适配器时拥有Discussion活动的Context 所以我建议你像下面这样修改适配器的构造函数。

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

为了采用修改后的构造函数的更改,需要从您的Discussion类中初始化适配器,如下所示。

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

希望有帮助!

与其他视图一样,在FeedViewHolder类中添加一个LinearLayout变量:

LinearLayout like;

然后

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

然后在onBindViewHolder使用:

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM