gpt4 book ai didi

android - ViewPager 内的 ListView 不滚动

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:46:03 27 4
gpt4 key购买 nike

我的 Activity 中有一个 ViewPager。我使用 Fragment 将列表显示到 ViewPager 中。在 ViewPager 中,我可以设置一个 ListViewListView 可以正常显示,但 ListView 不滚动。列表中的项目太少无法显示。

我该如何解决。

主 Activity

public class MainActivity extends Activity {

private PageAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;

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

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);

setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new PageAdapter(getSupportFragmentManager(), 3);

// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);

TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);

System.out.println(tabLayout.getTabCount());
/* for (int i = 0; i < tabLayout.getChildCount(); i++) {
tabLayout.getChildAt(i).setBackgroundResource(R.drawable.tab_background_select);
}*/

mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

@Override
public void onPageSelected(int position) {
Fragment j = mSectionsPagerAdapter.getItem(position);
if (j instanceof Downloads) {
((Downloads) j).refresh();
}
}

@Override
public void onPageScrollStateChanged(int state) {

}
});
File folder = new File(Const.APP_folder);
boolean success = true;
if (!folder.exists()) {
folder.mkdir();
}
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {

/**
* Declare ListView .
*/
ListView lv;

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);

/**
* Inititlize.
*/
lv = (ListView) rootView.findViewById(R.id.listView);

return rootView;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

List<String> lst = new ArrayList<String>();
for(int i = 1; i <= 10; i++) {
lst.add(i + "");
}

ArrayAdapter<String> ArAd = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, lst);
lv.setAdapter(ArAd);

}

}

}

这是我的 ListView 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.app.down.fragments.PlaceholderFragment">

<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
</LinearLayout>

主 Activity .xml

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
android:fitsSystemWindows="true"
tools:context="com.app.down.MainActivity">

<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
style="@style/HeaderBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay">

<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Home"
android:textColor="#efe6c7"
android:textSize="30dp" />


</android.support.v7.widget.Toolbar>

<android.support.design.widget.TabLayout
android:id="@+id/tabs"
style="@style/MyCustomTabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/activity_horizontal_margin"
android:background="@drawable/tab_main_border"
app:tabBackground="@drawable/tab_background_select" />

</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

最佳答案

这是 listview 的一个已知限制,您可以使用 recyclerview、viewpager、co-ordinator 布局,它们可以很好地协同工作。

关于android - ViewPager 内的 ListView 不滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35577206/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com