Recyclerview内的Recyclerview,获取父适配器内子行的点击位置 [英] Recyclerview inside Recyclerview , get click position of child row inside parent Adapter
问题描述
我的项目要求是,活动&我在列表中显示日期价格,我希望用户选择日期,日期颜色将更改,并且在点击购物车商品时应将其添加到卡中.
In my project requirement is as there are list of items are showing in Activity & i'm showing dates price with in list, what i want user will select date, the date color will change and when tap on cart item should be added to card.
问题:我无法获得父recyclerView onBindViewHolder
中的子recyclerView位置.
Problem : I am not able to get child recyclerView position inside parent recyclerView onBindViewHolder
.
有什么方法可以获取父行reyclerview中的子行(日期行)的位置.
Is there any way to get position of child row (dates row) inside parent reyclerview.
下面是图像,显示了三个列表,每个列表中都有日期的行列表.当用户点击特定日期时,我希望该日期位于onBindViewHolder中的父recyclerView Adapter类上.
Below is the image , showing with three lists and in every list there are list of rows of dates. When users taps on particular date i want that date on parent recyclerView Adapter class inside onBindViewHolder.
以下是适配器:
Below are the adapters :
下面是父适配器类
FlowerListAdapter.java :
public class FlowerListAdapter extends RecyclerView.Adapter<FlowerListAdapter.MyViewHolder> {
ArrayList<FlowerListPojo> list;
Context context;
public FlowerListAdapter(Context context, ArrayList<FlowerListPojo> list) {
this.context = context;
this.list = list;
}
//Pagination
public void updateList(ArrayList<FlowerListPojo> list) {
this.list.addAll(list);
this.notifyDataSetChanged();
}
@Override
public FlowerListAdapter.MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.flower_list_row, viewGroup, false);
return new FlowerListAdapter.MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(FlowerListAdapter.MyViewHolder holder, int position) {
LinearLayoutManager mLayoutManager;
holder.name.setText(list.get(position).getInfo().getName());
ArrayList<CalenderPojo> listCal = new ArrayList<>();
Glide.with(context).load(list.get(position).getInfo().getImage())
.thumbnail(0.5f)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(holder.imageFlower);
mLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
holder.recyclerView.setLayoutManager(mLayoutManager);
holder.recyclerView.setItemAnimator(new DefaultItemAnimator());
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
//todays date
Date cToday = Calendar.getInstance().getTime();
String todaysDate = df.format(cToday);
//last day last next 90 days
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, 90);
Date d = c.getTime();
String lastDate = df.format(d);
List<Date> dates = getDates(todaysDate, lastDate);
for (Date date : dates) {
String dayOfTheWeek = (String) DateFormat.format("EEE", date); // Thursday
String day = (String) DateFormat.format("dd", date); // 20
String monthString = (String) DateFormat.format("MMMM", date); // Jun
String monthNumber = (String) DateFormat.format("MM", date); // 06
String year = (String) DateFormat.format("yyyy", date); // 2013
listCal.add(new CalenderPojo(dayOfTheWeek, day, "200", monthString + " " + year));
}
holder.recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
int firstVisiblePosition = mLayoutManager.findFirstVisibleItemPosition();
if(firstVisiblePosition>=0)
holder.monthName.setText(listCal.get(firstVisiblePosition+3).getMonth());
}
});
holder.recyclerView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
//Date send to Adapter / Constructor call
holder.adapter = new CalenderAdapter(context, listCal);
holder.recyclerView.setAdapter(holder.adapter);
}
@Override
public int getItemCount() {
if (list.size() != 0)
return list.size();
else return 0;
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView name;
ImageView imageFlower;
RecyclerView recyclerView;
TextView monthName;
CalenderAdapter adapter;
public MyViewHolder(View itemView) {
super(itemView);
name = itemView.findViewById(R.id.flowerNameFLR);
imageFlower = itemView.findViewById(R.id.flowerImgFLR);
recyclerView = itemView.findViewById(R.id.recycler_view_calender);
monthName = itemView.findViewById(R.id.monthName);
}
}
private static List<Date> getDates(String dateString1, String dateString2) {
ArrayList<Date> dates = new ArrayList<Date>();
java.text.DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = null;
Date date2 = null;
try {
date1 = df1.parse(dateString1);
date2 = df1.parse(dateString2);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cal1 = Calendar.getInstance();
cal1.setTime(date1);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(date2);
while (!cal1.after(cal2)) {
dates.add(cal1.getTime());
cal1.add(Calendar.DATE, 1);
}
return dates;
}
}
下面是子适配器类
CalenderAdapter.java
public class CalenderAdapter extends RecyclerView.Adapter<CalenderAdapter.MyViewHolder> {
ArrayList<CalenderPojo> list;
Context context;
private int mSelectedItem = -1;
public CalenderAdapter(Context context, ArrayList<CalenderPojo> listCal) {
this.context = context;
this.list = listCal;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.date_row, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
final CalenderPojo listPotn = list.get(position);
holder.day.setText(listPotn.getDay());
holder.date.setText(listPotn.getDate());
holder.price.setText("$ "+listPotn.getPrice());
holder.linearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mSelectedItem = position;
notifyDataSetChanged();
// ((FlowerListAdapter)context).sendTimedetails(position);
}
});
if (mSelectedItem==position) {
holder.itemView.setBackgroundResource(R.drawable.selected_date_back);
holder.day.setTextColor(context.getResources().getColor(R.color.white));
holder.date.setTextColor(context.getResources().getColor(R.color.white));
holder.price.setTextColor(context.getResources().getColor(R.color.white));
} else {
holder.linearLayout.setBackgroundColor(context.getResources().getColor(R.color.primaryLight2));
holder.day.setTextColor(context.getResources().getColor(R.color.black));
holder.date.setTextColor(context.getResources().getColor(R.color.black));
holder.price.setTextColor(context.getResources().getColor(R.color.black));
}
}
@Override
public int getItemCount() {
if (list.size() != 0 && list !=null)
return list.size();
else return 0;
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView day, date, price;
LinearLayout linearLayout;
public MyViewHolder(View itemView) {
super(itemView);
day = itemView.findViewById(R.id.day);
date = itemView.findViewById(R.id.date);
price = itemView.findViewById(R.id.price);
linearLayout = itemView.findViewById(R.id.lLayout);
}
}
}
推荐答案
尝试一下
像这样创建一个
interface
public interface ClickPosition {
public void getPosition(int position);
}
在
FlowerListAdapter.java :
public class FlowerListAdapter extends RecyclerView.Adapter<FlowerListAdapter.MyViewHolder> {
ArrayList<FlowerListPojo> list;
Context context;
ClickPosition clickPosition;
public FlowerListAdapter(Context context, ArrayList<FlowerListPojo> list) {
this.context = context;
this.list = list;
}
//Pagination
public void updateList(ArrayList<FlowerListPojo> list) {
this.list.addAll(list);
this.notifyDataSetChanged();
}
@Override
public FlowerListAdapter.MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.flower_list_row, viewGroup, false);
return new FlowerListAdapter.MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(FlowerListAdapter.MyViewHolder holder, int position) {
LinearLayoutManager mLayoutManager;
holder.name.setText(list.get(position).getInfo().getName());
ArrayList<CalenderPojo> listCal = new ArrayList<>();
Glide.with(context).load(list.get(position).getInfo().getImage())
.thumbnail(0.5f)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(holder.imageFlower);
mLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
holder.recyclerView.setLayoutManager(mLayoutManager);
holder.recyclerView.setItemAnimator(new DefaultItemAnimator());
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
//todays date
Date cToday = Calendar.getInstance().getTime();
String todaysDate = df.format(cToday);
//last day last next 90 days
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, 90);
Date d = c.getTime();
String lastDate = df.format(d);
List<Date> dates = getDates(todaysDate, lastDate);
for (Date date : dates) {
String dayOfTheWeek = (String) DateFormat.format("EEE", date); // Thursday
String day = (String) DateFormat.format("dd", date); // 20
String monthString = (String) DateFormat.format("MMMM", date); // Jun
String monthNumber = (String) DateFormat.format("MM", date); // 06
String year = (String) DateFormat.format("yyyy", date); // 2013
listCal.add(new CalenderPojo(dayOfTheWeek, day, "200", monthString + " " + year));
}
holder.recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
int firstVisiblePosition = mLayoutManager.findFirstVisibleItemPosition();
if(firstVisiblePosition>=0)
holder.monthName.setText(listCal.get(firstVisiblePosition+3).getMonth());
}
});
holder.recyclerView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
//Date send to Adapter / Constructor call
holder.adapter = new CalenderAdapter(context, listCal,clickPosition);
holder.recyclerView.setAdapter(holder.adapter);
}
@Override
public int getItemCount() {
if (list.size() != 0)
return list.size();
else return 0;
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView name;
ImageView imageFlower;
RecyclerView recyclerView;
TextView monthName;
CalenderAdapter adapter;
public MyViewHolder(View itemView) {
super(itemView);
clickPosition= new ClickPosition() {
@Override
public void getPosition(int position) {
Toast.makeText(context, ""+position, Toast.LENGTH_SHORT).show();
}
};
name = itemView.findViewById(R.id.flowerNameFLR);
imageFlower = itemView.findViewById(R.id.flowerImgFLR);
recyclerView = itemView.findViewById(R.id.recycler_view_calender);
monthName = itemView.findViewById(R.id.monthName);
}
}
private static List<Date> getDates(String dateString1, String dateString2) {
ArrayList<Date> dates = new ArrayList<Date>();
java.text.DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = null;
Date date2 = null;
try {
date1 = df1.parse(dateString1);
date2 = df1.parse(dateString2);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cal1 = Calendar.getInstance();
cal1.setTime(date1);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(date2);
while (!cal1.after(cal2)) {
dates.add(cal1.getTime());
cal1.add(Calendar.DATE, 1);
}
return dates;
}
}
在CalenderAdapter.java中进行以下更改:
Make below change in your CalenderAdapter.java :
public class CalenderAdapter extends RecyclerView.Adapter<CalenderAdapter.MyViewHolder> {
ArrayList<CalenderPojo> list;
Context context;
private int mSelectedItem = -1;
ClickPosition clickPosition;
public CalenderAdapter(Context context, ArrayList<CalenderPojo> listCal, ClickPosition clickPosition) {
this.context = context;
this.list = listCal;
this.clickPosition = clickPosition;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.date_row, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
final CalenderPojo listPotn = list.get(position);
holder.day.setText(listPotn.getDay());
holder.date.setText(listPotn.getDate());
holder.price.setText("$ "+listPotn.getPrice());
holder.linearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mSelectedItem = position;
notifyDataSetChanged();
clickPosition.getPosition(position);
};
// ((FlowerListAdapter)context).sendTimedetails(position);
}
});
if (mSelectedItem==position) {
holder.itemView.setBackgroundResource(R.drawable.selected_date_back);
holder.day.setTextColor(context.getResources().getColor(R.color.white));
holder.date.setTextColor(context.getResources().getColor(R.color.white));
holder.price.setTextColor(context.getResources().getColor(R.color.white));
} else {
holder.linearLayout.setBackgroundColor(context.getResources().getColor(R.color.primaryLight2));
holder.day.setTextColor(context.getResources().getColor(R.color.black));
holder.date.setTextColor(context.getResources().getColor(R.color.black));
holder.price.setTextColor(context.getResources().getColor(R.color.black));
}
}
@Override
public int getItemCount() {
if (list.size() != 0 && list !=null)
return list.size();
else return 0;
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView day, date, price;
LinearLayout linearLayout;
public MyViewHolder(View itemView) {
super(itemView);
day = itemView.findViewById(R.id.day);
date = itemView.findViewById(R.id.date);
price = itemView.findViewById(R.id.price);
linearLayout = itemView.findViewById(R.id.lLayout);
}
}
}
这篇关于Recyclerview内的Recyclerview,获取父适配器内子行的点击位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!