【发布时间】:2012-02-08 16:51:09
【问题描述】:
我正在使用 Fragments 和兼容性支持构建一个选项卡式应用程序。
在其中一个选项卡中,我想要一个列表视图。在同一个选项卡中,我必须按按钮来更改列表视图项(假设每个按钮都分配了一个字符串数组)。 我不知道如何做到这一点。我已经为按钮构建了布局和 clicklisteners(它们正在工作,因为我用 toast 消息对其进行了测试)。
你能给我一个提示吗?我会将列表视图代码放在哪里?在 OnClick 上?在 OnCreateView 上?
对不起,我对android没有太多经验。
这是我的片段代码。
public class TabFragment6 extends ListFragment {
/** (non-Javadoc)
* @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
*/
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
// We have different layouts, and in one of them this
// fragment's containing frame doesn't exist. The fragment
// may still be created from its saved state, but there is
// no reason to try to create its view hierarchy because it
// won't be displayed. Note this is not needed -- we could
// just run the code below, where we would create and return
// the view hierarchy; it would just never be used.
return null;
}
LinearLayout theLayout = (LinearLayout)inflater.inflate(R.layout.tab_frag6_layout, container, false);
// Register for the Button.OnClick event GUITARRA
Button bShopGuit = (Button)theLayout.findViewById(R.id.buttonshopguit);
bShopGuit.setOnClickListener(new View.OnClickListener() { //abre setonclicklistener(
@Override
public void onClick(View v) {
Toast.makeText(TabFragment6.this.getActivity(), "GUITAR button clicked", Toast.LENGTH_LONG).show();
}
}); //fecha )
// Register for the Button.OnClick event BAIXO
Button bShopBass = (Button)theLayout.findViewById(R.id.buttonshopbass);
bShopBass.setOnClickListener(new View.OnClickListener() { //abre setonclicklistener(
@Override
public void onClick(View v) {
Toast.makeText(TabFragment6.this.getActivity(), "BASS button clicked", Toast.LENGTH_LONG).show();
}
}); //fecha )
return theLayout;
}
}
【问题讨论】:
标签: android android-listview android-fragments