在自定义标题中包含两个项目标题和按钮,而点击按钮新活动必须打开

时间:2017-06-28 08:35:07

标签: android android-recyclerview

我正在尝试像视频流应用程序这样的热门明星,在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));
            }
        });

enter image description here

2 个答案:

答案 0 :(得分:1)

尝试将所有视图放在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(....);
    }
}

}

答案 1 :(得分:0)

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

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(v.getContext(), Activity2.class);
                context.startActivity(intent);
            }
        });

    }

    @Override
    public void onClick(View v) {

        Intent intent = new Intent(v.getContext(), Activity2.class);
        context.startActivity(intent);
    }