【问题标题】:Why recyclerView is not showing all the items? [duplicate]为什么 recyclerView 没有显示所有项目? [复制]
【发布时间】:2017-04-22 21:34:15
【问题描述】:

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

这是 MyAdapter 和 ViewHolder 的代码

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.中显示的内容

请有人帮忙..!!!

编辑 这是我的 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>

【问题讨论】:

  • 请发布您的Activity 的主要布局。
  • 已发布...活动和布局。
  • Okie,对不起,不知道要搜索的内容是否正确。对不起。 :-(

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


【解决方案1】:

将此添加到您的浏览器中

app:layout_behavior="@string/appbar_scrolling_view_behavior"

【讨论】:

  • 不,先生,仍然没有变化。
  • 好的,把它添加到你的浏览器中
  • 好的,这已经修复了,但是你怎么知道它是错误的,这在整个应用程序中是一个非常小的东西。你们怎么能记住每个库的功能。Srsly,谢谢求助,卡在这里1周了。
  • 这些是初学者常犯的错误。关于这个问题有很多关于 SO 的问题,但只需要良好的搜索意识。很少有人记得确切的代码,但您只需要清除您的概念。发布答案时,我什至不知道确切的字符串,但知道有一个属性可以解决您的问题。请保持您的概念清晰。
【解决方案2】:

你的构造函数应该是这样的

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

【讨论】:

  • 但是,我没有传递 String[ ],这只会显示错误,不过感谢您的帮助。
  • 对不起,我误会了。
猜你喜欢
  • 1970-01-01
  • 2017-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-16
  • 2020-09-03
相关资源
最近更新 更多