gpt4 book ai didi

java - 如何将点击监听器添加到 Recycler View 的 subview ?

转载 作者:行者123 更新时间:2023-11-29 23:19:32 26 4
gpt4 key购买 nike

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

这是我从资源布局文件 content.xml 获取的 RecyclerViewFeedAdapter

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,适配器从中获取提要。我想在 View id:likelayout"like 上设置 onClickListner,以便在单击它时执行某些操作. 现在,干杯就够了。

请帮我看看怎么做。

<?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 元素设置点击监听器,则只需像声明 usernametimea 那样声明它们, statusa 等,然后给它添加监听器。

请记住,为了使 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,您需要在创建适配器。所以我想建议你像下面这样修改适配器的构造函数。

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

希望对您有所帮助!

关于java - 如何将点击监听器添加到 Recycler View 的 subview ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54634545/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com