我在片段类中有一个listview。单击列表视图项时,将打开另一个活动。该活动的xml文件在滚动视图中有2个图像视图。图像随点击的每个列表视图项而变化。我的应用中有5个列表视图。每个列表视图都有400多个项目。使用什么方法使其顺利运行。 switch语句是个好主意吗?目前我正在使用switch语句,到目前为止只有4个项目。我应该采用这种方法吗?
我的代码
public class urdufrag1 extends Fragment {
public urdufrag1() {
// Required empty public constructor
}
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
private static final String TAG = "urdufrag1";
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup
main_content, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.geetfrag_urdu, main_content, false);
final int[] menuImage = {R.drawable.tgeet94,
R.drawable.tgeet95,R.drawable.tgeet96,R.drawable.tgeet97};
final ListView listView = (ListView) view.findViewById(R.id.GeetListU);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Intent intent = new Intent(getActivity(), PackageG.class);
intent.putExtra("position", position);
startActivity(intent);
}
});
AdapterGeetUrdu adapter = new AdapterGeetUrdu(getContext(), menuImage);
listView.setAdapter(adapter);
// Inflate the layout for this fragment
return view;
}
}
第二项活动
public class PackageG extends AppCompatActivity {
ImageView img1, img2;
PhotoViewAttacher mAttacher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_package_g);
img1 = (ImageView) findViewById(R.id.imageView8);
img2 = (ImageView) findViewById(R.id.imageView9);
mAttacher = new PhotoViewAttacher(img1);
mAttacher = new PhotoViewAttacher(img2);
Intent intent = this.getIntent();
if(intent != null){
Integer position = intent.getExtras().getInt("position");
switch (position){
case 0: img1.setImageResource(R.drawable.geet94);
img2.setImageResource(R.drawable.geet94_1);
break;
case 1: img1.setImageResource(R.drawable.geet95);
img2.setImageResource(R.drawable.geet95_1);
break;
case 2: img1.setImageResource(R.drawable.geet96);
img2.setImageResource(R.drawable.geet96_1);
break;
default:
img1.setImageResource(R.drawable.carol);
img2.setImageResource(R.drawable.carol);
break;
}
}
mAttacher.update();
}
}
答案 0 :(得分:0)
这是TypedArray
的完美工作。使用类似
res/values/arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="firstImages">
<item>@drawable/R.drawable.geet94</item>
<item>@drawable/R.drawable.geet95</item>
<item>@drawable/R.drawable.geet96</item>
...
</array>
<array name="secondImages">
<item>@drawable/R.drawable.geet94_1</item>
<item>@drawable/R.drawable.geet95_1</item>
<item>@drawable/R.drawable.geet96_1</item>
...
</array>
</resources>
然后在你的第二个活动中:
Intent intent = this.getIntent();
if(intent != null){
Integer position = intent.getExtras().getInt("position");
Resources res = getResources();
TypedArray firstImages = res.obtainTypedArray(R.array.firstImages);
TypedArray secondImages = res.obtainTypedArray(R.array.secondImages);
Drawable firstDrawable = firstImages.getDrawable(position);
Drawable secondDrawable = secondImages.getDrawable(position);
img1.setImageDrawable(firstDrawable);
img2.setImageDrawable(secondDrawable);
}