【问题标题】:How i can add many RecyclerView into a Activity?我如何将许多 RecyclerView 添加到 Activity 中?
【发布时间】:2015-04-10 05:01:50
【问题描述】:

我尝试将 RecyclerView 列出到 Activity 中,但它不可能

这是主活动

private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LinearLayout layoutMain = new LinearLayout(this);
    layoutMain.setOrientation(LinearLayout.VERTICAL);
    setContentView(layoutMain);
    LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    RecyclerView recList = new RecyclerView(this);
    recList.setHasFixedSize(true);
    LinearLayoutManager llm = new LinearLayoutManager(this);
    llm.setOrientation(LinearLayoutManager.VERTICAL);
    recList.setLayoutManager(llm);

    custom_button buttons = new custom_button(this, "button");
    Button buttons2 = new Button(this);
    buttons2.setText("button2");

    ContactAdapter ca = new ContactAdapter(createList(3));
    recList.setAdapter(ca);

    layoutMain.addView(buttons); // this is custom button

    layoutMain.addView(recList); // this is RecyclerView but it

    layoutMain.addView(buttons2); 

}

完成后这是image

当我将许多视图添加到一个活动中时,它无法滚动:'(

谁能帮我解决这个问题

给我一​​些关键字或任何 TUT,非常感谢。 . .

p/s : 我只想制作一个像这样的谷歌商店特色列表的布局 IMAGE

【问题讨论】:

  • 使用相对布局。添加带有参数的按钮作为 alignParent 顶部按钮 2 作为 alignParent 底部和带有布局参数的回收器视图,其中按钮 2 上方和按钮下方。这将为您提供所需的布局。考虑给按钮起有意义的名字,并通过java命名约定(custom_button?)
  • ` ALIGN_PARENT_TOP CENTER_IN_PARENT ALIGN_PARENT_BOTTOM ` :'( 不工作你能帮我修复这个愚蠢的代码吗

标签: android listview gridview android-listview


【解决方案1】:

使用RelativeLayout 作为根并在按钮上设置RelativeLayout.LayoutParams 以与底部对齐。 您也可以使用 FrameLayout(边距和重力)来实现它,但据我所知,它在 Gingerbread 上效果不佳。

【讨论】:

    【解决方案2】:

    如果要启用滚动,请将 ScrollView 用作 layoutMain。 默认情况下,它有一个 LinearLayout。所以我建议制作一个布局xml来轻松完成。

    【讨论】:

    • java.lang.IllegalStateException: ScrollView 只能承载一个直接子节点
    • @ĐặngXuânVui 是的,这就是我建议制作布局 xml 的原因。制作它,并为子LinearLayout设置一个ID,您可以在onCreate()中通过findViewById(id)访问这个LinearLayout。然后与上面的代码相同。
    • 非常感谢,但我认为它不起作用(因为我确实尝试过)。 . .上面的布局很愚蠢。 . . .我想你可以为我建议一种更好的方法来制作像谷歌商店这样的布局。 . . . . .谢谢
    • @ĐặngXuânVui 您可以从此页面developer.android.com/guide/topics/ui/declaring-layout.html 获得提示。推荐的小部件是 ScrollView、LinearLayout(horizo​​ntal、vertical)、ListView。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多