我希望在我的stackView中添加一个onclickListner,以便它呈现另一个片段
片段将是一个包含textView和imageView的简单页面。 将有10个以上的片段,每个片段对应于StackView中的条目。
片段中的stackView所以它可能就像从另一个片段中调用片段
我的代码是:
Stack_Adapter.java
public class Stack_Adapter extends BaseAdapter {
ArrayList<Stack_Items> arrayList;
LayoutInflater inflater;
ViewHolder holder = null;
public Stack_Adapter(Context context, ArrayList<Stack_Items> arrayList) {
this.arrayList = arrayList;
this.inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return arrayList.size();
}
@Override
public Stack_Items getItem(int pos) {
return arrayList.get(pos);
}
@Override
public long getItemId(int pos) {
return pos;
}
@Override
public View getView(int pos, View view, ViewGroup parent) {
if (view == null) {
view = inflater.inflate(R.layout.stack_layout, parent, false);
holder = new ViewHolder();
holder.text = (TextView) view.findViewById(R.id.textView1);
holder.image = (ImageView) view.findViewById(R.id.imageView1);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.text.setText(arrayList.get(pos).getName());
holder.image.setBackgroundResource(arrayList.get(pos).getImage());
return view;
}
public class ViewHolder {
TextView text;
ImageView image;
} }
EventsFragment.java
public class EventsFragment extends android.support.v4.app.Fragment {
private StackView stackView;
private ArrayList<Stack_Items> list;
TypedArray eventLogo;
String eventName[];
@Nullable
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
eventLogo = getResources().obtainTypedArray(R.array.event_stack_icon);
eventName = getResources().getStringArray(R.array.event_stack);
View view = inflater.inflate(R.layout.events_layout, null);
stackView = (StackView) view.findViewById(R.id.stackView1);
list = new ArrayList<Stack_Items>();
//Adding items to the list
for (int i = 0; i < eventLogo.length(); i++) {
list.add(new Stack_Items(eventName[i], eventLogo.getResourceId(i, -1)));
}
//Calling adapter and setting it over stackView
Stack_Adapter adapter = new Stack_Adapter(getActivity().getApplicationContext(), list);
stackView.setAdapter(adapter);
stackView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View
view, int position, long id) {
MainActivity mainActivity = (MainActivity) getActivity();
FragmentTransaction fragmentTransaction = mainActivity.getSupportFragmentManager().beginTransaction();
switch (stackView.getSelectedItemPosition()) {
case 0:
Fragment homeFragment = new HomeFragment();
fragmentTransaction.replace(R.id.containerView,homeFragment);
fragmentTransaction.commit();
break;
case 1:
Fragment eventsFragment = new EventsFragment();
fragmentTransaction.replace(R.id.containerView,eventsFragment);
fragmentTransaction.commit();
break;
case 2:
Fragment encoreFragment = new EncoreFragment();
fragmentTransaction.replace(R.id.containerView,encoreFragment);
fragmentTransaction.commit();
break;
}
});
adapter.notifyDataSetChanged();
return view;
}
}
Stack_Items
public class Stack_Items {
String name;
Integer image;
public Stack_Items(String name, Integer image) {
this.name = name;
this.image = image;
}
public String getName() {
return name;
}
public int getImage() {
return image;
} }
events_layout
<StackView
android:id="@+id/stackView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:animateLayoutChanges="true" >
</StackView>
Stack_layout
<ImageView
android:id="@+id/imageView1"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerHorizontal="true" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_below="@+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:textColor="#FFFFFF" />
因为有这么多页面和所有类似的,我想找到一个简单的替代方法,通过链接strings.xml中的东西,如链接堆栈和所有..
修改-1
更新了EventsFragments.java