如何使用我的回收站视图更改活动?

时间:2014-12-30 19:55:10

标签: java android android-activity android-recyclerview

我对如何使用回收站视图更改活动感到困惑!很抱歉提供多余的代码,因为我不知道在哪里编写代码或者它甚至看起来像什么!任何帮助将非常感谢! 谢谢你提前!

public class DazAdapter extends RecyclerView.Adapter<DazAdapter.MyViewHolder> {
List<Information> data= Collections.emptyList();
private LayoutInflater inflater;
public DazAdapter(Context context, List<Information> data){
    inflater=LayoutInflater.from(context);
    this.data=data;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view=inflater.inflate(R.layout.custom_row, parent,false);
    MyViewHolder holder=new MyViewHolder(view);
    return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    Information current=data.get(position);
    holder.title.setText(current.title);
    holder.icon.setImageResource(current.iconId);
}
@Override
public int getItemCount() {
    return data.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{
    TextView title;
    ImageView icon;
    public MyViewHolder(View itemView) {
        super(itemView);
        title= (TextView) itemView.findViewById(R.id.listText);
        icon= (ImageView) itemView.findViewById(R.id.listImage);
    }
  }
}

1 个答案:

答案 0 :(得分:0)

尝试快速而肮脏:

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    Information current=data.get(position);
    holder.title.setText(current.title);
    holder.icon.setImageResource(current.iconId);
    holder.icon.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
             Intent intent = new Intent(icon.getContext(), AnotherActivity.class);
             ((Activity) icon.getContext()).startActivity(intent);
       }
    }
}

更正式地说,您应该创建一个Activity将实现的界面,然后将您的onclick中的上下文转换为可以回调您的视图已被点击,然后在活动中启动Intent。

这样的事情:

public interface OnDazClickListener {
    void onDazClick(String daz);
}

public MyActivity extends Activity implements OnDazClickListener {

    @Override
    onCreate {
        LayoutInflater inflater = LayoutInflater.from(context);
        DazAdapter adapter = new DazAdapter(inflater, data, this);
        ...  
    } 

    ...

    @Override
    public void onDazClick(String daz) {
      Intent intent = new Intent(this, AnotherActivity.class);
      startActivity(intent);
    }

}

public class DazAdapter extends RecyclerView.Adapter<DazAdapter.MyViewHolder> {

    private final List<Information> data = Collections.emptyList();
    private final LayoutInflater inflater;
    private final OnDazClickListener listener;

    public DazAdapter(LayoutInflater inflater, List<Information> data, OnDazClickListener listener){
      this.inflater = inflater;
      this.data.addAll(data);
      this.listener = listener;
    }

    ...

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
      Information current=data.get(position);
      holder.title.setText(current.title);
      holder.icon.setImageResource(current.iconId);
      holder.icon.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
             listener.onDazClick("some information");
         }
      }
    }
}