在尝试像视频流应用程序这样的热门明星时,在recyclerview中,我在header_layout中具有header_layout和Item_layout,而我具有两个字段textview和按钮,现在我的问题是单击按钮时它必须进行下一个活动。我们怎样才能做到这一点。我有适配器类和ViewHolder类。
header_type_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16sp">
<TextView
android:id="@+id/gridHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textColor="@color/colorPrimaryDark"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:text="MORE" />
</RelativeLayout>
在适配器类中
private void bindHeaderItem(final Holder holder, final int position) {
TextView title = (TextView) holder.itemView.findViewById(R.id.gridHeader);
title.setText(mListItem.get(position).getmItemTitle());
Button button= (Button) holder.itemView.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
context.startActivity(new Intent(context,Activity2.class));
}
});
最佳答案
尝试将所有视图放入ViewHolder。如果一切正常,它将正常工作。
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder> {
private static final String TAG = MyAdapter.class.getSimpleName();
private Context mContext;
private List<Item> mItems;
public MyAdapter(Context context, List<Item> items) {
this.mContext = context;
this.mItems = items;
}
@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(mContext)
.inflate(R.layout.header_type_layout, parent, false);
return new MyHolder(v);
}
@Override
public void onBindViewHolder(MyHolder holder, int position) {
Item item = mItems.get(position);
bindView(holder, item);
holder.button.setOnClickListener(v -> startYourActivity());
}
private void bindView(MyHolder holder, Item item) {
// Bind your view here
}
@Override
public int getItemCount() {
return mItems.size();
}
static class MyHolder extends RecyclerView.ViewHolder {
private Button button;
private TextView textview;
MyHolder(View itemView) {
super(itemView);
button = (Button) itemView.findViewById(...);
textview = (TextView) itemView.findViewById(....);
}
}
}
关于android - 在自定义标题中包含标题和按钮两个项目,单击按钮时必须打开新 Activity ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44797498/