如何为每个按下的按钮显示不同的列表视图

时间:2013-05-18 02:28:43

标签: android listview button android-listview

我有一排按钮,我想要填充列表视图。看起来像这样:


Button1 | Button2 | Button3 |将Button4

列出项目1

清单第2项

清单第3项

如果我按下Button1,我将显示一个视图,如果我选择Button2,我将显示另一个视图。我有listview工作没有选择按钮。但是,我想选择一个按钮并根据所选按钮填充listveiw。这只是我的第二个应用程序,所以我可能会采用错误的方式。

这是我的布局文件:         

<LinearLayout 
    android:layout_weight="2" 
    android:layout_height="fill_parent" 
    android:layout_width="match_parent" 
    >
<VideoView 
    android:id="@+id/video_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    />
</LinearLayout> 

<LinearLayout 
    android:layout_weight="1" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent"
    android:orientation="vertical"  >  
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/chapters"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/btn_layout_height"
            android:textSize="@dimen/text_size"
            android:background="@drawable/button_custom"
            android:text="@string/chapters" /> 

        <Button
            android:id="@+id/scales"
            android:layout_width="100dp"
            android:layout_height="@dimen/btn_layout_height"
            android:textSize="@dimen/text_size"
            android:background="@drawable/button_custom"
            android:text="@string/scales" />

        <Button
            android:id="@+id/b_flat"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/btn_layout_height"
            android:textSize="@dimen/text_size"
            android:background="@drawable/button_custom"
            android:text="@string/b_flat" /> 

        <Button
            android:id="@+id/e_flat"
            android:layout_width="75dp"
            android:layout_height="@dimen/btn_layout_height"    
            android:textSize="@dimen/text_size"
            android:background="@drawable/button_custom"
            android:text="@string/e_flat" /> 

        <Button
            android:id="@+id/concert"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/btn_layout_height"
            android:textSize="@dimen/text_size"
            android:background="@drawable/button_custom"
            android:text="@string/concert" />

        <Button
            android:id="@+id/bass"
            android:layout_width="75dp"
            android:layout_height="@dimen/btn_layout_height"
            android:textSize="@dimen/text_size"
            android:background="@drawable/button_custom"
            android:text="@string/bass" />  

        <Button
            android:id="@+id/video"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/btn_layout_height"
            android:textSize="@dimen/text_size"
            android:background="@drawable/button_custom"
            android:text="@string/video" />

        <Button
            android:id="@+id/practice"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/btn_layout_height"    
            android:textSize="@dimen/text_size"
            android:background="@drawable/button_custom"
            android:text="@string/practice" />                                                      
    </LinearLayout>

<FrameLayout 
          android:id="@+id/fragmentContainer"
          android:layout_height="match_parent"
          android:layout_width="match_parent" 
          />
</LinearLayout>

我想选择“章节”按钮并填充列表按钮或选择“音乐会”并填充列表视图。

我不知道按下按钮的位置或方法。这是我尝试失败的原因:

     public class ChapterListFragment extends ListFragment {
private static final boolean VERBOSE = true;
private ArrayList<Chapters> mChapters;
private static final String TAG = "ChapterListFragment";

private Button mChaptersButton;

@Override
public void onCreate(Bundle savedInstanceState) {
    if (VERBOSE) Log.v(TAG, "+++ onCreate +++");
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_improvisation);
    mChaptersButton = (Button)v.findViewById(R.id.chapters);
    mChaptersButton.setPressed(true);

    getActivity().setTitle(R.string.chapters_title);
    mChapters = ChapterList.get(getActivity()).getChapters();

    ChapterAdapter adapter = new ChapterAdapter(mChapters);
    setListAdapter(adapter);        

}

这不起作用,因为我无法在ListFragment中使用SetContentView。此外,按钮没有焦点。任何建议将不胜感激

1 个答案:

答案 0 :(得分:1)

创建公共列表视图适配器,并在每次单击按钮时使用不同的数据集填充列表视图。请务必致电adapter.clear(),以便listview中的旧数据消失。我希望你有这个主意。对于setContentView, non of ListFragment and Fragment parent have the setContentView()`方法。您应该有一个带有布局的活动,并在那里设置ListFragment。请查看此链接Fragment setcontentView Error