2015-12-02 20 views
0

不幸的是,我不知道這種類型的設計正確的Android術語:如何用兩個運行不同查詢的按鈕在Android ListView中創建一個頭文件?

Android Header ListView

我根據我的Android應用程序一個ListView元素,並在其上(在上面圖片(1)備註(2)LISTS(1))被選中,我想在ListView中顯示不同的結果。

所以,我的問題如下:

我如何才能使這項工作延長我的ListView?我只需要放置在頂部的兩個不同的按鈕,還是有一個特定的視圖使用?

+1

這些是標籤;您可以根據用戶選擇哪個選項卡來加載適當的數據。 – Eenvincible

+0

謝謝!對,就是那樣。 – user3475602

回答

0

嘗試使用TabLayout,檢查此link

也許是最好的解決辦法是使用TabLayout與使用ViewPager兩個片段(一個爲每個標籤)。檢查提供的全面教程鏈接

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

相關問題