gpt4 book ai didi

java - 节项 : Get Section Name on Tap of Item

转载 作者:行者123 更新时间:2023-12-02 13:17:21 24 4
gpt4 key购买 nike

当我点击该特定部分的任何项目时,尝试获取部分的名称。

我正在关注this教程,其中我的 UI 数据分为两部分:部分和项目

现在据我所知,我想查看部分的名称(一旦我点击任何项目)

举个例子,我有 5 个部分,每个部分本身包含 5 个项目,假设我已经点击了第 2 部分的第 4 个项目

那么我怎么知道,正如你所看到的,我正在获取点击项目的名称,但在点击时我还想获取部分的名称(我刚刚点击的项目)?

public class SingleItemRowHolder extends RecyclerView.ViewHolder {

....

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

.....

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

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

}
});

}

最佳答案

看到您提供的链接后,您只需将节名称传递并存储在 SectionListAdapter 中,如下所示:

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

private ArrayList<SingleItemModel> itemsList;
private Context mContext;
private String mSectionName;

public SectionListDataAdapter(Context context, String sectionName, ArrayList<SingleItemModel> itemsList) {
mSectionName = sectionName;
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) {

SingleItemModel singleItem = itemsList.get(i);

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


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

@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.tvTitle);
this.itemImage = (ImageView) view.findViewById(R.id.itemImage);


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


Toast.makeText(v.getContext(), mSectionName +" : "+ tvTitle.getText(), Toast.LENGTH_SHORT).show();

}
});


}

}

}

现在,在您的RecyclerViewDataAdapter中,只需更改onBindViewHolder内初始化SectionListDataAdapter的一行,如下所示:

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

private ArrayList<SectionDataModel> dataList;
private Context mContext;

public RecyclerViewDataAdapter(Context context, ArrayList<SectionDataModel> 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).getHeaderTitle();

ArrayList singleSectionItems = dataList.get(i).getAllItemsInSection();

itemRowHolder.itemTitle.setText(sectionName);

SectionListDataAdapter itemListDataAdapter = new SectionListDataAdapter(mContext, sectionName, 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.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);


}

}

}

关于java - 节项 : Get Section Name on Tap of Item,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43719398/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com