刚刚在我的代码中实现了Recyclerview,取代了Listview.
一切正常.显示对象.
但是logcat说
15:25:53.476 E/RecyclerView:没有连接适配器; 跳过布局
15:25:53.655 E/RecyclerView:没有连接适配器; 跳过布局
代码
ArtistArrayAdapter adapter = new ArtistArrayAdapter(this, artists);
recyclerView = (RecyclerView) findViewById(R.id.cardList);
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
Run Code Online (Sandbox Code Playgroud)
如您所见,我已经为Recycleview附加了一个适配器.那为什么我一直收到这个错误?
我已经阅读了与同一问题相关的其他问题但没有帮助.
我正在玩新的CardView,但边缘似乎没有应用.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:layout_marginLeft="18dp"
android:layout_marginRight="18dp"
card_view:cardCornerRadius="4dp"
card_view:cardBackgroundColor="#FFA"
card_view:layout_margind="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/info_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"/>
</LinearLayout>
</android.support.v7.widget.CardView>
Run Code Online (Sandbox Code Playgroud)
顺便说一下,我在ViewPager中的片段中使用它.该卡扩展了屏幕的整个宽度(match_parent),即使我在CardView上使用android:layout_marginLeft ="18dp"和android:layout_marginRight ="18dp".
我有什么想法可能做错了吗?
我设计了一个带有地图片段和recyclerView的布局.每个recyclerView项都是cardview(我已指定给出xml布局).
问题是RecyclerView项目没有填充屏幕宽度. img在这里
我试图将layout_width更改为fill_parent,match_parent但它无能为力
这是每个项目的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:id="@+id/cardView">
<LinearLayout
android:id="@+id/locationItemView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="120px"
android:layout_height="120px"
android:id="@+id/imgIcon"
android:background="@drawable/image_bg"
android:layout_margin="5dp"
android:scaleType="fitCenter"
android:adjustViewBounds="true" />
<LinearLayout
android:id="@+id/layoutInfo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="@+id/txtName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Location Name"
android:textColor="#d5c645"
android:textStyle="bold"
android:textSize="20dp"
android:padding="3dp" />
<TextView
android:id="@+id/txtAddress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Location Address"
android:textSize="16dp"
android:padding="3dp" />
<TextView
android:id="@+id/txtDistance"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:text="Location Distance"
android:textSize="14dp"
android:textStyle="italic"
android:padding="2dp"
android:textAlignment="viewEnd" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
Run Code Online (Sandbox Code Playgroud)
和main_layout
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" …Run Code Online (Sandbox Code Playgroud) 当layout_width为wrap_content而没有成功时,我试图将RecyclerView作为中心
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_schemes"
android:scrollbars="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
当RecyclerView给出任何明确的layout_width,例如200dp时,它确实居中,否则它只是左对齐.
如何在其layout_width为wrap_content时使RecyclerView为center_horizontal?
xml android android-layout android-studio android-recyclerview
我正在使用 recylcer 视图,每一行都包含 cardview。即使我的 textview 内容较少,我也需要显示 cardview 全宽。我尝试了很多方法但没有用。
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/primaryDarkColor"
android:orientation="vertical">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardview"
android:background="#ffffff"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="8dp"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="2dp"
android:orientation="horizontal">
<TextView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="3dp"
android:text="content"
android:layout_weight="1"
android:textColor="#ff212121"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
即使我设置了宽度以匹配父级但没有用。
更新
回收者视图
<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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#e5e5e5"
android:orientation="horizontal">
<android.support.v7.widget.RecyclerView
android:id="@+id/List"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
更新:使用以下方法修复
我已使用以下答案解决了我的问题
膨胀的文档:
从指定的 xml 资源膨胀一个新的视图层次结构。如果有错误,则抛出 InflateException。
要加载的 XML 布局资源的参数资源 ID(例如,R.layout.main_page)作为生成层次结构的父视图的根视图(如果 attachToRoot …