如何在单击嵌套的recyclerview的imageview后从父回收站视图更新imageview

时间:2017-12-04 12:31:29

标签: android android-layout android-recyclerview

请检查以下屏幕截图,当用户从嵌套的recyclerview点击imageview时,我想从父级recyclelerview更新imageview。 我为父母带来了两个独立的适配器。嵌套的recyclerview。我无法执行更新图像的功能,请帮忙。

enter image description here

父级Recyclerview适配器:

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

private ArrayList<PLDModel> dataList;
private Context mContext;
public RecyclerViewDataAdapter(Context context, ArrayList<PLDModel> dataList) {
    this.dataList = dataList;
    this.mContext = context;
}

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

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

    final String itemTitle = dataList.get(i).getTitle();
    final String itemDescription = dataList.get(i).getDescription();

    ArrayList<SmallImages> singleSectionItems = dataList.get(i).getSmallImages();

    itemRowHolder.itemTitle.setText(Html.fromHtml("<b>" + itemTitle + " </b> " + itemDescription));

    SectionListDataAdapter itemListDataAdapter = new SectionListDataAdapter(mContext, singleSectionItems);

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

}

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

public class ItemRowHolder extends RecyclerView.ViewHolder {

    protected TextView itemTitle,  expandImage;
    protected ImageView bookmarkImage,largeImage;
    protected RecyclerView recyclerSmallImageList;

    protected Button btnMore;


    public ItemRowHolder(View view) {
        super(view);
        this.itemTitle = (TextView) view.findViewById(R.id.title);
        this.bookmarkImage = (ImageView) view.findViewById(R.id.bookmark);
        this.largeImage = (ImageView) view.findViewById(R.id.large_image);
        this.expandImage = (TextView) view.findViewById(R.id.expand);
        this.recyclerSmallImageList = (RecyclerView) view.findViewById(R.id.recycler_small_image_list);

    }

}

}

嵌套的Recyclerview适配器:

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

private ArrayList<SmallImages> itemsList;
private Context mContext;

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

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

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

    SmallImages singleItem = itemsList.get(i);
}

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

public class SingleItemRowHolder extends RecyclerView.ViewHolder {

    protected ImageView itemImage;

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

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


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


                //Toast.makeText(v.getContext(), tvTitle.getText(), Toast.LENGTH_SHORT).show();

            }
        });


    }

}

}

2 个答案:

答案 0 :(得分:1)

使用两个Recyclerview将难以控制而不是使用单个适配器并从那里控制所有内容。我刚刚处理过这种类型的事情,这就是为什么我发布我的代码可能会有一些不需要的代码你可能需要。

/////适配器类

public class AdapterTodayTrip extends RecyclerView.Adapter<AdapterTodayTrip.VHItem> {

    private Context mContext;
    private int rowLayout;
    private List<ModelRouteDetailsUp> dataMembers;
    private ArrayList<ModelRouteDetailsUp> arraylist;
    private ArrayList<ModelKidDetailsUp> arraylist_kids;
    List<String> wordList = new ArrayList<>();
    Random rnd = new Random();
    int randomNumberFromArray;

    private ModelRouteDetailsUp personaldata;
    private ProgressDialog pDialog;
    private ConnectionDetector cd;
    String img_baseurl = "";
    String item = "";

    public AdapterTodayTrip(Context mcontext, int rowLayout, List<ModelRouteDetailsUp> tripList, String flag, String img_baseurl) {
        this.mContext = mcontext;
        this.rowLayout = rowLayout;
        this.dataMembers = tripList;
        wordList.clear();
        this.img_baseurl = img_baseurl;
        arraylist = new ArrayList<>();
        arraylist_kids = new ArrayList<>();
        arraylist.addAll(dataMembers);
        cd = new ConnectionDetector(mcontext);
        pDialog = KPUtils.initializeProgressDialog(mcontext);
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public AdapterTodayTrip.VHItem onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(rowLayout, viewGroup, false);
        return new AdapterTodayTrip.VHItem(v);
    }

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

    @Override
    public void onBindViewHolder(final AdapterTodayTrip.VHItem viewHolder, final int position) {
        viewHolder.setIsRecyclable(false);
        try {
            personaldata = dataMembers.get(position);
            if (!KPHashmapUtils.m_ride_route_details_up.get(position).getKidpool_route_id().isEmpty() && !KPHashmapUtils.m_ride_route_details_up.get(position).getKidpool_route_id().equals("null")) {
                viewHolder.tv_trip_id.setText("#" + KPHashmapUtils.m_ride_route_details_up.get(position).getKidpool_route_id());
            }

            ****///////inflate the child list here and onclick on the image below in the inflated view it will load the image in the main view****
            if (personaldata.getKidlist().size() > 0) {
                viewHolder.linear_childview.setVisibility(View.VISIBLE);
                viewHolder.tv_total_count.setText(""+personaldata.getKidlist().size());
                viewHolder.id_gallery.removeAllViews();
                LinearLayout.LayoutParams buttonLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                buttonLayoutParams.setMargins(0, 0, 8, 0);
                LayoutInflater layoutInflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                for (int i = 0; i < personaldata.getKidlist().size(); i++) {
                    View view = layoutInflater.inflate(R.layout.view_child_list, null);
                    view.setLayoutParams(buttonLayoutParams);
                    RelativeLayout rl_txt = (RelativeLayout)view.findViewById(R.id.rl_txt);
                    RelativeLayout rl_img = (RelativeLayout)view.findViewById(R.id.rl_img);

                    TextView tv_count = (TextView)view.findViewById(R.id.tv_count);
                    com.app.kidpooldriver.helper.CircularTextView tv_name = (com.app.kidpooldriver.helper.CircularTextView)view.findViewById(R.id.tv_name);
                    final CircleImageView iv_circular = (CircleImageView)view.findViewById(R.id.iv_circular);
                    int count = i + 1;
                    String count1 = "0";
                    if (count <= 10) {
                        count1 = "0" + count;
                    }

                    tv_count.setText(String.valueOf(count1));
                    viewHolder.id_gallery.addView(view);
                    final String baseurl = img_baseurl + "" + personaldata.getKidlist().get(i).getKid_image();
**/////set the url of the small image in the tag here**
                    if(!baseurl.isEmpty()) {
                        iv_circular.setTag(baseurl);
                    }
                    if (!personaldata.getKidlist().get(i).getKid_image().isEmpty()) {
                        GradientDrawable bgShape = (GradientDrawable) rl_img.getBackground();
                        bgShape.setColor(Color.parseColor("#A6b1a7a6"));
                        rl_txt.setVisibility(View.GONE);
                        //rl_img.setVisibility(View.VISIBLE);
                        tv_name.setVisibility(View.GONE);
                        Log.d("aimg_baseurl", baseurl);
                        try {
                            Picasso.with(mContext)
                                    .load(baseurl)
                                    .resize(60,60)
                                    .centerCrop()
                                    .into(iv_circular);

                            iv_circular.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    String url=iv_circular.getTag().toString().trim();
                                    if(!url.isEmpty())
                                    KPUtils.showToastShort(mContext,url);
                                    Picasso.with(mContext)
                                            .load(url)
                                            .resize(60,60)
                                            .centerCrop()
                                            .into(viewHolder.img_child);
                                }
                            });

                        } catch (Exception e) {
                        }
                    } else {

                    }
                }
            }else{
                viewHolder.linear_childview.setVisibility(View.GONE);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    class VHItem extends RecyclerView.ViewHolder {
        CardView cv_members;
        ImageView img_child;
        TextView tv_trip_id, tv_trip_status, tv_vehicle_number, tv_trip_start_time, tv_trip_end_time, tv_trip_way, tv_total_count;
        LinearLayout id_gallery,linear_childview;

        public VHItem(View itemView) {
            super(itemView);
            img_child= (ImageView) itemView.findViewById(R.id.img_child);
            cv_members = (CardView) itemView.findViewById(R.id.cv_members);
            tv_trip_id = (TextView) itemView.findViewById(R.id.tv_trip_id);
            tv_trip_status = (TextView) itemView.findViewById(R.id.tv_trip_status);
            tv_vehicle_number = (TextView) itemView.findViewById(R.id.tv_vehicle_number);
            tv_trip_start_time = (TextView) itemView.findViewById(R.id.tv_trip_start_time);
            tv_trip_end_time = (TextView) itemView.findViewById(R.id.tv_trip_end_time);
            tv_trip_way = (TextView) itemView.findViewById(R.id.tv_trip_way);
            tv_total_count = (TextView) itemView.findViewById(R.id.tv_total_count);
            id_gallery = (LinearLayout) itemView.findViewById(R.id.id_gallery);
            linear_childview= (LinearLayout) itemView.findViewById(R.id.linear_childview);
        }
    }
}

///////////////////////////这个布局在每一行都是膨胀的 view_child_list

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/iv_circular"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:src="@mipmap/ic_launcher"
        app:civ_border_color="#d27959"
        app:civ_border_width="1dp" />

    <RelativeLayout
        android:id="@+id/rl_txt"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_gravity="center"
        android:background="@drawable/gy_ring_circular"
        android:gravity="center"
        android:visibility="gone">

        <com.app.kidpooldriver.helper.CircularTextView
            android:id="@+id/tv_name"
            fontPath="fonts/Poppins-Bold.ttf"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:gravity="center"
            android:text="01"
            android:textColor="@color/white"
            android:textSize="35sp"
            tools:ignore="MissingPrefix" />

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/rl_img"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:background="@drawable/gy_ring_circular"
        android:gravity="center"
        android:visibility="visible">

        <TextView
            android:id="@+id/tv_count"
            fontPath="fonts/Poppins-Bold.ttf"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="01"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
            android:textColor="#ffffff"
            android:textStyle="bold"
            tools:ignore="MissingPrefix" />

    </RelativeLayout>
</FrameLayout>

/////这是夸大的mianlayout。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/cv_members"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/card_margin"
    android:elevation="@dimen/elevation"
    card_view:cardCornerRadius="5dp">

    <LinearLayout
        android:id="@+id/main_body"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone"
            android:layout_marginTop="@dimen/fifteen"
            android:orientation="horizontal"
            android:paddingLeft="@dimen/ten">

            <TextView
                android:id="@+id/tv_trip_id"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="#KD09201701"
                android:textColor="@color/colorPrimary"
                android:textSize="@dimen/twenty"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/tv_trip_status"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@color/light_green"
                android:gravity="center"
                android:padding="5dp"
                android:text="In Progress"
                android:textAppearance="@style/TextAppearance.AppCompat.Small"
                android:textColor="@color/black" />

        </LinearLayout>

        <TextView
            android:id="@+id/tv_vehicle_number"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingLeft="20dp"
            android:text="Route 26U-26D"
            android:visibility="gone"
            android:textAppearance="@style/TextAppearance.AppCompat.Small"
            android:textColor="@color/route_textcolor" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/tv_trip_start_time"
                android:layout_width="match_parent"
                android:visibility="gone"
                android:layout_height="match_parent"
                android:layout_marginTop="@dimen/five"
                android:paddingLeft="20dp"
                android:text="06:30am"
                android:textAppearance="@style/TextAppearance.AppCompat.Medium"
                android:textColor="@color/grey_textcolor" />

            <TextView
                android:id="@+id/tv_trip_end_time"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="@dimen/five"
                android:paddingLeft="20dp"
                android:text="08:30am"
                android:textAppearance="@style/TextAppearance.AppCompat.Medium"
                android:textColor="@color/grey_textcolor"
                android:visibility="gone" />

        </LinearLayout>

        <TextView
            android:id="@+id/tv_trip_way"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="@dimen/five"
            android:paddingLeft="20dp"
            android:visibility="gone"
            android:paddingRight="20dp"
            android:text="Chingrighata > NiccoPark > SDF > College More > DLF 1 > Eco Space"
            android:textAppearance="@style/TextAppearance.AppCompat.Small"
            android:textColor="@color/grey_textcolor"
            android:textStyle="normal" />
        <ImageView
            android:id="@+id/img_child"
            android:layout_width="200dp"
            android:layout_gravity="center"
            android:layout_height="200dp" />

        <LinearLayout
            android:id="@+id/linear_childview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="@dimen/fifteen"
            android:orientation="horizontal">

            <HorizontalScrollView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_gravity="center_vertical"
                android:layout_weight="1"
                android:scrollbars="none">

                <LinearLayout
                    android:id="@+id/id_gallery"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:orientation="horizontal" />

            </HorizontalScrollView>

            <LinearLayout
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:background="@drawable/ly_ring_circular"
                android:gravity="center_vertical">

                <TextView
                    android:id="@+id/tv_total_count"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    tools:ignore="MissingPrefix"
                    fontPath="fonts/Poppins-Bold.ttf"
                    android:text="+20"
                    android:textAppearance="@style/TextAppearance.AppCompat.Medium"
                    android:textColor="@color/white"
                    android:textStyle="bold" />

            </LinearLayout>

        </LinearLayout>
    </LinearLayout>

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

///// POJO CLASS&amp; json解析&amp;适配器/////

public class ModelRouteDetailsUp {

    String city_id;
    String area_name;
    String area_status;
    String is_active;
    String areas;

    private ArrayList<ModelKidDetailsUp> kidlist;

    ///////this is the kid list 
    public ArrayList<ModelKidDetailsUp> getKidlist() {
        return kidlist;
    }

    public void setKidlist(ArrayList<ModelKidDetailsUp> kidlist) {
        this.kidlist = kidlist;
    }
 }

**///json parsing.......**
 public boolean addRideDetails(JSONObject jsonObject) {
        Boolean flag = false;
        String isstatus = "";

        if (jsonObject != null && jsonObject.length() > 0) {
            try {
                JSONArray mainArray = jsonObject.getJSONArray("schedules");

                for (int i = 0; i < mainArray.length(); i++) {
                    ModelRouteDetailsUp modelRouteDetails = new ModelRouteDetailsUp();
                    JSONObject c = mainArray.getJSONObject(i);


                    //////   For Route Details   //////
                    JSONObject route_details = c.getJSONObject("route_details");
                    modelRouteDetails.setDs_id(route_details.optString("ds_id"));
                    modelRouteDetails.setDriver_id(route_details.optString("driver_id"));
                    modelRouteDetails.setTrip_id(route_details.optString("trip_id"));
                    modelRouteDetails.setRoute_id(route_details.optString("route_id"));
                    modelRouteDetails.setVehicle_id(route_details.optString("vehicle_id"));
                    modelRouteDetails.setStart_time(route_details.optString("start_time"));
                    modelRouteDetails.setEnd_time(route_details.optString("end_time"));

                    //////   For Allotted Kids  //////
                    JSONArray kidArray = c.getJSONArray("alloted_kids");
                    ArrayList<ModelKidDetailsUp> genre = new ArrayList<ModelKidDetailsUp>();
                    if (kidArray.length() > 0) {
                        for (int j = 0; j < kidArray.length(); j++) {
                            ModelKidDetailsUp kidDetailsUp = new ModelKidDetailsUp();
                            JSONObject kidObject = kidArray.getJSONObject(j);
                            kidDetailsUp.setKid_name(kidObject.getString("kid_name"));
                            kidDetailsUp.setKid_gender(kidObject.getString("kid_gender"));
                            kidDetailsUp.setKid_dob(kidObject.getString("kid_dob"));
                            kidDetailsUp.setKid_image(kidObject.getString("kid_image"));
                            genre.add(kidDetailsUp);
                        }
                    }
///////add the kidlist here
                    modelRouteDetails.setKidlist(genre);

////main array contains all the data i.e route details and kidlist for every row
KPHashmapUtils.m_ride_route_details_up.add(modelRouteDetails);
                    //}
                }

            } catch (Exception e) {
                e.printStackTrace();
            }


        }
        return flag;
    }



**/////adapter callfrom class**

private void showData() {
        if (KPHashmapUtils.m_ride_route_details_up.size() > 0){
            adapterTodayTrip = new AdapterTodayTrip(mContext, R.layout.list_item_todaytrip, KPHashmapUtils.m_ride_route_details_up, "TodayTrip",img_baseurl);
            rv_trip_list.setAdapter(adapterTodayTrip);
        }else {
            tv_msg.setVisibility(View.VISIBLE);
        }
    }

enter image description here

答案 1 :(得分:0)

通常,解决方案是将自定义接口侦听器传递给嵌套适配器,而嵌套适配器将在任何时候单击其项目时报告。

1。 您可以创建如下界面:

public interface INestedClicked {
   onNestedItemClicked(Drawable drawble)
}

2。 传递SectionListDataAdapter一个INestedClicked的构造函数:

SectionListDataAdapter itemListDataAdapter = newSectionListDataAdapter(mContext, singleSectionItems, new INestedClicked() {
    @Override
    void onNestedItemClicked(Drawable drawble) {
       // Do whatever you need after the click, you get the drawable here
    }
});
  1. SectionListDataAdapter的构造函数中,将侦听器的实例保存为适配器参数

    private INestedClicked listener;

  2. 4。 单击嵌套项时报告侦听器:

    view.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if(listener != null){
            listener.onNestedItemClicked(imageView.getDrawable());
        }
    }