2015-09-21 79 views
0

剛烤麪包作品上onClick()的onclick上工作(),沒有別的,我已在AndroidManifest意圖和startactivity不recyclerview適配器

IntentstartActivity中所有條目RecyclerView適配器上onClick()不工作而Toast工作正常

這裏是我的代碼

public class ForecastAdapter extends RecyclerView.Adapter<ForecastAdapter.MyForecastViewHolder> { 
public Context context; 

private LayoutInflater inflator; 

List<Information> data = Collections.emptyList(); 

public ForecastAdapter(Context context, List<Information> data) { 
    inflator = LayoutInflater.from(context); 
    this.data = data; 
    this.context = context; 
} 

@Override 
public MyForecastViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View view = inflator.inflate(R.layout.custom_row_forecast, parent, false); 
    MyForecastViewHolder holder = new MyForecastViewHolder(view); 
    return holder; 
} 

@Override 
public void onBindViewHolder(MyForecastViewHolder holder, final 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 MyForecastViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 
    TextView title; 
    ImageView icon; 

    public MyForecastViewHolder(View itemView) { 
     super(itemView); 
     itemView.setOnClickListener(this); 
     title = (TextView) itemView.findViewById(R.id.temp); 
     icon = (ImageView) itemView.findViewById(R.id.listIcon3); 
    } 

    @Override 
    public void onClick(View v) { 
     context.startActivity(new Intent(context,SubActivity.class)); 
    } 
} 
} 
+3

當你說「不工作,」該應用是否會崩潰?如果是這樣,請從Logcat發佈錯誤 –

+1

您應該閱讀[問];) – mithrop

回答

2

你將如何知道哪些位置已經CLI這種方式? 你應該改變你的clickListener到你的onBindView方法裏面,它可以給你點擊的位置並將其設置在你的根佈局上。

是這樣的:

class MyForecastViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 
    TextView title; 
    ImageView icon; 

    public MyForecastViewHolder(View itemView) { 
     super(itemView); 
     itemView.setOnClickListener(this); 
     title = (TextView) itemView.findViewById(R.id.temp); 
     icon = (ImageView) itemView.findViewById(R.id.listIcon3); 
     yourRootView = (LayoutType) itemView.findViewById(R.id.root); 
    } 

    @Override 
    public void onClick(View v) { 
     context.startActivity(new Intent(context,SubActivity.class)); 
    } 
} 

@Override 
public void onBindViewHolder(MyForecastViewHolder holder, final int position) { 
    Information current = data.get(position); 
    holder.title.setText(current.title); 
    holder.icon.setImageResource(current.iconId); 
    holder.yourRootView.setTag(position); // get it after with getTag() 
    holder.yourRootView.setOnClickListener(this); 
} 
相關問題