【发布时间】:2019-08-11 07:57:19
【问题描述】:
我已经制作了一个自定义 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 );
}
}
这是我想在点击时打开的 Activity
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 );
}
}
}
点击片段中的任何项目均无响应
【问题讨论】:
标签: android android-fragments onclick onclicklistener