如何在点击不同的回收站视图时启动不同的活动

时间:2016-01-27 07:50:39

标签: android android-recyclerview android-cardview

我有一个带有GridLayoutManager的RecyclerView。点击每个项目我想要开始不同的活动。我试过了但是没有用。这是我的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>

请帮助我....

2 个答案:

答案 0 :(得分:3)

根据您发布的内容,onClick()

消费了您的观看TextView事件

请删除:

 android:focusable="true"
 android:clickable="true"
来自countryname TextView

标记

并且在getPosition()被弃用时也将getAdapterPosition()更改为getPosition()

答案 1 :(得分:0)

试试这个:

v.getContext().startActivity(intent);

而不是:

mContext.startActivity(intent);