android - 在传递单击的自定义列表项的数据时单击带有 RecyclerView 的片段的项目时如何打开新活动?
问题描述
我制作了一个自定义 Place 对象和 Place Adapter。我使用了 Parcelable,但是当我单击 Fragments 的任何项目时没有任何反应
尝试使用 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
项目,并将侦听器传递给适配器的构造函数。请按照以下步骤操作。
第 1 步
在类中声明监听器,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;
}
第 2 步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 () );
}
第 3 步 在片段中将侦听器传递给适配器。
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 );
}
请注意,您需要声明 List outisde才能从方法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
推荐阅读
- python-3.x - Python从两个具有相同长度的列表中获取唯一值和最小值对
- processing - 在处理 3 问题中将摩尔斯电码编码器转换为解码器
- reactjs - Material UI Select 组件上的“keepMounted”属性未将菜单项安装到 DOM
- python - 无法使用 BeautifulSoup 和 Requests 抓取下拉菜单
- c# - 使用自动换行将整个 url 保存在文本块中
- delphi - 如何在循环内同时显示图像
- import - 是否可以通过 xml 转储导入来保护 Mediawiki 页面表单覆盖?
- angular - 使用默认 Cloudfront dns 放大 cookie 身份验证不起作用
- python - 提取 JSON 标签并构建一个新的 Json
- graph - 在子图上使用算法