我正在尝试找出一种更聪明的方法来在我的onBindViewHolder
中创建动态意图。
public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int position) {
viewHolder.ivIntroList.setImageResource(mIntroListImages.get(position));
viewHolder.tvIntroList.setText(mIntroListNames.get(position));
viewHolder.ivIntroList.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
switch (position){
case 0:
Intent intentHero = new Intent(mContext,heroActivity.class);
mContext.startActivity(intentHero);
break;
case 1:
Intent intentItem = new Intent(mContext,itemActivity.class);
mContext.startActivity(intentItem);
break;
.
.
.
case n:
break
}
}
});
使用职位来创建自己的意图的更聪明的方法是什么?
我知道switch
并不是最聪明的解决方案。
重要的是要知道我想使用mIntroListNames,因为我们那里有hero,item,spell ...等,而这些活动都被称为heroActivity,itemActivity,spellActivity ...
这就是为什么我认为应该有一个更明智的解决方案!
答案 0 :(得分:0)
尝试引用此示例代码,希望对您有所帮助: 通过使用intent.put extra,我们可以使用所需数据动态更新单个活动:
public void onBindViewHolder(backadapter.exViewHolder holder, final int position){
holder.exname.setText(exList.get(position).name);
holder.exlogo.setImageResource(exList.get(position).logoId);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (position == 0 ){
Intent i1=new Intent(context,abdetails.class);
i1.putExtra("img",R.drawable.twobarpullups);
i1.putExtra("text",R.string.crunches);
context.startActivity(i1);
}
if (position == 1) {
Intent i1=new Intent(context,abdetails.class);
i1.putExtra("img",R.drawable.highpulleyrowgif);
i1.putExtra("text",R.string.declinecrunch);
context.startActivity(i1);
}
if (position == 2) {
Intent i1=new Intent(context,abdetails.class);
i1.putExtra("img",R.drawable.chinupsgif);
i1.putExtra("text",R.string.dumsidebend);
context.startActivity(i1);
}
if (position == 3) {
Intent i1=new Intent(context,abdetails.class);
i1.putExtra("img",R.drawable.curlbaronearmrow);
i1.putExtra("text",R.string.hanglegraise);
context.startActivity(i1);
}
if (position == 4) {
Intent i1=new Intent(context,abdetails.class);
i1.putExtra("img",R.drawable.hangdumrowgif);
i1.putExtra("text",R.string.inclineleg);
context.startActivity(i1);
}
if (position == 5) {
Intent i1=new Intent(context,abdetails.class);
i1.putExtra("img",R.drawable.ropechingif);
i1.putExtra("text",R.string.legraise);
context.startActivity(i1);
}
if (position == 6) {
Intent i1=new Intent(context,abdetails.class);
i1.putExtra("img",R.drawable.deadliftgif);
i1.putExtra("text",R.string.flatbenchlyingleg);
context.startActivity(i1);
}
if (position == 7) {
Intent i1=new Intent(context,abdetails.class);
i1.putExtra("img",R.drawable.seatedrowsgif);
i1.putExtra("text",R.string.jackknife);
context.startActivity(i1);
}
if (position == 8) {
Intent i1=new Intent(context,abdetails.class);
i1.putExtra("img",R.drawable.barbentoverrowgif);
i1.putExtra("text",R.string.jackknife);
context.startActivity(i1);
}
if (position == 9) {
Intent i1=new Intent(context,abdetails.class);
i1.putExtra("img",R.drawable.bentoverreversegripgif);
i1.putExtra("text",R.string.twisthip);
context.startActivity(i1);
}
if (position == 10) {
Intent i1=new Intent(context,abdetails.class);
i1.putExtra("img",R.drawable.tbarrowsgif);
i1.putExtra("text",R.string.weightcrunch);
context.startActivity(i1);
}
if (position == 11) {
Intent i1=new Intent(context,abdetails.class);
i1.putExtra("img",R.drawable.straigharmpulldowngif);
i1.putExtra("text",R.string.plank);
context.startActivity(i1);
}
}
});
}
目标活动的主要类别的代码:
public class abdetails extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_abdetails);
GifImageView gifImageView;
gifImageView=findViewById(R.id.gifview);
Intent i1=getIntent();
TextView tv;
tv = findViewById(R.id.exdesc);
tv.setMovementMethod(new ScrollingMovementMethod());
gifImageView.setBackgroundResource(i1.getIntExtra("img",0));
tv.setText(i1.getIntExtra("text",0));
}