代码用于recyclerview我想分别在子选项中实现点击操作。 我应该如何实现此代码下面的给定代码?
这是我的项目代码,包含适配器,子项,父项
adapter.java
package com.blipclap.engineering_solution.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.blipclap.engineering_solution.Models.TitleChild;
import com.blipclap.engineering_solution.Models.TitleParent;
import com.blipclap.engineering_solution.R;
import com.blipclap.engineering_solution.ViewHolder.TitleChildViewHolder;
import com.blipclap.engineering_solution.ViewHolder.TitleParentViewHolder;
import java.util.List;
public class adapter extends ExpandableRecyclerAdapter<TitleParentViewHolder,TitleChildViewHolder> {
LayoutInflater inflater;
public adapter(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.op1.setText(title.getop1());
titleChildViewHolder.op2.setText(title.getop2());
titleChildViewHolder.op3.setText(title.getop3());
titleChildViewHolder.op4.setText(title.getop4());
titleChildViewHolder.op5.setText(title.getop5());
}
}
TitleChild.java
package com.blipclap.engineering_solution.Models;
public class TitleChild {
public String op1;
public String op2;
public String op3;
public String op4;
public String op5;
public TitleChild(String op1, String op2, String op3, String op4,String op5) {
this.op1 = op1;
this.op2 = op2;
this.op3 = op3;
this.op4 = op4;
this.op5 = op5;
}
public String getop1() {return op1;}
public void setop1(String op1) {this.op1 = op1;}
public String getop2() {return op2;}
public void setop2(String op2) {this.op2 = op2;}
public String getop3() {return op3;}
public void setop3(String op3) {this.op3 = op3;}
public String getop4() {return op4;}
public void setop4(String op4) {this.op4 = op4;}
public String getop5() {return op5;}
public void setop5(String op5) {this.op5 = op5;}
}
TitleCreator.java
package com.blipclap.engineering_solution.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<>();
for (int i=1;i<=8;i++)
{
TitleParent title = new TitleParent(String.format("SEM%d",i));
_titleParents.add(title);
}
}
public static TitleCreator get(Context context)
{
if (_titleCreator==null)
_titleCreator=new TitleCreator(context);
return _titleCreator;
}
public List<TitleParent> getall() {
return _titleParents;
}
}
**TitleParent.java**
package com.blipclap.engineering_solution.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;
}
}
TitleChildViewHolder.java
package com.blipclap.engineering_solution.ViewHolder;
import android.view.View;
import android.widget.TextView;
import com.bignerdranch.expandablerecyclerview.ViewHolder.ChildViewHolder;
import com.blipclap.engineering_solution.R;
public class TitleChildViewHolder extends ChildViewHolder {
public TextView op1,op2,op3,op4,op5;
public TitleChildViewHolder(View itemView) {
super(itemView);
op1 =(TextView)itemView.findViewById(R.id.op1);
op2 =(TextView)itemView.findViewById(R.id.op2);
op3 =(TextView)itemView.findViewById(R.id.op3);
op4 =(TextView)itemView.findViewById(R.id.op4);
op5 =(TextView)itemView.findViewById(R.id.op5);
}
}
TitleParentViewHolder.java
package com.blipclap.engineering_solution.ViewHolder;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import com.bignerdranch.expandablerecyclerview.ViewHolder.ParentViewHolder;
import com.blipclap.engineering_solution.R;
public class TitleParentViewHolder extends ParentViewHolder {
public TextView _textview;
public ImageButton _imagebutton;
public TitleParentViewHolder(View itemView) {
super(itemView);
_textview = (TextView)itemView.findViewById(R.id.parentTitle);
_imagebutton =(ImageButton) itemView.findViewById(R.id.expandArrow);
}
}
SYFragment.java
package com.blipclap.engineering_solution;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.bignerdranch.expandablerecyclerview.Model.ParentObject;
import com.blipclap.engineering_solution.Adapter.adapter;
import com.blipclap.engineering_solution.Models.TitleChild;
import com.blipclap.engineering_solution.Models.TitleCreator;
import com.blipclap.engineering_solution.Models.TitleParent;
import java.util.ArrayList;
import java.util.List;
public class SYFragment extends Fragment {
RecyclerView recyclerView;
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
((adapter)recyclerView.getAdapter()).onSaveInstanceState(outState);
}
private List<ParentObject> initData() {
TitleCreator titleCreator =TitleCreator.get(getActivity());
List<TitleParent> titles = titleCreator.getall();
List<ParentObject> parentObjects =new ArrayList<>();
for (TitleParent title:titles)
{
List<Object> childList =new ArrayList<>();
childList.add(new TitleChild("I.T","C.E","EXTC","MECH","CIVIL" ));
title.setChildObjectList(childList);
parentObjects.add(title);
}
return parentObjects;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//returning our layout file
//change R.layout.yourlayoutfilename for each of your fragments
super.onCreate(savedInstanceState);
View rootView =inflater.inflate(R.layout.fragment_sy, container, false);
recyclerView =(RecyclerView)rootView.findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
adapter adapter =new adapter(getActivity(),initData());
adapter.setParentClickableViewAnimationDefaultDuration();
adapter.setParentAndIconExpandOnClick(true);
recyclerView.setAdapter(adapter);
return rootView;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//you can set the title for your toolbar here for different fragments different titles
getActivity().setTitle("Syllabus");
}
}
list_child.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/op1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="op1" />
<TextView
android:id="@+id/op2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/op1"
android:padding="8dp"
android:text="op2" />
<TextView
android:id="@+id/op3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/op2"
android:padding="8dp"
android:text="op3" />
<TextView
android:id="@+id/op4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/op3"
android:padding="8dp"
android:text="op4" />
<TextView
android:id="@+id/op5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/op4"
android:padding="8dp"
android:text="op5" />
</RelativeLayout>
</android.support.v7.widget.CardView>
**list_parent.xml**
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/parentTitle"
android:padding="16dp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/expandArrow"
android:visibility="gone"
android:layout_alignParentRight="true"
android:layout_margin="8dp"
android:src="@android:drawable/arrow_down_float"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
fragment_sy.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.blipclap.engineering_solution.SYFragment">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recyclerview"></android.support.v7.widget.RecyclerView>
</FrameLayout>
点击代码
我应该如何在我的项目中实现此代码,应该像每次点击clid选项时特定的pdf应该打开
任何人都可以提供帮助。
每当我添加这个,我最终都会有错误。
@Override
public void onBindChildViewHolder(IssueViewHolder issueViewHolder, int position, Object childListItem) {
Issue issue = (Issue) childListItem;
issueViewHolder.bind(issue);
issueViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//your code
}
});
}
答案 0 :(得分:1)
在适配器类中定义接口
public interface onItemClickListener {
void onItemClicked(View view, int position);
}
public void setOnItemClickListener(onItemClickListener listener) {
this.onItemClickListener = listener;
}
在自定义视图持有人实施View.OnClickListner
并设置Click Listener以获取所需视图。
public static class CustomViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
CustomViewHolder(View itemView){
super(itemView);
yourview.setOnClickListener(this);
}
@Override
public void onClick(View view) {
onItemClickListener.onItemClicked(view, getAdapterPosition());
}
}
现在在Adapter对象中添加setOnItemClickListener,您可以使用视图的id分叉click事件。
yourAdapter.setOnItemClickListener(new YourAdapter.onItemClickListener() {
@Override
public void onItemClicked(View view, int position) {
// view.getId()
});