我做了recyclerView,每个孩子都是“详细信息”。
我现在询问当用户点击“详细信息”时如何将用户发送到下一个活动。每次点击都会将他发送给其他活动。
private List<ParentObject> initData() {
TitleCreator titleCreator = TitleCreator.get(this);
List<TitleParent> titles = titleCreator.getAll();
List<ParentObject> parentObject = new ArrayList<>();
for(TitleParent title:titles)
{
List<Object> childList = new ArrayList<>();
childList.add(new TitleChild("Detail", ""));
title.setChildObjectList(childList);
parentObject.add(title);
}
return parentObject;
}
这是我将每个孩子设置为“详细信息”的代码。
我有四个父标题,当然还有四个子标题。当用户点击第一个*父标题时,它会打开 child 标题。问题是当用户点击这个孩子标题时如何将他发送到其他活动?
这是我的适配器fil
package com.example.user_pc.glavnastranicaprojekat.Adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import
com.bignerdranch.expandablerecyclerview.Adapter.ExpandableRecyclerAdapter;
import com.bignerdranch.expandablerecyclerview.Model.ParentObject;
import com.example.user_pc.glavnastranicaprojekat.models.TitleChild;
import com.example.user_pc.glavnastranicaprojekat.models.TitleParent;
import com.example.user_pc.glavnastranicaprojekat.R;
import
com.example.user_pc.glavnastranicaprojekat.ViewHolders.TitleChildViewHolder;
import
com.example.user_pc.glavnastranicaprojekat.ViewHolders.TitleParentViewHolder;
import java.util.List;
public class MyAdapter extends
ExpandableRecyclerAdapter<TitleParentViewHolder,TitleChildViewHolder> {
LayoutInflater inflater;
public MyAdapter(Context context, List<ParentObject> parentItemList) {
super(context, parentItemList);
inflater = LayoutInflater.from(context);
}
@Override
public TitleParentViewHolder onCreateParentViewHolder(ViewGroup viewGroup) {
View view = inflater.inflate(R.layout.list_parent,viewGroup,false);
return new TitleParentViewHolder(view);
}
@Override
public TitleChildViewHolder onCreateChildViewHolder(ViewGroup viewGroup) {
View view = inflater.inflate(R.layout.list_child,viewGroup,false);
return new TitleChildViewHolder(view);
}
@Override
public void onBindParentViewHolder(TitleParentViewHolder titleParentViewHolder, int i, Object o) {
TitleParent title = (TitleParent)o;
titleParentViewHolder._textView.setText(title.getTitle());
}
@Override
public void onBindChildViewHolder(TitleChildViewHolder titleChildViewHolder, int i, Object o) {
TitleChild title = (TitleChild)o;
titleChildViewHolder.option1.setText(title.getOption1());
titleChildViewHolder.option2.setText(title.getOption2());
}
}
TitleParent.java:
package com.example.user_pc.glavnastranicaprojekat.models;
import com.bignerdranch.expandablerecyclerview.Model.ParentObject;
import java.util.List;
import java.util.UUID;
public class TitleParent implements ParentObject{
private List<Object> mChildrenList;
private UUID _id;
private String title;
public TitleParent(String title) {
this.title = title;
_id = UUID.randomUUID();
}
public UUID get_id() {
return _id;
}
public void set_id(UUID _id) {
this._id = _id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public List<Object> getChildObjectList() {
return mChildrenList;
}
@Override
public void setChildObjectList(List<Object> list) {
mChildrenList = list;
}
}
这是TitleChild.java:
package com.example.user_pc.glavnastranicaprojekat.models;
public class TitleChild {
public String option1;
public String option2;
public TitleChild(String option1, String option2) {
this.option1 = option1;
this.option2 = option2;
}
public String getOption1() {
return option1;
}
public void setOption1(String option1) {
this.option1 = option1;
}
public String getOption2() {
return option2;
}
public void setOption2(String option2) {
this.option2 = option2;
}
}
TitleCreator.java:
package com.example.user_pc.glavnastranicaprojekat.models;
import android.content.Context;
import java.util.ArrayList;
import java.util.List;
public class TitleCreator {
static TitleCreator _titleCreator;
List<TitleParent> _titleParents;
public TitleCreator(Context context) {
_titleParents = new ArrayList<>();
TitleParent title = new TitleParent(String.format("Coppa"));
_titleParents.add(title);
TitleParent title1 = new TitleParent(String.format("Levante"));
_titleParents.add(title1);
TitleParent title2 = new TitleParent(String.format("Monument"));
_titleParents.add(title2);
TitleParent title3 = new TitleParent(String.format("Comming soon"));
_titleParents.add(title3);
}
public static TitleCreator get(Context context)
{
if(_titleCreator == null)
_titleCreator = new TitleCreator(context);
return _titleCreator;
}
public List<TitleParent> getAll() {
return _titleParents;
}
}
TitleChildViewHolder:
package com.example.user_pc.glavnastranicaprojekat.ViewHolders;
import android.view.View;
import android.widget.TextView;
import com.bignerdranch.expandablerecyclerview.ViewHolder.ChildViewHolder;
import com.example.user_pc.glavnastranicaprojekat.R;
public class TitleChildViewHolder extends ChildViewHolder {
public TextView option1,option2;
public TitleChildViewHolder(View itemView) {
super(itemView);
option1 = itemView.findViewById(R.id.option1);
option2 = itemView.findViewById(R.id.option2);
}
}
TitleParentViewHolder:
package com.example.user_pc.glavnastranicaprojekat.ViewHolders;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import com.bignerdranch.expandablerecyclerview.ViewHolder.ParentViewHolder;
import com.example.user_pc.glavnastranicaprojekat.R;
public class TitleParentViewHolder extends ParentViewHolder {
public TextView _textView;
public ImageButton _imageButton;
public TitleParentViewHolder(View itemView) {
super(itemView);
_textView = itemView.findViewById(R.id.parentTitle);
_imageButton = itemView.findViewById(R.id.expandArrow);
}
}
MainActivity:
RecyclerView recyclerView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
recyclerView = findViewById(R.id.myRecyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
MyAdapter adapter = new MyAdapter(this,initData());
adapter.setParentClickableViewAnimationDefaultDuration();
adapter.setParentAndIconExpandOnClick(true);
recyclerView.setAdapter(adapter);
}
private List<ParentObject> initData() {
TitleCreator titleCreator = TitleCreator.get(this);
List<TitleParent> titles = titleCreator.getAll();
List<ParentObject> parentObject = new ArrayList<>();
for(TitleParent title:titles)
{
List<Object> childList = new ArrayList<>();
childList.add(new TitleChild("Detail", ""));
title.setChildObjectList(childList);
parentObject.add(title);
}
return parentObject;
}
答案 0 :(得分:0)
titleChildViewHolder.itemView.setOnClickListener(
new OnClickListener(View v){
context.startActivity(new Intent(context,DESTINATION_ACTIVITY.class)));
}
);
将此代码放在onBindChildViewHolder
答案 1 :(得分:0)
尝试这样的事情:
@Override
public void onBindChildViewHolder(TitleChildViewHolder titleChildViewHolder, int i, Object o) {
TitleChild title = (TitleChild)o;
titleChildViewHolder.option1.setText(title.getOption1());
titleChildViewHolder.option2.setText(title.getOption2());
titleChildViewHolder.option1.setOnClickListener(
new OnClickListener(View v){
context.startActivity(new Intent(context,ACTIVITY_TO_LAUNCH.class)));
}
);
}
如果需要,还可以在titleChildViewHolder.option2
也许你可以添加一个视图option1和option2的容器,这样你就可以只设置一个ClickListener