gpt4 book ai didi

java - 如何在StackView中实现onClickListener

转载 作者:行者123 更新时间:2023-11-30 01:50:36 25 4
gpt4 key购买 nike

我想在我的 stackView 中添加一个 onclickListner,以便它呈现另一个 fragment ,

fragment 将是一个包含 TextView 和 ImageView 的简单页面。将有 10 多个 fragment ,每个 fragment 对应于 StackView 中的一个条目。

fragment 中的 stackView,所以它可能就像从另一个 fragment 调用一个 fragment

我的代码是:

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;
} }

事件布局

<StackView
android:id="@+id/stackView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:animateLayoutChanges="true" >
</StackView>

堆栈布局

<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 中的东西找到一个简单的替代方法,比如链接堆栈和所有..

EDIT-1

更新的 EventsFragments.java

最佳答案

StackView 间接继承自 AdapterView ,这意味着您可以使用 setOnItemClickListener在上面,就像您对 ListView

所做的那样

关于java - 如何在StackView中实现onClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33105467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com