我有三个标签和ViewPager在三个片段之间交换。 其中一个片段是ListFragment,它包含ListView。 问题是该列表不可滚动。
这是我添加标签和ViewPager的MainActivity:
private void addTabs() {
tabLayout = (TabLayout)findViewById(R.id.tabLayout);
tabLayout.addTab(tabLayout.newTab().setTag("first"));
tabLayout.addTab(tabLayout.newTab().setTag("second"));
tabLayout.addTab(tabLayout.newTab().setTag("third"));
viewPager = (ViewPager)findViewById(R.id.pager);
TabsPagerAdapter adapter = new TabsPagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
tabLayout.getTabAt(0).setText("Player");
tabLayout.getTabAt(1).setText("Playlist");
tabLayout.getTabAt(2).setText("Songs");
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
// Code goes here
}
@Override
public void onPageScrollStateChanged(int state) {
// Code goes here
}
});
}
MainActivity xml:
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/grayCustom"
app:popupTheme="@style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tabLayout"
android:fillViewport="false"
android:background="@color/grayCustom"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
app:tabIndicatorColor="@android:color/white"
style="@style/MyCustomTabLayout" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="@color/white"/>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
播放列表类:
public class Playlist {
private HashMap<String, Song> playList;
private final Context context;
public Playlist(Context context)
{
playList = new HashMap<String, Song>();
this.context = context;
updatePlayList();
}
private void updatePlayList()
{
ContentResolver cr = context.getContentResolver();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + "!= 0";
String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";
Cursor cur = cr.query(uri, null, selection, null, sortOrder);
int count = 0;
if(cur != null)
{
count = cur.getCount();
if(count > 0)
{
while(cur.moveToNext())
{
int id = cur.getInt(cur.getColumnIndex(MediaStore.Audio.Media._ID));
String name = cur.getString(cur.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
String path = cur.getString(cur.getColumnIndex(MediaStore.Audio.Media.DATA));
playList.put(name, new Song(id, name, path));
}
}
}
cur.close();
}
public HashMap<String, Song> getPlayList() {
return playList;
}
}
PlaylistFragment类:
public class PlaylistFragment extends ListFragment{
private static final String ARG_PAGE_NUMBER = "page_number";
private static PlaylistFragment mInstance = null;
private Playlist playList;
public static PlaylistFragment getInstance(int page) {
if(mInstance == null) {
mInstance = new PlaylistFragment();
Bundle args = new Bundle();
args.putInt(ARG_PAGE_NUMBER, page);
mInstance.setArguments(args);
}
return mInstance;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_playlist, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initialize();
}
private void initialize()
{
playList = new Playlist(getActivity());
ArrayList<String> songsNames = toKeysArray(playList.getPlayList());
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.list_items, songsNames);
setListAdapter(adapter);
}
private ArrayList<String> toKeysArray(HashMap<String, Song> playlist)
{
Set<String> set = playlist.keySet();
ArrayList<String> arr = new ArrayList<String>();
for(String name : set)
arr.add(name);
return arr;
}
}
PlaylistFragment xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ofekot.audioplayer.PlaylistFragment">
<ListView
android:id="@+id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
TabsPagerAdapter类:
公共类TabsPagerAdapter扩展了FragmentPagerAdapter {
private int numOfTabs;
public TabsPagerAdapter(FragmentManager fm, int numOfTabs) {
super(fm);
this.numOfTabs = numOfTabs;
}
@Override
public Fragment getItem(int position) {
switch(position) {
case 0:
return PlayerFragment.getInstance(position);
case 1:
return PlaylistFragment.getInstance(position);
case 2:
return SongsFragment.getInstance(position);
default:
return SongsFragment.getInstance(position);
}
}
@Override
public int getCount() {
return numOfTabs;
}
}
我试图解决这个问题几个小时没有运气。 TY
答案 0 :(得分:0)
我有同样的问题,它通过这段代码解决了:
as