Created
August 15, 2013 01:30
-
-
Save andrewhr/6237462 to your computer and use it in GitHub Desktop.
List view com uma view especial de header que contém um `ViewPager`. Esse código permite a navegação normal da `ListView` caso algum dos itens seja tocado. Para o caso de tocar diretamente no `ViewPager`, ele assume os eventos. Para tanto, além do código normalmente esperado para cabeçalhos de uma `ListView`, precisamos adicionar um listener de …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends FragmentActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
View header = getLayoutInflater().inflate(R.layout.sections, null); | |
assert header != null; | |
ViewPager viewPager = (ViewPager) header.findViewById(R.id.view_pager); | |
viewPager.setAdapter(new ReallyAwesomeAdapter(getSupportFragmentManager())); | |
viewPager.setOnTouchListener(new View.OnTouchListener() { | |
@Override | |
public boolean onTouch(View view, MotionEvent event) { | |
// don't let potentially parent scroll views to be confused with our | |
// touch event | |
switch (event.getAction()) { | |
case MotionEvent.ACTION_DOWN: | |
case MotionEvent.ACTION_MOVE: | |
case MotionEvent.ACTION_UP: | |
((ViewGroup) view).requestDisallowInterceptTouchEvent(true); | |
break; | |
} | |
// don't consume event, so the view pager can process it as expected | |
return false; | |
} | |
}); | |
String[] data = getResources().getStringArray(R.array.items); | |
ListView listView = (ListView) findViewById(R.id.list_view); | |
listView.addHeaderView(header); | |
listView.setAdapter(new ArrayAdapter<String>( | |
this, | |
android.R.layout.simple_list_item_1, | |
android.R.id.text1, | |
data | |
)); | |
listView.setEmptyView(findViewById(R.id.blank_slate)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sou seu fã ❤️