如何使用两个运行不同查询的按钮在Android ListView中创建标题?

时间:2015-12-02 21:08:54

标签: java android listview android-listview

不幸的是,我不知道这类设计的正确Android术语:

Android Header ListView

我的Android应用程序中有一个ListView元素,具体取决于类别(上图 NOTES(2) LISTS(1) ,我想在ListView中显示不同的结果。

所以,我的问题如下:

如何扩展我的ListView以使其工作?我只需要在顶部放置两个不同的按钮,还是要使用特定的视图?

2 个答案:

答案 0 :(得分:1)

我想你想要创建一个选项卡式视图。要使用Android材质设计创建选项卡式视图,您可以使用TabLayout和View Pager。

对于每个标签,您需要创建一个片段。像这样

public class Tab1 extends Fragment {

//Overriden method onCreateView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    //Returning the layout file after inflating 
    //Change R.layout.tab1 in you classes 
    return inflater.inflate(R.layout.tab1, container, false);
}
}

看看我们要返回的是inflater.inflate(r.layout.tab1 .. 这是要显示的选项卡的xml资源文件。

您还需要一个可以使用FragmentStatePagerAdapter类实现的类适配器。

public class Pager extends FragmentStatePagerAdapter {

//integer to count number of tabs
int tabCount;

//Constructor to the class 
public Pager(FragmentManager fm, int tabCount) {
    super(fm);
    //Initializing tab count
    this.tabCount= tabCount;
}

//Overriding method getItem
@Override
public Fragment getItem(int position) {
    //Returning the current tabs 
    switch (position) {
        case 0:
            Tab1 tab1 = new Tab1();
            return tab1;
        case 1:
            Tab2 tab2 = new Tab2();
            return tab2;
        case 2:
            Tab3 tab3 = new Tab3();
            return tab3;
        default:
            return null;
    }
}

//Overriden method getCount to get the number of tabs 
@Override
public int getCount() {
    return tabCount;
}
}

最后,只需在您的活动中实施标签即可。

public class MainActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener{

//This is our tablayout
private TabLayout tabLayout;

//This is our viewPager
private ViewPager viewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Adding toolbar to the activity
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    //Initializing the tablayout
    tabLayout = (TabLayout) findViewById(R.id.tabLayout);

    //Adding the tabs using addTab() method
    tabLayout.addTab(tabLayout.newTab().setText("Tab1"));
    tabLayout.addTab(tabLayout.newTab().setText("Tab2"));
    tabLayout.addTab(tabLayout.newTab().setText("Tab3"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    //Initializing viewPager
    viewPager = (ViewPager) findViewById(R.id.pager);

    //Creating our pager adapter
    Pager adapter = new Pager(getSupportFragmentManager(), tabLayout.getTabCount());

    //Adding adapter to pager
    viewPager.setAdapter(adapter);

    //Adding onTabSelectedListener to swipe views
    tabLayout.setOnTabSelectedListener(this);
}

@Override
public void onTabSelected(TabLayout.Tab tab) {
    viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {

}

@Override
public void onTabReselected(TabLayout.Tab tab) {

}
}

有关详细信息,请查看本教程,这对我来说非常合适。 Android TabLayout Example using ViewPager and Fragments

答案 1 :(得分:0)

尝试使用TabLayout,请检查此link

可能最好的解决方案是使用TabLayout和ViewPager使用两个片段(每个片段一个)。检查提供的链接以获得全面的教程