我在 android 中制作了一个 RecyclerView,带有一个选项卡布局和一个碎片化的 Activity。问题是没有显示所有视图。前 2 个视图总是被省略。这是我的零散活动的代码。
public class Listee extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.recyclerview, container, false);
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler);
recyclerView.setHasFixedSize(true);
Myadapter adapter = new Myadapter(new String[]{"test one", "test two", "test three", "test four", "test five", "test six", "test seven", "dadadaad", "dadasda", "dadasdasda"});
recyclerView.setAdapter(adapter);
LinearLayoutManager ll = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(ll);
return view;
}}
这是带有 ViewHolder 的 MyAdapter 的代码
class Myadapter extends RecyclerView.Adapter<Myadapter.MyViewHolder> {
String name[];
public Myadapter(Context context){
Resources resources =context.getResources();
name=resources.getStringArray(R.array.places);
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_tile, parent, false);
MyViewHolder v = new MyViewHolder(view);
return v;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.mtext.setText(name[position]);
}
@Override
public int getItemCount() {
return name.length;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView mtext;
public MyViewHolder(View itemView) {
super(itemView);
mtext = (TextView) itemView.findViewById(R.id.tv_text);
}
}}
这是我的 RecyclerView.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:id="@+id/recycler"
android:layout_height="wrap_content"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:clipToPadding="false"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
这是我的 item_tile.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="68dp" >
<TextView
android:id="@+id/tv_text"
android:text="my name is alpit"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center" >
</TextView>
现在,这就是我无法看到所有列表中显示的内容,为什么会发生这种情况..?,仅显示一半列表,Rest 在选项卡中,我通过在顶部添加一些列表项进行了检查。
请有人帮忙..!!!
编辑 这是我的 main_activity 布局
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewpager(viewPager);
TabLayout tableLayout = (TabLayout) findViewById(R.id.tabs);
tableLayout.setupWithViewPager(viewPager);
tableLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tableLayout.setTabMode(TabLayout.MODE_FIXED);
}
private void setupViewpager(ViewPager viewPager) {
Adapter adap = new Adapter(getSupportFragmentManager());
adap.addFragment(new Listee(), "List");
adap.addFragment(new Card(), "Card");
viewPager.setAdapter(adap);
}}
而且,这是我的片段页面适配器类
class Adapter extends FragmentPagerAdapter {
List<Fragment> frag = new ArrayList<>();
List<String> name = new ArrayList<>();
public Adapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return frag.get(position);
}
@Override
public int getCount() {
return name.size();
}
public void addFragment(Fragment fragment, String s) {
frag.add(fragment);
name.add(s);
}
public CharSequence getPageTitle(int position) {
return name.get(position);
}}
最后是 main_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"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:minHeight="0dp" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMaxWidth="0dp" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="569dp">
</android.support.v4.view.ViewPager>