在带有启动ActivityOnresult的recyclerView中使用RecyclerView [英] RecyclerView with in recyclerView with start ActivityOnresult

查看:75
本文介绍了在带有启动ActivityOnresult的recyclerView中使用RecyclerView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个回收站视图(A),其中包含另一个回收站视图(B).当我单击recyclerview(A)项目中的加号图标时,它将打开一个带有recyclerview(C)的新活动.长按并选择一些项目,然后单击完成.这需要在单击加号图标的recyclerview(A)项上更新一个recyclerview(B).

I have a recycler view(A) containing another recyclerview(B). When I click on the plus icon in recyclerview(A) item, it opens up a new activity with a recyclerview(C). Long pressing and selecting few items and clicking on done. This needs to update a recyclerview(B) on recyclerview (A) item on which I clicked the plus icon.

但是,相反,包含recyclerview(B)的recyclerview(A)的所有项目都正在更新.

But instead, all the items of recyclerview(A) which contains recyclerview(B) is being updated.

private void displayUpcomingEvents(View view) {
    upcomingEventsArrayList = new ArrayList<>();
    upcomingEvents = new UpcomingEvents("Meet and greet", "Coffee Connect | ", "Phython", "21 - ", "Jun - ", "22", "Jun", "21 Jun 10 AM", "Washinton");
    upcomingEventsArrayList.add(upcomingEvents);
    upcomingEvents = new UpcomingEvents("Meet and greet", "Coffee Connect | ", "Phython", "21 - ", "Jun - ", "22", "Jun", "21 Jun 10 AM", "Washinton");
    upcomingEventsArrayList.add(upcomingEvents);
    upcomingEvents = new UpcomingEvents("Meet and greet", "Coffee Connect | ", "Phython", "21 - ", "Jun - ", "22", "Jun", "21 Jun 10 AM", "Washinton");
    upcomingEventsArrayList.add(upcomingEvents);
    upcomingEventsAdapter = new RecyclerAdapterUpcomingEvents(getContext(), upcomingEventsArrayList);

    recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view_upcoming_event);

    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getAppContext());
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setHasFixedSize(true);
    recyclerView.setAdapter(upcomingEventsAdapter);

}



class RecyclerAdapterUpcomingEvents extends RecyclerView.Adapter<RecyclerAdapterUpcomingEvents.MyViewHolder> {

    private List<UpcomingEvents> upcomingEventsList;
    private Context context;
    private LayoutInflater inflater;


    RecyclerAdapterUpcomingEvents(Context context, List<UpcomingEvents> upcomingEventsList) {
        this.context = context;
        this.upcomingEventsList = upcomingEventsList;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View rootView = inflater.inflate(R.layout.card_view_upcoming_event, parent, false);
        return new RecyclerAdapterUpcomingEvents.MyViewHolder(rootView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

        UpcomingEvents upcomingEvents = upcomingEventsList.get(position);

        holder.eventUpcomingName.setText(upcomingEvents.getEventUpcomingName());
        holder.eventUpcomingType.setText(upcomingEvents.getEventUpcomingType());
        holder.eventUpcomingDate.setText(upcomingEvents.getEventUpcomingDateStart() + upcomingEvents.getEventUpcomingDateEnd());
        holder.eventUpcomingMonth.setText(upcomingEvents.getEventUpcomingMonthEnd() + upcomingEvents.getEventUpcomingMonthEnd());
        holder.eventUpcomingAlarmNotify.setText(upcomingEvents.getEventUpcomingAlarmNotify());
        holder.eventUpcomingPlace.setText(upcomingEvents.getEventUpcomingPlace());

        holder.eventUpcomingAddPeople.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Activity origin = (Activity) context;
                origin.startActivityForResult(new Intent(getAppContext(), ContactsActivity.class), CONTACT_CODE);
            }
        });


        holder.contactRecyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
        contactAdapter = new ContactAdapter(getAppContext(), R.layout.card__contact_image, contactsList);`enter code here`
        holder.contactRecyclerView.setAdapter(contactAdapter);

    }


    @Override
    public int getItemCount() {
        return upcomingEventsList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        private TextView eventUpcomingName, eventUpcomingType, eventUpcomingDate, eventUpcomingMonth, eventUpcomingAlarmNotify, eventUpcomingPlace, eventUpcomingYes, eventUpcomingInterested, eventUpcomingNo;
        private ImageView eventUpcomingAddPeople, eventUpcomingmore;
        private RecyclerView contactRecyclerView;


        MyViewHolder(View itemView) {
            super(itemView);
            eventUpcomingName = (TextView) itemView.findViewById(R.id.event_name);
            eventUpcomingType = (TextView) itemView.findViewById(R.id.event_type);
            eventUpcomingDate = (TextView) itemView.findViewById(R.id.event_date);
            eventUpcomingMonth = (TextView) itemView.findViewById(R.id.event_month);

            eventUpcomingAlarmNotify = (TextView) itemView.findViewById(R.id.event_time_notify);
            eventUpcomingPlace = (TextView) itemView.findViewById(R.id.event_place);

            eventUpcomingYes = (TextView) itemView.findViewById(R.id.event_yes);
            eventUpcomingInterested = (TextView) itemView.findViewById(R.id.event_interested);
            eventUpcomingNo = (TextView) itemView.findViewById(R.id.event_no);

            eventUpcomingmore = (ImageView) itemView.findViewById(R.id.event_more);
            eventUpcomingAddPeople = (ImageView) itemView.findViewById(R.id.hex_one_event);

            contactRecyclerView = (RecyclerView) itemView.findViewById(R.id.upcome_events_recycler);

        }
    }
}



@Override
public void onReceived(int requestCode, int resultCode, Intent data) {

    if (requestCode == CONTACT_CODE && resultCode == RESULT_OK && data != null) {

        List<Contact> contactsLists = (ArrayList<Contact>) data.getSerializableExtra("contacts");
        contactsList.clear();
        contactsList.addAll(contactsLists);

        contactAdapter.setContactList(contactsLists);

        upcomingEventsAdapter = new RecyclerAdapterUpcomingEvents(getContext(), upcomingEventsArrayList);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getAppContext());
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setHasFixedSize(true);
        recyclerView.setAdapter(upcomingEventsAdapter);
    }
}

CardView

        <android.support.v7.widget.RecyclerView
            android:id="@+id/upcome_events_recycler"
            android:layout_width="match_parent"
            android:layout_height="52dp"
            android:layout_below="@id/event_place"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="52dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp">

        </android.support.v7.widget.RecyclerView>

        <ImageView
            android:id="@+id/hex_one_event"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:layout_alignParentEnd="true"
            android:layout_below="@id/event_place"
            android:layout_margin="8dp"
            android:src="@drawable/hex_src_plus" />

推荐答案

首先通过以下方法更改代码:

First change your code by this below :

   holder.eventUpcomingAddPeople.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Activity origin = (Activity) context;
            Intent i = new Intent(getAppContext(), ContactsActivity.class);
            i.putExtra("selectedPosition", position);
            origin.startActivityForResult(i, CONTACT_CODE);
        }
    });

@Override
public void onReceived(int requestCode, int resultCode, Intent data) {

if (requestCode == CONTACT_CODE && resultCode == RESULT_OK && data != null) {
    int result = data.getIntExtra("selectedPosition","put a default value in case of null");
    List<Contact> contactsLists = (ArrayList<Contact>) data.getSerializableExtra("contacts");
    contactsList.clear();
    contactsList.addAll(contactsLists);

    contactAdapter.setContactList(contactsLists);
     upcomingEventsAdapter.notifyItemChanged(result);
}
}

在第二个活动中,当您单击完成时,您应该具有类似的内容:

on the second activity you should have something like that when you click on done :

int param = getIntent().getIntExtra("selectedPosition");
Intent resultIntent = new Intent();
resultIntent.putExtra("contacts", Your selected contacts);
resultIntent.putExtra("selectedPosition", param);

setResult(RESULT_OK, resultIntent);
finish();

希望这是清楚而有用的:)

Hope it's clear and helpful :)

这篇关于在带有启动ActivityOnresult的recyclerView中使用RecyclerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆