简体   繁体   中英

How to start different activities on click of different recycler views

I have a RecyclerView with GridLayoutManager. On click of each Item i want tstart different acticities. I have tried but it is not working. This is my DashboardAdapter class

 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);
    }
}
 }

And in my DashBoardActivity i have

    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);

And is 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>

Please help me....

From what u have posted it seems that your View onClick() event is consumed by TextView

Please remove the :

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

tag from countryname TextView

And also change getPosition() to getAdapterPosition() as getPosition() is deprecated

Try This:

v.getContext().startActivity(intent);

Instead of:

mContext.startActivity(intent);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM