单击卡片视图时如何使用不同的数据打开相同的活动

时间:2017-07-18 06:01:58

标签: java android json

我创建了一个带卡片视图的水平循环视图。我创建了单卡并在循环视图中多次显示。数据已从Web服务中显示。现在这是应用程序的UI

enter image description here

现在我需要做的是当我点击任何一张牌时,应该打开一个活动。我创建了一个活动,当我点击任何卡片时,该活动应该打开。根据卡片数据,数据应该在活动中发生变化。

public class RecyclerViewDataAdapter extends RecyclerView.Adapter<RecyclerViewDataAdapter.ItemRowHolder> {

private List<Data> dataList;
private Context mContext;

public RecyclerViewDataAdapter(Context context, List<Data> dataList) {
    this.dataList = dataList;
    this.mContext = context;
}

@Override
public ItemRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item, null);
    ItemRowHolder mh = new ItemRowHolder(v);
    return mh;
}

@Override
public void onBindViewHolder(ItemRowHolder itemRowHolder, int i) {

    final String sectionName = dataList.get(i).getTitle();

    List singleSectionItems = dataList.get(i).getSection();

    itemRowHolder.itemTitle.setText(sectionName);

    SectionListDataAdapter itemListDataAdapter = new SectionListDataAdapter(mContext, singleSectionItems);

    itemRowHolder.recycler_view_list.setHasFixedSize(true);
    itemRowHolder.recycler_view_list.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
    itemRowHolder.recycler_view_list.setAdapter(itemListDataAdapter);


     itemRowHolder.recycler_view_list.setNestedScrollingEnabled(false);


   /*  itemRowHolder.recycler_view_list.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int action = event.getAction();
            switch (action) {
                case MotionEvent.ACTION_DOWN:
                    // Disallow ScrollView to intercept touch events.
                    v.getParent().requestDisallowInterceptTouchEvent(true);
                    break;
                case MotionEvent.ACTION_UP:
                    //Allow ScrollView to intercept touch events once again.
                    v.getParent().requestDisallowInterceptTouchEvent(false);
                    break;
            }
            // Handle RecyclerView touch events.
            v.onTouchEvent(event);
            return true;
        }
    });*/

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


            Toast.makeText(v.getContext(), "click event on more, "+sectionName , Toast.LENGTH_SHORT).show();



        }
    });







   /* Glide.with(mContext)
            .load(feedItem.getImageURL())
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .centerCrop()
            .error(R.drawable.bg)
            .into(feedListRowHolder.thumbView);*/
}

@Override
public int getItemCount() {
    return (null != dataList ? dataList.size() : 0);
}

public class ItemRowHolder extends RecyclerView.ViewHolder {

    protected TextView itemTitle;

    protected RecyclerView recycler_view_list;

    protected Button btnMore;



    public ItemRowHolder(View view) {
        super(view);

        this.itemTitle = (TextView) view.findViewById(R.id.itemTitle);
        this.recycler_view_list = (RecyclerView) view.findViewById(R.id.recycler_view_list);
        this.btnMore= (Button) view.findViewById(R.id.btnMore);


    }

}

}

另一个班级

public class SectionListDataAdapter extends RecyclerView.Adapter<SectionListDataAdapter.SingleItemRowHolder> {

private List<Section> itemsList;
private Context mContext;



public SectionListDataAdapter(Context context, List<Section> itemsList) {
    this.itemsList = itemsList;
    this.mContext = context;
}

@Override
public SingleItemRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_single_card, null);
    SingleItemRowHolder mh = new SingleItemRowHolder(v);
    return mh;
}

@Override
public void onBindViewHolder(SingleItemRowHolder holder, int i) {

    Section singleItem = itemsList.get(i);

    holder.tvTitle.setText(singleItem.getName());



}

@Override
public int getItemCount() {
    return (null != itemsList ? itemsList.size() : 0);
}

public class SingleItemRowHolder extends RecyclerView.ViewHolder {

    protected TextView tvTitle;

    protected ImageView itemImage;


    public SingleItemRowHolder(View view) {
        super(view);

        this.tvTitle = (TextView) view.findViewById(R.id.tvTitle1);
        this.itemImage = (ImageView) view.findViewById(R.id.itemImage);


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


                Intent intent = new Intent(v.getContext(), BooksDescription.class);
                mContext.startActivity(intent);

            }
        });


    `}

}}`

4 个答案:

答案 0 :(得分:1)

1-您可以将所需数据(例如:id或整个对象)传递给您的活动,然后在您的活动中处理: Intent.putExtra("YOUR_KEY","YOUR_SERIALIZED_OBJECT_OR_STRING");

2 - 请不要在你的onBindViewHolder中设置onOnClickListener(在你的第一个适配器中),最好把它放在你的viewHolder中。

答案 1 :(得分:1)

您可以使用intent或Bundle传递数据:

e.g。

使用意图:

Intent intent = new Intent(this, OtherActivity.class);
intent.putExtra(OtherActivity.KEY_EXTRA, yourDataObject);
startActivity(intent);

使用Bundle:

Bundle b = new Bundle();
b = getIntent().getExtras();
String name = b.getString("name");

答案 2 :(得分:0)

你需要实现项目点击监听器。要实施recyclerview项目clicklistner,请遵循此link

然后通过意图传递您的数据,link

将帮助您将数据传递给活动,然后将其传递到另一端

答案 3 :(得分:0)

只需在适配器OnClick Listener

中添加以下代码即可
Intent intent = new Intent(v.getContext(), BooksDescription.class);
intent.putExtra("YOUR KEY", yourData);
v.getContext().startActivity(intent);