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您需要将侦听器分配给OnBindViewWholder
方法中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);
@Override
public void onPlaceClick(int position) {
Intent intent = new Intent( MonumentsFragment.this.getActivity ()
,SelectedPlaceActivity.class );
intent.putExtra("Place Item", places.get(position));
startActivity ( intent );
}
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
问题内容: 我有一个关于将点击的Cardview数据传递到活动的问题,这里是全文: 我有一个称为“详细信息”的活动,该活动的布局中包含2个TextViews,分别是Title和Description。 我设置了一个片段(tab_1),其中包含recyclerview代码和item数据,其中的每个item都包含:title&description。 我想要的是 : 当用户单击项目时,它将打开“详细信
我有一个MainActivity和三个片段,其中包含recyclerViews,recyclerViews包含两个文本视图,我希望用户单击recyclerview,中的文本应传递给新活动,我有点新,因此如果您能给我详细的代码和一些解释,我将不胜感激。 我想点击recyclerview中的一个项目,它应该打开一个新的活动,数据(文本)应该从recyclerview传递到新的活动。 这是我的HymnM
我已经创建了一个应用程序的布局有三个标签在Android2.3。每个选项卡都有一个片段,在每个片段中,都有一个列表视图。在列表视图中显示的所有数据都来自互联网,数据总是在变化,所以每次我看那个片段的时候都想重新加载数据。例如,我现在正在观看“tab1”,当我单击“tab2”时,“tab2”中的数据将重新加载,而当我单击返回“tab1”时,“tab1”中的数据也将再次重新加载。我试过了 TabSAd
我已经尝试了所有的方法,但它不适合我。我想打开或恢复应用程序,无论屏幕打开,同时单击通知。 我使用了以下方法:
我有一个RecyclerView列表,其中显示了一个包含一些文本视图和按钮的项目。我已经将每个项目的按钮链接到特定功能。碎片看起来像这样。 这两个按钮(红色和蓝色)根据用户的状态更改其功能。e、 g.当用户未被批准时,蓝色按钮将显示“激活帐户”,红色按钮将显示“删除请求”,每个按钮都有自己的功能。按钮的所有功能都按预期工作,但有一个小问题。当我点击这两个按钮中的一个时,它有时不会注册点击。我认为这
我正在尝试动画图标在我的应用程序每当它是被点击。我实现了我想要的简单动画,但它总是动画一次后,我来到那个屏幕。我希望每次点击时,它都应该是动画,并且它应该顺时针旋转,持续时间为2秒,当再次按下时,它应该逆时针旋转,持续时间为2秒。请帮帮我 这里是代码的部分-