为什么recyclerView没有显示所有项目?

时间:2016-12-07 19:41:38

标签: android android-fragments android-recyclerview material-design recyclerview-layout

我在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>

现在,这是我I cant see all the list,Why its happening..?,Only half list is being shown, Rest is in tab, I have checked that by adding some list item at top.

中显示的内容

Plsss有人帮助.. !!!

修改 这是我的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);
}}

而且,这是我的Fragment Page Adapter类

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>

2 个答案:

答案 0 :(得分:0)

在viewpager中添加此内容

app:layout_behavior="@string/appbar_scrolling_view_behavior"

答案 1 :(得分:0)

您的构造函数应该如下所示

    public Myadapter(Context context, String[] arr)
   { 
      Resources resources =context.getResources();  
      name=arr;
   }