【问题标题】:How can I show a different listview for each button pressed如何为按下的每个按钮显示不同的列表视图
【发布时间】:2013-05-13 06:37:10
【问题描述】:

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


按钮1 |按钮2 |按钮3 |按钮4

列出项目 1

列出项目 2

列出项目 3

如果我按下 Button1,我将显示一个视图,如果我选择 Button2,我将显示另一个视图。我有没有选择按钮的列表视图。但是,我想选择一个按钮并根据所选按钮填充一个 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。此外,按钮没有焦点。任何建议将不胜感激

【问题讨论】:

  • 你可以使用android标签布局来显示不同的列表/页面
  • @itsrajesh4uguys 使用android标签布局有什么好处?
  • 您可以根据需要在页面中绑定不同的列表视图。
  • @itsrajesh4uguys 谢谢,你能给我指出任何好的例子吗?
  • 当然我会写一个样本并提供给你

标签: android listview button android-listview


【解决方案1】:

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

【讨论】:

  • 他也可以使用fragment和onClick的按钮替换使用fragmentManager的fragment。这种方式的好处是,如果他在其他任何地方都需要相同的列表,他可以直接使用它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-20
相关资源
最近更新 更多