无法从Recycler Adapter启动Activity

时间:2015-09-30 14:02:33

标签: android android-activity recycler-adapter

我试图找到解决方案,但我的弱编程技巧并没有帮助我解决问题。

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

public class GridAdapter extends RecyclerView.Adapter<GridAdapter.ViewHolder> {
List<HeroItem> mItems;
Context mContext;
MyOnItemClickListener listener;

public GridAdapter(Context context) {
    super();
    this.mContext = context;
    mItems = new ArrayList<HeroItem>();
    HeroItem species = new HeroItem();
    //species.setName("Amur Leopard");
    species.setThumbnail(R.drawable.test1);
    mItems.add(species);


    species = new HeroItem();
    //species.setName("Black Rhino");
    species.setThumbnail(R.drawable.test2);
    mItems.add(species);

    species = new HeroItem();
    //species.setName("Orangutan");
    species.setThumbnail(R.drawable.test3);
    mItems.add(species);

    species = new HeroItem();
    //species.setName("Sea Lions");
    species.setThumbnail(R.drawable.test4);
    mItems.add(species);

    species = new HeroItem();
    //species.setName("Indian Elephant");
    species.setThumbnail(R.drawable.test5);
    mItems.add(species);

    species = new HeroItem();
    // species.setName("Giant Panda");
    species.setThumbnail(R.drawable.test6);
    mItems.add(species);

    species = new HeroItem();
    //species.setName("Snow Leopard");
    species.setThumbnail(R.drawable.test1);
    mItems.add(species);

    species = new HeroItem();
    //species.setName("Dolphin");
    species.setThumbnail(R.drawable.test1);
    mItems.add(species);
}
@Override
public GridAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, final int i) {
    View v = LayoutInflater.from(viewGroup.getContext())
            .inflate(R.layout.grid_item, viewGroup, false);
    return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
    HeroItem nature = mItems.get(position);
    //viewHolder.tvspecies.setText(nature.getName());
    //viewHolder.imgThumbnail.setImageResource(nature.getThumbnail());
    Picasso.with(mContext)
            .load(nature.getThumbnail())
            .into(viewHolder.imgThumbnail);
    viewHolder.setListener(new MyOnItemClickListener() {
        @Override
        public void MyOnClickListener(int position) {
            //Log.d("Click","Position: " + position);
            switch (position) {
                case 0:
                    Intent intent = new Intent(mContext, Wolverine.class);
                    mContext.startActivity(intent);
                    break;
            }
        }
    });
}
@Override
public int getItemCount() {
    return mItems.size();
}


class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    public ImageView imgThumbnail;
    //public TextView tvspecies;

    public ViewHolder(View itemView) {
        super(itemView);
        imgThumbnail = (ImageView) itemView.findViewById(R.id.photoView);
        //tvspecies = (TextView)itemView.findViewById(R.id.tv_species);
        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        listener.MyOnClickListener(getAdapterPosition());
    }

    public void setListener(MyOnItemClickListener onItemClick) {
        listener = onItemClick;
    }
}

public interface MyOnItemClickListener {
    void MyOnClickListener(int position);
}
}

所以,正如您所见,我编写了自己的listener position item position。我在日志Log.d("Click","Position: " + position);中显示fragment Activity没有问题,但我仍然无法启动新活动。

以下是使用adapter

import android.graphics.Point; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.DisplayMetrics; import android.view.Display; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; public class TabHero extends Fragment { RecyclerView mRecyclerView; RecyclerView.LayoutManager mLayoutManager; RecyclerView.Adapter mAdapter; int widthRecycler,widthScreen,padding; public static TabHero newInstance() { Bundle args = new Bundle(); TabHero fragment = new TabHero(); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.tab_hero, container, false); // Calling the RecyclerView mRecyclerView = (RecyclerView) v.findViewById(R.id.recycler_view); mRecyclerView.setHasFixedSize(true); // The number of Columns mLayoutManager = new GridLayoutManager(getActivity(), 2); mRecyclerView.setLayoutManager(mLayoutManager); mAdapter = new GridAdapter(getContext()); mRecyclerView.setAdapter(mAdapter); return v; } 代码
viewer.clock.onTick

}

我做错了什么?

1 个答案:

答案 0 :(得分:0)

itemViewHolder.CategoryItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i=new Intent(context, SubcatogaryActivity.class);
                i.putExtra("id",Integer.toString(received_dataList.get(position).getId()));

                context.startActivity(i);
            }
        });