gpt4 book ai didi

android - 如何在点击不同的回收者 View 时启动不同的 Activity

转载 作者:行者123 更新时间:2023-11-29 17:24:57 26 4
gpt4 key购买 nike

我有一个带有 GridLayoutManager 的 RecyclerView。单击每个项目我想开始不同的 Activity 。我试过了,但没有用。这是我的 DashboardAdapter 类

 public class DashboardAdapter extends RecyclerView.Adapter<DashboardAdapter.ViewHolder>{

private List<DashboardPojo> countries;
private int rowLayout;
private Context mContext;

public DashboardAdapter(List<DashboardPojo> countries, int rowLayout, Context context) {
this.countries = countries;
this.rowLayout = rowLayout;
this.mContext = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(rowLayout, viewGroup, false);
return new ViewHolder(v);
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
DashboardPojo country = countries.get(i);
viewHolder.countryName.setText(country.name);
viewHolder.countryImage.setImageResource(country.getImageResourceId(mContext));
}

@Override
public int getItemCount() {
return countries == null ? 0 : countries.size();
}

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView countryName;
public ImageView countryImage;

public ViewHolder(View itemView) {
super(itemView);
mContext = itemView.getContext();
itemView.setOnClickListener(this);
countryName = (TextView) itemView.findViewById(R.id.countryName);
countryImage = (ImageView)itemView.findViewById(R.id.countryImage);
}
@Override
public void onClick(View v) {

final Intent intent;
if (getPosition() == 0){
intent = new Intent(mContext, TutorialActivity.class);
} else if (getPosition() == 1){
intent = new Intent(mContext, JobsActivity.class);
} else {
intent = new Intent(mContext, TutorialActivity.class);
}
mContext.startActivity(intent);
}
}
}

在我的 DashBoardActivity 中有

    mRecyclerView = (RecyclerView) findViewById(R.id.list);
mLayoutManager = new GridLayoutManager(this, 2);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mAdapter = new DashboardAdapter(DashboardManager.getInstance().getCountries(), R.layout.dashboard_row, this);
mRecyclerView.setAdapter(mAdapter);

并且是dashboard_row.xml

     <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_margin="5dp"
card_view:cardCornerRadius="5dp"
android:layout_height="match_parent">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/countryImage"
android:layout_width="match_parent"
android:layout_height="120dp"
android:scaleType="centerCrop"
android:tint="@color/photo_tint"
android:layout_centerInParent="true"
/>

<TextView
android:id="@+id/countryName"
android:gravity="center"
android:background="?android:selectableItemBackground"
android:focusable="true"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="100dp"
android:textSize="24sp"
android:layout_centerInParent="true"
android:textColor="@android:color/white"
/>

</RelativeLayout>

</android.support.v7.widget.CardView>

activity_dashboard.xml

         <android.support.v4.widget.DrawerLayout       xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout
android:id="@+id/rel"
android:layout_width="wrap_content"
android:layout_height="fill_parent">

<include
android:id="@+id/app_bar"
layout="@layout/app_bar" />

<android.support.v7.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DashBoardActivity"
android:layout_below="@+id/app_bar"
android:layout_marginTop="20dp"/>


</RelativeLayout>

<fragment
android:id="@+id/fragment_navigation_drawer"
android:name="solutions.techieweb.com.techiewebsolutions.NavigationDrawerFragment"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="@layout/fragment_navigation_drawer"
tools:layout="@layout/fragment_navigation_drawer" />

</android.support.v4.widget.DrawerLayout>

请帮帮我....

最佳答案

从你发布的内容来看,你的 View onClick() 事件似乎被 TextView 消耗了

请删除:

 android:focusable="true"
android:clickable="true"

标签来自 countryname TextView

同时将 getPosition() 更改为 getAdapterPosition(),因为 getPosition() 已弃用

关于android - 如何在点击不同的回收者 View 时启动不同的 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35031523/

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