gpt4 book ai didi

java - 在 RecyclerView 中我想要 onclicklistener 操作

转载 作者:行者123 更新时间:2023-12-02 11:52:08 28 4
gpt4 key购买 nike

代码是针对recyclerview的,我想单独实现子选项中的点击操作。我应该如何实现此代码下面的给定代码?

这是我的项目代码,带有适配器、子项、父项

适配器.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
}
});
}

最佳答案

在适配器类中定义接口(interface)

public interface onItemClickListener {
void onItemClicked(View view, int position);
}

public void setOnItemClickListener(onItemClickListener listener) {
this.onItemClickListener = listener;
}

在您的自定义 View 持有者上实现View.OnClickListner并为所需 View 设置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,您就可以使用 View 的 id 来 fork 点击事件。

yourAdapter.setOnItemClickListener(new YourAdapter.onItemClickListener() {
@Override
public void onItemClicked(View view, int position) {
// view.getId()
});

关于java - 在 RecyclerView 中我想要 onclicklistener 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47818254/

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