本文介绍了如何在传递所单击的自定义列表项的数据的同时,使用RecEconerView单击片段的项来打开新的活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经创建了一个自定义的Place对象和Place Adapter。我已经使用了Parcelable,但当我单击任何碎片项时都没有反应
已尝试使用Parcelable,但在单击任何项目时没有响应。
放置对象
public class Place implements Parcelable {
/** String resource ID for the name of the place */
private int mPlaceID;
private int mImageResourceId;
private int mNearestStation;
private int mSiteInfo;
public Place(int placeID, int imageResourceId, int NearestStation, int
SiteInfo) {
mPlaceID = placeID;
mImageResourceId = imageResourceId;
mNearestStation = NearestStation;
mSiteInfo = SiteInfo;
}
protected Place(Parcel in) {
mImageResourceId = in.readInt();
mPlaceID = in.readInt ();
mNearestStation = in.readInt ();
mSiteInfo = in.readInt ();
}
public static final Creator<Place> CREATOR = new Creator<Place>() {
@Override
public Place createFromParcel(Parcel in) {
return new Place (in);
}
@Override
public Place[] newArray(int size) {
return new Place[size];
}
};
/**
* Get the string resource ID for the name of the place.
*/
public int getPlaceName() {
return mPlaceID;
}
/**
* Return the image resource ID of the place.
*/
public int getImageResourceId() {
return mImageResourceId;
}
public int getNearestStation() {
return mNearestStation;
}
public int getSiteInfo() {
return mSiteInfo;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeInt(mImageResourceId);
parcel.writeInt(mPlaceID);
parcel.writeInt ( mNearestStation );
parcel.writeInt ( mSiteInfo );
}
}
放置适配器
public class PlaceAdapter extends
RecyclerView.Adapter<PlaceAdapter.PlaceViewHolder> {
Context mContext;
List<Place> mData;
public static class PlaceViewHolder extends RecyclerView.ViewHolder{
public TextView mTextView;
public ImageView mImageView;
public PlaceViewHolder(@NonNull View itemView) {
super ( itemView );
mTextView = itemView.findViewById ( R.id.ImageViewText );
mImageView = itemView.findViewById ( R.id.ImageView );
}
}
public PlaceAdapter(Context mContext, List<Place> mData){
this.mContext = mContext;
this.mData = mData;
}
@NonNull
@Override
public PlaceViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int
viewType) {
View v = LayoutInflater.from ( mContext ).inflate (
R.layout.list_item, parent, false );
PlaceViewHolder pvh = new PlaceViewHolder ( v );
return pvh;
}
@Override
public void onBindViewHolder(@NonNull PlaceViewHolder holder, int
position) {
holder.mTextView.setText ( mData.get ( position ).getPlaceName () );
holder.mImageView.setImageResource ( mData.get ( position
).getImageResourceId () );
}
@Override
public int getItemCount() {
return mData.size ();
}
public interface OnPlaceListener{
void onPlaceClick(int position);
}
}
这是我单击项目的片段之一
public class MonumentsFragment extends Fragment implements
PlaceAdapter.OnPlaceListener {
private RecyclerView mRecyclerView;
public MonumentsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate( R.layout.places_list, container,
false);
final List<Place> places = new ArrayList<> ();
Place place1 = new Place(R.string.monument1,
R.drawable.windsor_castle, R.string.monument1S, R.string.monument1I);
places.add(place1);
Place place2 = new Place(R.string.monument2,
R.drawable.trafalgar_square,R.string.monument2S, R.string.monument2I);
places.add(place2);
Place place3 = new Place(R.string.monument3,
R.drawable.buckingham_palace,R.string.monument3S, R.string.monument3I);
places.add(place3);
Place place4 = new Place(R.string.monument4,
R.drawable.bank_of_england,R.string.monument4S, R.string.monument4I);
places.add(place4);
Place place5 = new Place(R.string.monument5,
R.drawable.kensington_palace,R.string.monument5S, R.string.monument5I);
places.add(place5);
Place place6 = new Place(R.string.monument6,
R.drawable.london_wall,R.string.monument6S, R.string.monument6I);
places.add(place6);
mRecyclerView = (RecyclerView) rootView.findViewById (
R.id.recycler_view );
PlaceAdapter placeAdapter = new PlaceAdapter ( getContext (), places
);
mRecyclerView.setLayoutManager ( new LinearLayoutManager ( getActivity
() ) );
mRecyclerView.setAdapter ( placeAdapter );
return rootView;
}
@Override
public void onPlaceClick(int position) {
Intent intent = new Intent( MonumentsFragment.this.getActivity ()
,SelectedPlaceActivity.class );
intent.putExtra("Place Item", position);
startActivity ( intent );
}
}
这是我要在单击时打开的活动
public class SelectedPlaceActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView( R.layout.selected_place);
Intent intent = getIntent();
if (intent!=null) {
Place currentPlace = intent.getParcelableExtra("Place Item");
int imageRes = currentPlace.getImageResourceId ();
int placeName = currentPlace.getPlaceName ();
int nearestStation = currentPlace.getNearestStation ();
int moreInfo = currentPlace.getSiteInfo ();
ImageView placeImage = findViewById (R.id.selected_place_image );
TextView namePlace = findViewById ( R.id.selected_place_name );
TextView station = findViewById ( R.id.nearest_station );
TextView information = findViewById ( R.id.more_info );
placeImage.setImageResource(imageRes);
namePlace.setText(placeName);
station.setText(nearestStation);
information.setText ( moreInfo );
}
}
}
单击片段中的任何项目时无响应
推荐答案
您尚未将侦听器分配给项的onClick
,并将侦听器传递给适配器的构造函数。请按照以下步骤操作。
第一步
在PlaceAdapter
类中声明侦听器,并通过如下所示的构造函数将其赋值给它。
// declare the listener
OnPlaceListener mListener;
// pass the listener along with Context and the List
public PlaceAdapter(Context mContext, List<Place> mData, OnPlaceListener listener){
this.mContext = mContext;
this.mData = mData;
// assign the listener
this.mListener = listener;
}
第二步
您需要将侦听器分配给onBindViewHolder
方法中的itemView的onClick
@Override
public void onBindViewHolder(@NonNull PlaceViewHolder holder, int
position) {
// assign the listener here
holder.itemView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v) {
mListener.onPlaceClick(position);
}
});
holder.mTextView.setText ( mData.get ( position ).getPlaceName () );
holder.mImageView.setImageResource ( mData.get ( position
).getImageResourceId () );
}
第三步 在片段中,将侦听器传递给适配器。
PlaceAdapter placeAdapter = new PlaceAdapter(getContext(), places, this);
还,我注意到您正在将位置传递给意图。相反,您需要传递与该位置相关的Place对象
@Override
public void onPlaceClick(int position) {
Intent intent = new Intent( MonumentsFragment.this.getActivity ()
,SelectedPlaceActivity.class );
intent.putExtra("Place Item", places.get(position));
startActivity ( intent );
}
注意您需要声明列表输出onCreateView
才能从onPlaceClick
方法访问它。
public class MonumentsFragment extends Fragment implements
PlaceAdapter.OnPlaceListener {
private RecyclerView mRecyclerView;
// Declare the list here
private List<Place> places;
public MonumentsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate( R.layout.places_list, container,
false);
// initialize the list
places = new ArrayList<>();
Place place1 = new Place(R.string.monument1,
R.drawable.windsor_castle, R.string.monument1S, R.string.monument1I);
places.add(place1);
// rest of the code
这篇关于如何在传递所单击的自定义列表项的数据的同时,使用RecEconerView单击片段的项来打开新的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!